Skip to content

Commit 2ae07a5

Browse files
committed
Tweak sub/superscript spacing implementation.
Rename _in_subscript_or_superscript to the more descriptive _needs_space_after_subsuper; simplify its setting in operatorname(); avoid the need to introduce an extra explicitly-typed spaced_nucleus variable.
1 parent 703c086 commit 2ae07a5

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
21472147
self._math_expression = p.math
21482148

21492149
# To add space to nucleus operators after sub/superscripts
2150-
self._in_subscript_or_superscript = False
2150+
self._needs_space_after_subsuper = False
21512151

21522152
def parse(self, s: str, fonts_object: Fonts, fontsize: float, dpi: float) -> Hlist:
21532153
"""
@@ -2165,7 +2165,7 @@ def parse(self, s: str, fonts_object: Fonts, fontsize: float, dpi: float) -> Hli
21652165
# explain becomes a plain method on pyparsing 3 (err.explain(0)).
21662166
raise ValueError("\n" + ParseException.explain(err, 0)) from None
21672167
self._state_stack = []
2168-
self._in_subscript_or_superscript = False
2168+
self._needs_space_after_subsuper = False
21692169
# prevent operator spacing from leaking into a new expression
21702170
self._em_width_cache = {}
21712171
ParserElement.reset_cache()
@@ -2275,7 +2275,7 @@ def symbol(self, s: str, loc: int,
22752275
prev_char = next((c for c in s[:loc][::-1] if c != ' '), '')
22762276
# Binary operators at start of string should not be spaced
22772277
# Also, operators in sub- or superscripts should not be spaced
2278-
if (self._in_subscript_or_superscript or (
2278+
if (self._needs_space_after_subsuper or (
22792279
c in self._binary_operators and (
22802280
len(s[:loc].split()) == 0 or prev_char in {
22812281
'{', *self._left_delims, *self._relation_symbols}))):
@@ -2381,13 +2381,9 @@ def operatorname(self, s: str, loc: int, toks: ParseResults) -> T.Any:
23812381
# Add thin space except when followed by parenthesis, bracket, etc.
23822382
hlist_list += [self._make_space(self._space_widths[r'\,'])]
23832383
self.pop_state()
2384-
# if followed by a super/subscript, set flag to true
2385-
# This flag tells subsuper to add space after this operator
2386-
if next_char in {'^', '_'}:
2387-
self._in_subscript_or_superscript = True
2388-
else:
2389-
self._in_subscript_or_superscript = False
2390-
2384+
# If followed by a sub/superscript, set flag to true to tell subsuper
2385+
# to add space after this operator.
2386+
self._needs_space_after_subsuper = next_char in {'^', '_'}
23912387
return Hlist(hlist_list)
23922388

23932389
def start_group(self, toks: ParseResults) -> T.Any:
@@ -2497,12 +2493,10 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
24972493
shift = hlist.height + vgap + nucleus.depth
24982494
vlt = Vlist(vlist)
24992495
vlt.shift_amount = shift
2500-
result = Hlist([
2501-
vlt,
2502-
*([self._make_space(self._space_widths[r'\,'])]
2503-
if self._in_subscript_or_superscript else []),
2504-
])
2505-
self._in_subscript_or_superscript = False
2496+
optional_spacing = ([self._make_space(self._space_widths[r'\,'])]
2497+
if self._needs_space_after_subsuper else [])
2498+
self._needs_space_after_subsuper = False
2499+
result = Hlist([vlt, *optional_spacing])
25062500
return [result]
25072501

25082502
# We remove kerning on the last character for consistency (otherwise
@@ -2594,12 +2588,10 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
25942588

25952589
# Do we need to add a space after the nucleus?
25962590
# To find out, check the flag set by operatorname
2597-
spaced_nucleus: list[Node] = [nucleus, x]
2598-
if self._in_subscript_or_superscript:
2599-
spaced_nucleus += [self._make_space(self._space_widths[r'\,'])]
2600-
self._in_subscript_or_superscript = False
2601-
2602-
result = Hlist(spaced_nucleus)
2591+
optional_spacing = ([self._make_space(self._space_widths[r'\,'])]
2592+
if self._needs_space_after_subsuper else [])
2593+
self._needs_space_after_subsuper = False
2594+
result = Hlist([nucleus, x, *optional_spacing])
26032595
return [result]
26042596

26052597
def _genfrac(self, ldelim: str, rdelim: str, rule: float | None, style: _MathStyle,

0 commit comments

Comments
 (0)