Skip to content

fix: MathTex double-brace splitting no longer fires on natural LaTeX }}#4602

Merged
behackl merged 3 commits intoManimCommunity:mainfrom
behackl:fix/brace-splitting
Feb 22, 2026
Merged

fix: MathTex double-brace splitting no longer fires on natural LaTeX }}#4602
behackl merged 3 commits intoManimCommunity:mainfrom
behackl:fix/brace-splitting

Conversation

@behackl
Copy link
Member

@behackl behackl commented Feb 21, 2026

Fixes #4601.

MathTex supports 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 to re.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_strings now delegates to a new _split_double_braces static method that uses a character-level state machine with three guards:

  • Whitespace-gated opener: {{ 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.
  • Depth-tracking closer: once inside a group, }} only closes it when the inner brace depth is zero, so content like {{ a^{b^{c}} }} is handled correctly.
  • Correct escape priority: \\ 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 the MathTex docstring explaining the notation and its rules
  • tests/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_braces covering intended notation, false-positive guards, and all four \}}/\\}}/\\\}}/\\\\}} backslash variants
  • docs/source/guides/using_text.rst: document the whitespace requirement in the guide

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.
@behackl behackl added the pr:bugfix Bug fix for use in PRs solving a specific issue:bug label Feb 21, 2026
Copy link
Contributor

@henrikmidtiby henrikmidtiby left a comment

Choose a reason for hiding this comment

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

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.

@behackl behackl linked an issue Feb 22, 2026 that may be closed by this pull request
@behackl behackl merged commit 000e779 into ManimCommunity:main Feb 22, 2026
15 checks passed
@behackl behackl deleted the fix/brace-splitting branch February 22, 2026 09:48
@behackl behackl changed the title fix: MathTex double-brace splitting no longer fires on natural LaTeX }} fix: MathTex double-brace splitting no longer fires on natural LaTeX }} Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:bugfix Bug fix for use in PRs solving a specific issue:bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Manim 0.20.0 now denies compiling "}}" in latex Incorrect MathTex rendering due to overly eager double brace splitting

2 participants