@@ -1882,6 +1882,9 @@ def set_names_and_parse_actions():
1882
1882
self ._expression = p .main
1883
1883
self ._math_expression = p .math
1884
1884
1885
+ # To add space to nucleus operators after sub/superscripts
1886
+ self ._in_subscript_or_superscript = False
1887
+
1885
1888
def parse (self , s , fonts_object , fontsize , dpi ):
1886
1889
"""
1887
1890
Parse expression *s* using the given *fonts_object* for
@@ -1900,6 +1903,8 @@ def parse(self, s, fonts_object, fontsize, dpi):
1900
1903
" " * (err .column - 1 ) + "^" ,
1901
1904
str (err )])) from err
1902
1905
self ._state_stack = None
1906
+ self ._in_subscript_or_superscript = False
1907
+ # prevent operator spacing from leaking into a new expression
1903
1908
self ._em_width_cache = {}
1904
1909
self ._expression .resetCache ()
1905
1910
return result [0 ]
@@ -2108,6 +2113,13 @@ def operatorname(self, s, loc, toks):
2108
2113
# Add thin space except when followed by parenthesis, bracket, etc.
2109
2114
hlist_list += [self ._make_space (self ._space_widths [r'\,' ])]
2110
2115
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
+
2111
2123
return Hlist (hlist_list )
2112
2124
2113
2125
def start_group (self , s , loc , toks ):
@@ -2305,8 +2317,15 @@ def subsuper(self, s, loc, toks):
2305
2317
2306
2318
if not self .is_dropsub (last_char ):
2307
2319
x .width += constants .script_space * xHeight
2308
- result = Hlist ([nucleus , x ])
2309
2320
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 )
2310
2329
return [result ]
2311
2330
2312
2331
def _genfrac (self , ldelim , rdelim , rule , style , num , den ):
0 commit comments