@@ -1947,6 +1947,9 @@ def __init__(self):
1947
1947
self ._expression = p .main
1948
1948
self ._math_expression = p .math
1949
1949
1950
+ # To add space to nucleus operators after sub/superscripts
1951
+ self ._in_subscript_or_superscript = False
1952
+
1950
1953
def parse (self , s , fonts_object , fontsize , dpi ):
1951
1954
"""
1952
1955
Parse expression *s* using the given *fonts_object* for
@@ -1965,6 +1968,8 @@ def parse(self, s, fonts_object, fontsize, dpi):
1965
1968
" " * (err .column - 1 ) + "^" ,
1966
1969
str (err )])) from err
1967
1970
self ._state_stack = None
1971
+ self ._in_subscript_or_superscript = False
1972
+ # prevent operator spacing from leaking into a new expression
1968
1973
self ._em_width_cache = {}
1969
1974
self ._expression .resetCache ()
1970
1975
return result [0 ]
@@ -2164,6 +2169,13 @@ def operatorname(self, s, loc, toks):
2164
2169
# Add thin space except when followed by parenthesis, bracket, etc.
2165
2170
hlist_list += [self ._make_space (self ._space_widths [r'\,' ])]
2166
2171
self .pop_state ()
2172
+ # if followed by a super/subscript, set flag to true
2173
+ # This flag tells subsuper to add space after this operator
2174
+ if next_char in {'^' , '_' }:
2175
+ self ._in_subscript_or_superscript = True
2176
+ else :
2177
+ self ._in_subscript_or_superscript = False
2178
+
2167
2179
return Hlist (hlist_list )
2168
2180
2169
2181
def start_group (self , s , loc , toks ):
@@ -2394,8 +2406,15 @@ def subsuper(self, s, loc, toks):
2394
2406
2395
2407
if not self .is_dropsub (last_char ):
2396
2408
x .width += constants .script_space * xHeight
2397
- result = Hlist ([nucleus , x ])
2398
2409
2410
+ # Do we need to add a space after the nucleus?
2411
+ # To find out, check the flag set by operatorname
2412
+ spaced_nucleus = [nucleus , x ]
2413
+ if self ._in_subscript_or_superscript :
2414
+ spaced_nucleus += [self ._make_space (self ._space_widths [r'\,' ])]
2415
+ self ._in_subscript_or_superscript = False
2416
+
2417
+ result = Hlist (spaced_nucleus )
2399
2418
return [result ]
2400
2419
2401
2420
def _genfrac (self , ldelim , rdelim , rule , style , num , den ):
0 commit comments