Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/release/next_whats_new/mathtext_limits.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
``\limits`` and ``\nolimits`` in mathtext
-----------------------------------------
Mathtext now parses the ``\limits`` and ``\nolimits`` commands. They control
whether the sub- and superscripts of the preceding operator are placed directly
above and below it, or to its side:

- ``\limits`` forces over/under placement, e.g. ``$\int\limits_a^b$`` renders
the bounds above and below the integral sign.
- ``\nolimits`` forces side placement, e.g. ``$\sum\nolimits_i^n$`` renders the
bounds to the right of the summation sign.

Previously these commands raised a parse error.
9 changes: 9 additions & 0 deletions galleries/users_explain/text/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@
#
# \sum_{i=0}^\infty x_i
#
# You can override the default placement with ``\limits`` (force under/over) and
# ``\nolimits`` (force to the side) directly after the operator::
#
# r'$\int\limits_0^\infty x_i \quad \sum\nolimits_{i=0}^\infty x_i$'
#
# .. math::
#
# \int\limits_0^\infty x_i \quad \sum\nolimits_{i=0}^\infty x_i
#
# Fractions, binomials, and stacked numbers
# -----------------------------------------
# Fractions, binomials, and stacked numbers can be created with the
Expand Down
20 changes: 17 additions & 3 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2267,12 +2267,18 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
content=Group(OneOrMore(p.token)) +
ZeroOrMore(Literal("\\\\").suppress()))("parts"))

# \limits and \nolimits force (resp. forbid) the sub/superscripts of the
# preceding operator to be placed above/below rather than to the side.
p.limits = Regex(r"\\(?:no)?limits(?![A-Za-z])")("limits")

p.subsuper = (
(Optional(p.placeable)("nucleus")
+ Optional(p.limits)
+ OneOrMore(one_of(["_", "^"]) - p.placeable)("subsuper")
+ Regex("'*")("apostrophes"))
| Regex("'+")("apostrophes")
| (p.named_placeable("nucleus") + Regex("'*")("apostrophes"))
| (p.named_placeable("nucleus")
+ Optional(p.limits) + Regex("'*")("apostrophes"))
)

p.simple = p.space | p.customspace | p.font | p.subsuper
Expand Down Expand Up @@ -2627,6 +2633,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
nucleus = toks.get("nucleus", Hbox(0))
subsuper = toks.get("subsuper", [])
napostrophes = len(toks.get("apostrophes", []))
limits = toks.get("limits")

if not subsuper and not napostrophes:
return nucleus
Expand Down Expand Up @@ -2662,8 +2669,15 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
super.kern()
super.hpack()

# Handle over/under symbols, such as sum or prod
if self.is_overunder(nucleus):
# Handle over/under symbols, such as sum or prod. \limits and
# \nolimits override the default placement for the nucleus.
if limits == r"\limits":
overunder = True
elif limits == r"\nolimits":
overunder = False
else:
overunder = self.is_overunder(nucleus)
if overunder:
vlist = []
shift = 0.
width = nucleus.width
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,23 @@ def test_short_long_accents(fig_test, fig_ref):
0, .5, "$" + "".join(fr"\{l} a" for l in corresponding_long_accs) + "$")


@check_figures_equal()
def test_limits(fig_test, fig_ref):
# \limits forces over/under placement, matching the default for \sum;
# \nolimits forces side placement, matching the default for \int.
fig_test.text(0.5, 0.5, r"$\sum\limits_a^b x \quad \int\nolimits_a^b x$")
fig_ref.text(0.5, 0.5, r"$\sum_a^b x \quad \int_a^b x$")


def test_limits_parse():
# \limits and \nolimits should parse without error, including with no
# following sub/superscript (gh-28051).
parser = mathtext.MathTextParser("agg")
for expr in [r"$\int\limits_a^b$", r"$\sum\nolimits_i x$",
r"$\lim\limits_{x\to 0} f$", r"$\int\limits$"]:
parser.parse(expr)


def test_fontinfo():
fontpath = mpl.font_manager.findfont("DejaVu Sans")
font = mpl.ft2font.FT2Font(fontpath)
Expand Down
Loading