manim icon indicating copy to clipboard operation
manim copied to clipboard

Colors & Regex support for MathTex MObjects

Open BinaryQuantumSoul opened this issue 4 months ago • 0 comments

Fixes https://github.com/ManimCommunity/manim/issues/2884

Overview: What does this pull request change?

  • New ColoredSingleStringMathTex class uses latex xcolor package to set default colors of a TeX expression. It can take both a tex_to_tex_color_map (String->Color) and a regex_to_tex_color_map (Regex->Color) map.
  • MathTex is set to derive from ColoredSingleStringMathTex and has two new optional arguments with the same name for this same usage.
  • MathTex has two new arguments regexes_to_isolate and regex_to_color_map which acts exactly like substrings_to_isolate and tex_to_color_map but allow for regex keys.

Motivation and Explanation: Why and how do your changes improve the library?

Right now for all complex TeXexpressions, we have to rely on workarounds like\over` or use character indices to target specific string colors. With this method, only a map is required and can work for all animations, no matter their complexity. For example, this code would never have worked earlier :

class MainScene(Scene):
    def construct(self):
        example = MathTex(
            r'\begin{cases} 3+\frac{x+y^x}{x_{\log(y)}}\\ \binom{34}{x^21}\end{cases}',
            regex_to_tex_color_map={r'[-]?\d+': YELLOW},
            tex_to_tex_color_map={"x": BLUE, "y": RED}
        )
       self.play(Write(example))

Here a regex maps all numbers to yellow.

Links to added or changed documentation pages

TODO ?

Reviewer Checklist

  • [ ] The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • [ ] If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • [ ] If applicable: newly added functions and classes are tested

BinaryQuantumSoul avatar Sep 04 '25 00:09 BinaryQuantumSoul