Skip to content

Fix nested braces in mathtext \text arguments - #32147

Open
ahmed5145 wants to merge 1 commit into
matplotlib:mainfrom
ahmed5145:fix-text-nested-braces
Open

Fix nested braces in mathtext \text arguments#32147
ahmed5145 wants to merge 1 commit into
matplotlib:mainfrom
ahmed5145:fix-text-nested-braces

Conversation

@ahmed5145

Copy link
Copy Markdown

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
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
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?

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

2 participants