Skip to content

Commit feecd7c

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 feecd7c

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 14 additions & 18 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:
@@ -2500,9 +2496,9 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
25002496
result = Hlist([
25012497
vlt,
25022498
*([self._make_space(self._space_widths[r'\,'])]
2503-
if self._in_subscript_or_superscript else []),
2499+
if self._needs_space_after_subsuper else []),
25042500
])
2505-
self._in_subscript_or_superscript = False
2501+
self._needs_space_after_subsuper = False
25062502
return [result]
25072503

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

25952591
# Do we need to add a space after the nucleus?
25962592
# 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)
2593+
result = Hlist([
2594+
nucleus, x,
2595+
*([self._make_space(self._space_widths[r'\,'])]
2596+
if self._needs_space_after_subsuper else []),
2597+
])
2598+
self._needs_space_after_subsuper = False
26032599
return [result]
26042600

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

0 commit comments

Comments
 (0)