ENH: Parse \limits and \nolimits in mathtext - #32333
Open
ahmed5145 wants to merge 1 commit into
Open
Conversation
Adds support for the \limits and \nolimits commands, which force the sub- and superscripts of the preceding operator to be placed above/below it or to its side, respectively. Previously these commands raised a ParseFatalException (Unknown symbol). Closes matplotlib#28051
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
Closes #28051.
\limitsand\nolimitsare TeX commands that control where an operator's sub- and superscripts go:\limitsplaces them directly above and below the operator,\nolimitsplaces them to the side. mathtext didn't parse them at all, so$\int\limits_a^b$raisedParseFatalException: Unknown symbol: \limits.mathtext already has the over/under layout machinery: it uses it automatically for symbols like
\sumand\prodand for functions like\lim. So the missing piece was really just parsing the commands and letting them steer that existing decision. I added\limits/\nolimitsto thesubsupergrammar rule and made the parse action honor them:\limitsforces over/under placement of the nucleus,\nolimitsforces side placement, and otherwise the default (is_overunder) behavior is unchanged. Both commands also parse when no sub/superscript follows (e.g.$\int\limits$), matching TeX's leniency.Because the over/under path is reused, the visual results line up with the existing defaults:
\sum\limits_a^brenders identically to a plain\sum_a^b, and\int\nolimits_a^brenders identically to a plain\int_a^b. I leaned on that for testing: acheck_figures_equaltest asserts both equalities, so no new baseline images are needed. There's also a parse-only test covering the commands, including the no-subscript case.AI Disclosure
I used Claude Code to navigate the mathtext parser and locate the
subsupergrammar rule and its parse action. I decided on the approach, reuse the existing over/under machinery and let\limits/\nolimitsoverride the placement, and reviewed and directed the edits and tests. My environment can't do a full FreeType build, so instead of running the image-comparison suite I verified behavior by loading the modified parser against an installed matplotlib: I confirmed the issue's example and other cases now parse, that all existing mathtext test expressions still parse (no regression), and that\sum\limits/\int\nolimitsrender pixel-identically to their plain-operator equivalents (which is what the newcheck_figures_equaltest relies on).check_figures_equaltest (test_limits) and a parse test (test_limits_parse). No new baseline images are required because the test compares against the existing default layouts.doc/release/next_whats_new/note.