Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71adb18

Browse files
committed
Extend operator thin space insertion to \operatorname{}.
1 parent bcc2748 commit 71adb18

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

lib/matplotlib/mathtext.py

Lines changed: 17 additions & 15 deletions
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,33 +2873,35 @@ 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_list = [Char(c, state) for c in toks[0]]
2880-
next_char = next((c for c in s[loc+len(toks[0])+1:] if c != ' '), '')
2881-
delimiters = self._left_delim | self._ambi_delim | self._right_delim
2882-
delimiters |= set(r"^_".split())
2883-
if (next_char not in delimiters and
2884-
toks[0] not in self._overunder_functions):
2885-
# Add thin space except when followed by parenthesis, bracket, etc.
2886-
hlist_list += [self._make_space(self._space_widths[r'\,'])]
2887-
hlist = Hlist(hlist_list)
2888-
self.pop_state()
2876+
hlist = self.operatorname(s, loc, toks)
28892877
hlist.function_name = toks[0]
28902878
return hlist
28912879

28922880
def operatorname(self, s, loc, toks):
28932881
self.push_state()
28942882
state = self.get_state()
28952883
state.font = 'rm'
2884+
hlist_list = []
28962885
# Change the font of Chars, but leave Kerns alone
28972886
for c in toks[0]:
28982887
if isinstance(c, Char):
28992888
c.font = 'rm'
29002889
c._update_metrics()
2890+
hlist_list.append(c)
2891+
else:
2892+
hlist_list.append(Char(c, state))
2893+
next_char_loc = loc + len(toks[0]) + 1
2894+
if isinstance(toks[0], ParseResults):
2895+
next_char_loc += 14 # Skip `operatorname{}`
2896+
next_char = next((c for c in s[next_char_loc:] if c != ' '), '')
2897+
delimiters = self._left_delim | self._ambi_delim | self._right_delim
2898+
delimiters |= set(r"^_".split())
2899+
if (next_char not in delimiters and
2900+
toks[0] not in self._overunder_functions):
2901+
# Add thin space except when followed by parenthesis, bracket, etc.
2902+
hlist_list += [self._make_space(self._space_widths[r'\,'])]
29012903
self.pop_state()
2902-
return Hlist(toks[0])
2904+
return Hlist(hlist_list)
29032905

29042906
def start_group(self, s, loc, toks):
29052907
self.push_state()

lib/matplotlib/tests/test_mathtext.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,14 @@ def test_operator_space(fig_test, fig_ref):
291291
fig_test.text(0.1, 0.2, r"$\log(6)$")
292292
fig_test.text(0.1, 0.3, r"$\arcsin 6$")
293293
fig_test.text(0.1, 0.4, r"$\arcsin|6|$")
294+
fig_test.text(0.1, 0.5, r"$\operatorname{op} 6$")
295+
fig_test.text(0.1, 0.6, r"$\operatorname{op}[6]$")
294296
fig_ref.text(0.1, 0.1, r"$\mathrm{log\,}6$")
295297
fig_ref.text(0.1, 0.2, r"$\mathrm{log}(6)$")
296298
fig_ref.text(0.1, 0.3, r"$\mathrm{arcsin\,}6$")
297299
fig_ref.text(0.1, 0.4, r"$\mathrm{arcsin}|6|$")
300+
fig_ref.text(0.1, 0.5, r"$\mathrm{op\,}6$")
301+
fig_ref.text(0.1, 0.6, r"$\mathrm{op}[6]$")
298302

299303

300304
def test_mathtext_fallback_valid():

0 commit comments

Comments
 (0)