@@ -1882,6 +1882,9 @@ def set_names_and_parse_actions():
18821882 self ._expression = p .main
18831883 self ._math_expression = p .math
18841884
1885+ # To add space to nucleus operators after sub/superscripts
1886+ self ._in_subscript_or_superscript = False
1887+
18851888 def parse (self , s , fonts_object , fontsize , dpi ):
18861889 """
18871890 Parse expression *s* using the given *fonts_object* for
@@ -1900,6 +1903,8 @@ def parse(self, s, fonts_object, fontsize, dpi):
19001903 " " * (err .column - 1 ) + "^" ,
19011904 str (err )])) from err
19021905 self ._state_stack = None
1906+ self ._in_subscript_or_superscript = False
1907+ # prevent operator spacing from leaking into a new expression
19031908 self ._em_width_cache = {}
19041909 self ._expression .resetCache ()
19051910 return result [0 ]
@@ -2108,6 +2113,13 @@ def operatorname(self, s, loc, toks):
21082113 # Add thin space except when followed by parenthesis, bracket, etc.
21092114 hlist_list += [self ._make_space (self ._space_widths [r'\,' ])]
21102115 self .pop_state ()
2116+ # if followed by a super/subscript, set flag to true
2117+ # This flag tells subsuper to add space after this operator
2118+ if next_char in {'^' , '_' }:
2119+ self ._in_subscript_or_superscript = True
2120+ else :
2121+ self ._in_subscript_or_superscript = False
2122+
21112123 return Hlist (hlist_list )
21122124
21132125 def start_group (self , s , loc , toks ):
@@ -2305,8 +2317,15 @@ def subsuper(self, s, loc, toks):
23052317
23062318 if not self .is_dropsub (last_char ):
23072319 x .width += constants .script_space * xHeight
2308- result = Hlist ([nucleus , x ])
23092320
2321+ # Do we need to add a space after the nucleus?
2322+ # To find out, check the flag set by operatorname
2323+ spaced_nucleus = [nucleus , x ]
2324+ if self ._in_subscript_or_superscript :
2325+ spaced_nucleus += [self ._make_space (self ._space_widths [r'\,' ])]
2326+ self ._in_subscript_or_superscript = False
2327+
2328+ result = Hlist (spaced_nucleus )
23102329 return [result ]
23112330
23122331 def _genfrac (self , ldelim , rdelim , rule , style , num , den ):
0 commit comments