Skip to content

Commit 81a560c

Browse files
authored
Merge pull request #17890 from mpetroff/fix-mathtext-operator-spacing
Properly use thin space after math text operator
2 parents cea02d3 + da01cf8 commit 81a560c

17 files changed

+47
-718
lines changed

lib/matplotlib/mathtext.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from pyparsing import (
2929
Combine, Empty, FollowedBy, Forward, Group, Literal, oneOf, OneOrMore,
3030
Optional, ParseBaseException, ParseFatalException, ParserElement,
31-
QuotedString, Regex, StringEnd, Suppress, ZeroOrMore)
31+
ParseResults, QuotedString, Regex, StringEnd, Suppress, ZeroOrMore)
3232

3333
from matplotlib import cbook, colors as mcolors, rcParams
3434
from matplotlib.afm import AFM
@@ -2873,25 +2873,37 @@ def accent(self, s, loc, toks):
28732873
])
28742874

28752875
def function(self, s, loc, toks):
2876-
self.push_state()
2877-
state = self.get_state()
2878-
state.font = 'rm'
2879-
hlist = Hlist([Char(c, state) for c in toks[0]])
2880-
self.pop_state()
2876+
hlist = self.operatorname(s, loc, toks)
28812877
hlist.function_name = toks[0]
28822878
return hlist
28832879

28842880
def operatorname(self, s, loc, toks):
28852881
self.push_state()
28862882
state = self.get_state()
28872883
state.font = 'rm'
2884+
hlist_list = []
28882885
# Change the font of Chars, but leave Kerns alone
28892886
for c in toks[0]:
28902887
if isinstance(c, Char):
28912888
c.font = 'rm'
28922889
c._update_metrics()
2890+
hlist_list.append(c)
2891+
elif isinstance(c, str):
2892+
hlist_list.append(Char(c, state))
2893+
else:
2894+
hlist_list.append(c)
2895+
next_char_loc = loc + len(toks[0]) + 1
2896+
if isinstance(toks[0], ParseResults):
2897+
next_char_loc += len('operatorname{}')
2898+
next_char = next((c for c in s[next_char_loc:] if c != ' '), '')
2899+
delimiters = self._left_delim | self._ambi_delim | self._right_delim
2900+
delimiters |= {'^', '_'}
2901+
if (next_char not in delimiters and
2902+
toks[0] not in self._overunder_functions):
2903+
# Add thin space except when followed by parenthesis, bracket, etc.
2904+
hlist_list += [self._make_space(self._space_widths[r'\,'])]
28932905
self.pop_state()
2894-
return Hlist(toks[0])
2906+
return Hlist(hlist_list)
28952907

28962908
def start_group(self, s, loc, toks):
28972909
self.push_state()
Binary file not shown.
Binary file not shown.

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

-202
This file was deleted.
Binary file not shown.
Binary file not shown.

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

-115
This file was deleted.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)