@@ -2132,7 +2132,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
21322132 self ._math_expression = p .math
21332133
21342134 # To add space to nucleus operators after sub/superscripts
2135- self ._in_subscript_or_superscript = False
2135+ self ._needs_space_after_subsuper = False
21362136
21372137 def parse (self , s : str , fonts_object : Fonts , fontsize : float , dpi : float ) -> Hlist :
21382138 """
@@ -2150,7 +2150,7 @@ def parse(self, s: str, fonts_object: Fonts, fontsize: float, dpi: float) -> Hli
21502150 # explain becomes a plain method on pyparsing 3 (err.explain(0)).
21512151 raise ValueError ("\n " + ParseException .explain (err , 0 )) from None
21522152 self ._state_stack = []
2153- self ._in_subscript_or_superscript = False
2153+ self ._needs_space_after_subsuper = False
21542154 # prevent operator spacing from leaking into a new expression
21552155 self ._em_width_cache = {}
21562156 ParserElement .reset_cache ()
@@ -2260,7 +2260,7 @@ def symbol(self, s: str, loc: int,
22602260 prev_char = next ((c for c in s [:loc ][::- 1 ] if c != ' ' ), '' )
22612261 # Binary operators at start of string should not be spaced
22622262 # Also, operators in sub- or superscripts should not be spaced
2263- if (self ._in_subscript_or_superscript or (
2263+ if (self ._needs_space_after_subsuper or (
22642264 c in self ._binary_operators and (
22652265 len (s [:loc ].split ()) == 0 or prev_char in {
22662266 '{' , * self ._left_delims , * self ._relation_symbols }))):
@@ -2366,13 +2366,9 @@ def operatorname(self, s: str, loc: int, toks: ParseResults) -> T.Any:
23662366 # Add thin space except when followed by parenthesis, bracket, etc.
23672367 hlist_list += [self ._make_space (self ._space_widths [r'\,' ])]
23682368 self .pop_state ()
2369- # if followed by a super/subscript, set flag to true
2370- # This flag tells subsuper to add space after this operator
2371- if next_char in {'^' , '_' }:
2372- self ._in_subscript_or_superscript = True
2373- else :
2374- self ._in_subscript_or_superscript = False
2375-
2369+ # If followed by a sub/superscript, set flag to true to tell subsuper
2370+ # to add space after this operator.
2371+ self ._needs_space_after_subsuper = next_char in {'^' , '_' }
23762372 return Hlist (hlist_list )
23772373
23782374 def start_group (self , toks : ParseResults ) -> T .Any :
@@ -2482,12 +2478,10 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
24822478 shift = hlist .height + vgap + nucleus .depth
24832479 vlt = Vlist (vlist )
24842480 vlt .shift_amount = shift
2485- result = Hlist ([
2486- vlt ,
2487- * ([self ._make_space (self ._space_widths [r'\,' ])]
2488- if self ._in_subscript_or_superscript else []),
2489- ])
2490- self ._in_subscript_or_superscript = False
2481+ optional_spacing = ([self ._make_space (self ._space_widths [r'\,' ])]
2482+ if self ._needs_space_after_subsuper else [])
2483+ self ._needs_space_after_subsuper = False
2484+ result = Hlist ([vlt , * optional_spacing ])
24912485 return [result ]
24922486
24932487 # We remove kerning on the last character for consistency (otherwise
@@ -2579,12 +2573,10 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
25792573
25802574 # Do we need to add a space after the nucleus?
25812575 # To find out, check the flag set by operatorname
2582- spaced_nucleus : list [Node ] = [nucleus , x ]
2583- if self ._in_subscript_or_superscript :
2584- spaced_nucleus += [self ._make_space (self ._space_widths [r'\,' ])]
2585- self ._in_subscript_or_superscript = False
2586-
2587- result = Hlist (spaced_nucleus )
2576+ optional_spacing = ([self ._make_space (self ._space_widths [r'\,' ])]
2577+ if self ._needs_space_after_subsuper else [])
2578+ self ._needs_space_after_subsuper = False
2579+ result = Hlist ([nucleus , x , * optional_spacing ])
25882580 return [result ]
25892581
25902582 def _genfrac (self , ldelim : str , rdelim : str , rule : float | None , style : _MathStyle ,
0 commit comments