Skip to content

Fix nested braces in mathtext \text arguments - #32147

Merged
QuLogic merged 3 commits into
matplotlib:mainfrom
ahmed5145:fix-text-nested-braces
Aug 21, 2026
Merged

Fix nested braces in mathtext \text arguments#32147
QuLogic merged 3 commits into
matplotlib:mainfrom
ahmed5145:fix-text-nested-braces

Conversation

@ahmed5145

Copy link
Copy Markdown
Contributor

PR summary

Closes #32105.

\text used pyparsing's QuotedString, which ends at the first }, so that made \text{{example}} failed with a cryptic ParseException.

I replaced that with a small brace-balanced token that tracks nesting depth.

Nested unescaped braces are treated as TeX groups (not rendered), matching real LaTeX, so \text{{example}} renders the same as \text{example}. Literal braces continue to use \{ and \}.

AI Disclosure

I used Cursor (generative AI) to explore the mathtext parser, draft the regression tests, and help write this PR description (to make sure I am following standards). I reviewed the change, verified the behavior against TeX grouping semantics, and ran a local smoke test covering nested braces, escaped braces, the existing \text test string, and malformed input.

PR quality check

  • Use an expressive title
  • New and changed code is tested
  • Plotting related features are demonstrated in an example
  • New features and API changes have release notes
  • Documentation complies with guidelines

N/A for example/release notes/docs: this is a bug fix restoring expected LaTeX-like behavior of an existing command.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@anntzer

anntzer commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

pyparsing has the builtin nested_expr; could that not be used instead of building our own machinery?

@ahmed5145

ahmed5145 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

@anntzer Good suggestion. I looked at this. nested_expr works well for \substack, where the body is recursively parsed tokens and a nested list is the natural result.

For \text, the argument is a brace-balanced literal string: nested braces are TeX groups (not rendered), {/} are literal braces, and interior whitespace must be preserved.

nested_expr returns nested lists and, by default, skips whitespace between chunks, so we’d still need custom content, escape handling, and a flatten/unescape parse action. That ends up about as much machinery as _BracedText, with more risk around spaces next to escapes.

Happy to switch if you’d still prefer the nested_expr shape for consistency. Please lmk!

@ahmed5145

Copy link
Copy Markdown
Contributor Author

Friendly bump. Circling back on the nested_expr question from Aug 2. Happy to refactor if you’d still prefer that for consistency; otherwise I think the current _BracedText approach is ready for another look. Thanks!

@anntzer

anntzer commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

I am a bit confused why only \text needs this handling. In theory mathtext strings can be full of balanced braces a bit everywhere, which are no-ops (unless preceded by a command), so why is handling of balanced braces specifically different here?

@ahmed5145

Copy link
Copy Markdown
Contributor Author

Good question. They aren't, except for \text.

In math mode, nested braces already go through p.group / optional_group / required_group, so they're no-ops as you describe. That's also why \mathrm{{example}} works, \mathrm is a font-prefixed group, not a delimited string.

\text never used that path. Its argument is a text-mode literal (keep spaces, don't treat _/^ as sub/super, \{/\} for actual braces), which is why it was a QuotedString to begin with. That matcher just happens to end at the first }.

Reusing optional_group would get nested braces for free, but it would also parse the body as math, which isn't what \text does. _BracedText is only replacing that quoted-string matcher, not adding a second grouping system.

A recursive text-mode group would be more consistent long-term, but it seemed out of scope for this bug.

@anntzer anntzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still not entirely convinced that we need to roll out our own hand-made solution, but I guess this works.

@ahmed5145

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Agreed it's a bit of custom parsing; I went this way to keep \text as a quoted literal. Happy to revisit if we want it on the recursive-group path later. We can also open a follow-up issue to investigate through it.

Comment thread lib/matplotlib/_mathtext.py Outdated
@QuLogic

QuLogic commented Aug 20, 2026

Copy link
Copy Markdown
Member

Looking at the reduced coverage, I guess you should also add some invalid test cases.

@QuLogic
QuLogic merged commit b379c1b into matplotlib:main Aug 21, 2026
39 of 41 checks passed
@QuLogic QuLogic modified the milestones: v3.12.0, v3.11.2 Aug 21, 2026
@QuLogic

QuLogic commented Aug 21, 2026

Copy link
Copy Markdown
Member

@meeseeksdev backport to v3.11.x

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.

[Bug]: Cryptic Mathtext ParseException on \text with double curly braces

3 participants