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?

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