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

Skip to content

Fix mathtext spacing of operators at expression boundaries - #32331

Open
ahmed5145 wants to merge 3 commits into
matplotlib:mainfrom
ahmed5145:fix-relation-operator-spacing
Open

Fix mathtext spacing of operators at expression boundaries#32331
ahmed5145 wants to merge 3 commits into
matplotlib:mainfrom
ahmed5145:fix-relation-operator-spacing

Conversation

@ahmed5145

@ahmed5145 ahmed5145 commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

PR summary

Closes #23315.

Relation, binary and arrow operators are "spaced symbols", mathtext puts a space on both sides of them. But TeX drops the glue at the boundaries of a math list, so an operator at the very start or end of an expression (or of a {...} group) shouldn't be spaced on that side. For example, in a$=b$ there's no space before the =, only after it. mathtext spaced both sides, and only binary operators were special-cased, and only at the start of the string.

I now decide the left and right spacing of each spaced symbol independently, and drop the space on any side that touches the start/end of the expression or an opening/closing delimiter. I also fixed the next-character lookahead so it skips past the whole source token (e.g. a multi-character command like \doteq) instead of just loc + 1, which is what made named relations at the end still get a stray space.

This lets me restore the simpler \ddots test that previously needed a negative \hspace workaround, and I added a check_figures_equal regression test.

Two existing image tests change, and I've regenerated their baselines (built locally with matplotlib's bundled FreeType, the same one the image tests use):

  • test_mathtext_rendering index 71 ($\Cap$ $\Cup$ $\leftharpoonup$ $\barwedge$ $\rightharpoonup$): the harpoons, alone in $...$, no longer get edge spacing.
  • test_mathtext_rendering_lightweight index 1: the \rightarrow/\leftarrow inside \overset/\underset no longer get edge spacing.

Both are correct changes. I verified index 72 (the \ddots test) renders pixel-identically to its existing baseline across all fontsets, so it does not need regeneration.

AI Disclosure

I used Claude Code to explore and navigate the mathtext parser and scaffold the regression test. I decided on the approach, edited the code and made the changes. Then, I checked the result against TeX's spacing: compared rendered output before and after the change and confirmed the restored \ddots test matches its existing baseline.

  • Use an expressive title
  • New and changed code is tested: a new check_figures_equal test and the restored \ddots test, plus regenerated baselines for the two image tests whose rendering changes.
  • N/A: no new plotting feature to demonstrate in an example.
  • Behavior change documented in doc/api/next_api_changes/behavior/23315-AM.rst.
  • N/A: no other documentation changes needed.

Relation, binary and arrow operators are "spaced symbols": mathtext puts
a space on each side of them.  TeX, however, discards the glue at the
boundaries of a math list, so an operator at the very start or end of an
expression (or of a {...} group) is not spaced on that side -- e.g. in
"a$=b$" there is no space before the "=", only after it.  mathtext
instead spaced both sides, and only binary operators were special-cased
at the start of the string.

Decide the left and right spacing of a spaced symbol independently, and
drop the space on any side that abuts the start/end of the expression or
an opening/closing delimiter.  The lookahead for the next character now
skips over the source token (which may be a multi-character command such
as "\doteq") instead of just loc + 1.

This restores the simpler \ddots test that had to be worked around with
negative \hspace, and adds a check_figures_equal regression test.

Closes matplotlib#23315
@ahmed5145
ahmed5145 force-pushed the fix-relation-operator-spacing branch from da76485 to 9a143d7 Compare September 10, 2026 18:13
Document the behavior change from matplotlib#23315: spaced operators (relation,
binary, arrow) are no longer spaced on a side that abuts the start or
end of a math expression.
Regenerate the baseline images that change now that spaced operators are
not padded at the boundaries of an expression: index 71 of
test_mathtext_rendering (a lone \leftharpoonup / \rightharpoonup) and
index 1 of test_mathtext_rendering_lightweight (\rightarrow / \leftarrow
inside \overset / \underset).  PNGs are optimized with oxipng.
@ahmed5145
ahmed5145 force-pushed the fix-relation-operator-spacing branch from 54942d2 to 6abfb08 Compare September 11, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relation operator in mathtext should not be spaced when at end

1 participant