Skip to content

Commit 0380a24

Browse files
timhoffmhenrybeUM
andcommitted
Fix spacing after mathtext operators with sub/superscripts
Picks up #22839. Closes #22839, #17852. Co-authored-by: henrybeUM <98666765+henrybeUM@users.noreply.github.com>
1 parent c33557d commit 0380a24

17 files changed

+25
-768
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,9 @@ def __init__(self):
19471947
self._expression = p.main
19481948
self._math_expression = p.math
19491949

1950+
# To add space to nucleus operators after sub/superscripts
1951+
self._in_subscript_or_superscript = False
1952+
19501953
def parse(self, s, fonts_object, fontsize, dpi):
19511954
"""
19521955
Parse expression *s* using the given *fonts_object* for
@@ -1965,6 +1968,8 @@ def parse(self, s, fonts_object, fontsize, dpi):
19651968
" " * (err.column - 1) + "^",
19661969
str(err)])) from err
19671970
self._state_stack = None
1971+
self._in_subscript_or_superscript = False
1972+
# prevent operator spacing from leaking into a new expression
19681973
self._em_width_cache = {}
19691974
self._expression.resetCache()
19701975
return result[0]
@@ -2164,6 +2169,13 @@ def operatorname(self, s, loc, toks):
21642169
# Add thin space except when followed by parenthesis, bracket, etc.
21652170
hlist_list += [self._make_space(self._space_widths[r'\,'])]
21662171
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+
21672179
return Hlist(hlist_list)
21682180

21692181
def start_group(self, s, loc, toks):
@@ -2394,8 +2406,15 @@ def subsuper(self, s, loc, toks):
23942406

23952407
if not self.is_dropsub(last_char):
23962408
x.width += constants.script_space * xHeight
2397-
result = Hlist([nucleus, x])
23982409

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)
23992418
return [result]
24002419

24012420
def _genfrac(self, ldelim, rdelim, rule, style, num, den):
Binary file not shown.
Binary file not shown.

lib/matplotlib/tests/baseline_images/test_mathtext/mathtext_cm_30.svg

Lines changed: 0 additions & 188 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.

lib/matplotlib/tests/baseline_images/test_mathtext/mathtext_dejavusans_30.svg

Lines changed: 0 additions & 124 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)