fix: MathTex double-brace splitting no longer fires on natural LaTeX }}#4602
Merged
behackl merged 3 commits intoManimCommunity:mainfrom Feb 22, 2026
Merged
fix: MathTex double-brace splitting no longer fires on natural LaTeX }}#4602behackl merged 3 commits intoManimCommunity:mainfrom
}}#4602behackl merged 3 commits intoManimCommunity:mainfrom
Conversation
The previous re.split(r'{{|}}', ...) call split on any occurrence of
{{ or }} in the input string, which broke strings whose only }} came
from closing two nested LaTeX brace groups (e.g. ^{\frac{Mq}{M+m}}).
The new _split_double_braces() static method uses a character-level
state machine with three guards:
* Escape priority: \\ is consumed before \{ / \}, so \\}} is
correctly read as an escaped backslash followed by a real }}, not
misinterpreted as \ + \} + lone }.
* Whitespace-gated opener: {{ is only treated as a Manim group opener
at the start of the string or after whitespace. Naturally-occurring
{{ in LaTeX is usually preceded by non-whitespace (e.g. \frac{{{n}}}
or a^{{2}}), so this eliminates the most common false positives.
* Depth-tracking closer: inside a Manim group, }} only closes the
group when the inner brace depth is zero, so {{ a^{b^{c}} }} is
handled correctly and nested LaTeX }} cannot trigger an early close.
Fixes ManimCommunity#4601.
Add a Notes section to the MathTex docstring explaining:
- how {{ }} splits a string into submobjects
- that {{ must be at start-of-string or preceded by whitespace
- that this leaves natural LaTeX like \frac{{{n}}}{k} untouched
- the { { ... } } escape hatch when a split is not wanted
Apply the same explanation to the double-brace paragraph in
docs/source/guides/using_text.rst.
09bfe4f to
755cbcf
Compare
henrikmidtiby
approved these changes
Feb 22, 2026
Contributor
henrikmidtiby
left a comment
There was a problem hiding this comment.
This looks good to me.
The improved parser for handling detection of double brace notation to split the input into smaller chunks is a much more robust approach than the regular expression it replaces.
This was referenced Feb 22, 2026
}}
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.
Fixes #4601.
MathTexsupports a{{ ... }}notation for splitting a single string argument into multiple submobjects. The splitting logic was recently rewritten (in #4515) and inadvertently changed the regex from a paired{{(.*?)}}match tore.split(r"{{|}}", ...), which splits on any{{or}}in isolation. This broke strings whose only}}comes from closing two nested LaTeX brace groups — a common pattern, e.g.^{\frac{Mq}{M+m}}.Fix
_prepare_tex_stringsnow delegates to a new_split_double_bracesstatic method that uses a character-level state machine with three guards:{{is only treated as a Manim group opener when it appears at the start of the string or is immediately preceded by whitespace. Naturally-occurring{{in LaTeX almost always follows non-whitespace (e.g.\frac{{{n}}}{k},a^{{2}}), so this eliminates the most common false positives.}}only closes it when the inner brace depth is zero, so content like{{ a^{b^{c}} }}is handled correctly.\\is consumed as a unit before\{/\}, ensuring e.g.\\}}is read as an escaped backslash followed by a real}}rather than being misread as\+\}+ lone}.The existing workaround mentioned in the error message — inserting a space between the braces (
{{ ... }}→{ { ... } }) — remains valid and correct under the new rules.Changes
manim/mobject/text/tex_mobject.py: replace the regex split with_split_double_braces; add a Notes section to theMathTexdocstring explaining the notation and its rulestests/module/mobject/text/test_texmobject.py: regression test for Incorrect MathTex rendering due to overly eager double brace splitting #4601; 16-case parametrized unit test for_split_double_bracescovering intended notation, false-positive guards, and all four\}}/\\}}/\\\}}/\\\\}}backslash variantsdocs/source/guides/using_text.rst: document the whitespace requirement in the guide