Skip to content

Commit 42e9b97

Browse files
committed
Properly use thin space after math text operator.
A thin space should follow a math operator except when it is followed by something like a parenthesis or a bracket. This should fix issue #17852.
1 parent ec0132f commit 42e9b97

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/matplotlib/mathtext.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2876,7 +2876,14 @@ def function(self, s, loc, toks):
28762876
self.push_state()
28772877
state = self.get_state()
28782878
state.font = 'rm'
2879-
hlist = Hlist([Char(c, state) for c in toks[0]])
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+
if (next_char not in delimiters and
2883+
toks[0] not in self._overunder_functions):
2884+
# Add thin space except when followed by parenthesis, bracket, etc.
2885+
hlist_list += [self._make_space(self._space_widths[r'\,'])]
2886+
hlist = Hlist(hlist_list)
28802887
self.pop_state()
28812888
hlist.function_name = toks[0]
28822889
return hlist

lib/matplotlib/tests/test_mathtext.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,18 @@ def test_spaces(fig_test, fig_ref):
285285
fig_ref.subplots().set_title(r"$1\/2\:3~4$")
286286

287287

288+
@check_figures_equal(extensions=["png"])
289+
def test_operator_space(fig_test, fig_ref):
290+
fig_test.text(0.1, 0.1, r"$\log 6$")
291+
fig_test.text(0.1, 0.2, r"$\log(6)$")
292+
fig_test.text(0.1, 0.3, r"$\arcsin 6$")
293+
fig_test.text(0.1, 0.4, r"$\arcsin|6|$")
294+
fig_ref.text(0.1, 0.1, r"$\mathrm{log\,}6$")
295+
fig_ref.text(0.1, 0.2, r"$\mathrm{log}(6)$")
296+
fig_ref.text(0.1, 0.3, r"$\mathrm{arcsin\,}6$")
297+
fig_ref.text(0.1, 0.4, r"$\mathrm{arcsin}|6|$")
298+
299+
288300
def test_mathtext_fallback_valid():
289301
for fallback in ['cm', 'stix', 'stixsans', 'None']:
290302
mpl.rcParams['mathtext.fallback'] = fallback

0 commit comments

Comments
 (0)