Fix nested braces in mathtext \text arguments - #32147
Conversation
|
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. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
|
pyparsing has the builtin nested_expr; could that not be used instead of building our own machinery? |
|
@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! |
|
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! |
|
I am a bit confused why only |
PR summary
Closes #32105.
\textused pyparsing'sQuotedString, 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
\texttest string, and malformed input.PR quality check
N/A for example/release notes/docs: this is a bug fix restoring expected LaTeX-like behavior of an existing command.