Skip to content

Commit 4a99a83

Browse files
anntzerQuLogic
authored andcommitted
Fix spacing in r"$\max f$".
Previously, in a mathtext string like `r"$\sin x$"`, a thin space would (correctly) be added between "sin" and "x", but that space would be missing in expressions like `r"$\max f$"`. The difference arose because of the slightly different handling of subscripts and superscripts after the `\sin` and `\max` operators: `\sin^n` puts the superscript as a normal exponent, but `\max_x` puts the subscript centered below the operator name ("overunder symbol). The previous code for inserting the thin space did not handle the "overunder" case; fix that. The new behavior is tested by the change in test_operator_space, as well as by mathtext1_dejavusans_06. The change in mathtext_foo_29 arises because the extra thin space now inserted after `\limsup` slightly shifts the centering of the whole string. Ideally that thin space should be suppressed if there's no token after the operator, but that's not something currently implemented either for e.g. `\sin` (compare e.g. the right-alignments in `text(.5, .9, r"$\sin$", ha="right"); text(.5, .8, r"$\mathrm{sin}$", ha="right"); axvline(.5)` where the extra thin space after `\sin` is visible), so this patch just makes things more consistent.
1 parent 707f384 commit 4a99a83

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,8 +2362,7 @@ def operatorname(self, s: str, loc: int, toks: ParseResults) -> T.Any:
23622362
next_char_loc += len('operatorname{}')
23632363
next_char = next((c for c in s[next_char_loc:] if c != ' '), '')
23642364
delimiters = self._delims | {'^', '_'}
2365-
if (next_char not in delimiters and
2366-
name not in self._overunder_functions):
2365+
if next_char not in delimiters:
23672366
# Add thin space except when followed by parenthesis, bracket, etc.
23682367
hlist_list += [self._make_space(self._space_widths[r'\,'])]
23692368
self.pop_state()
@@ -2483,7 +2482,12 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
24832482
shift = hlist.height + vgap + nucleus.depth
24842483
vlt = Vlist(vlist)
24852484
vlt.shift_amount = shift
2486-
result = Hlist([vlt])
2485+
result = Hlist([
2486+
vlt,
2487+
*([self._make_space(self._space_widths[r'\,'])]
2488+
if self._in_subscript_or_superscript else []),
2489+
])
2490+
self._in_subscript_or_superscript = False
24872491
return [result]
24882492

24892493
# We remove kerning on the last character for consistency (otherwise

lib/matplotlib/tests/test_mathtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def test_operator_space(fig_test, fig_ref):
400400
fig_test.text(0.1, 0.6, r"$\operatorname{op}[6]$")
401401
fig_test.text(0.1, 0.7, r"$\cos^2$")
402402
fig_test.text(0.1, 0.8, r"$\log_2$")
403-
fig_test.text(0.1, 0.9, r"$\sin^2 \cos$") # GitHub issue #17852
403+
fig_test.text(0.1, 0.9, r"$\sin^2 \max \cos$") # GitHub issue #17852
404404

405405
fig_ref.text(0.1, 0.1, r"$\mathrm{log\,}6$")
406406
fig_ref.text(0.1, 0.2, r"$\mathrm{log}(6)$")
@@ -410,7 +410,7 @@ def test_operator_space(fig_test, fig_ref):
410410
fig_ref.text(0.1, 0.6, r"$\mathrm{op}[6]$")
411411
fig_ref.text(0.1, 0.7, r"$\mathrm{cos}^2$")
412412
fig_ref.text(0.1, 0.8, r"$\mathrm{log}_2$")
413-
fig_ref.text(0.1, 0.9, r"$\mathrm{sin}^2 \mathrm{\,cos}$")
413+
fig_ref.text(0.1, 0.9, r"$\mathrm{sin}^2 \mathrm{\,max} \mathrm{\,cos}$")
414414

415415

416416
@check_figures_equal()

0 commit comments

Comments
 (0)