manim
manim copied to clipboard
Colors & Regex support for MathTex MObjects
Fixes https://github.com/ManimCommunity/manim/issues/2884
Overview: What does this pull request change?
- New
ColoredSingleStringMathTexclass uses latexxcolorpackage to set default colors of a TeX expression. It can take both atex_to_tex_color_map(String->Color) and aregex_to_tex_color_map(Regex->Color) map. - MathTex is set to derive from
ColoredSingleStringMathTexand has two new optional arguments with the same name for this same usage. - MathTex has two new arguments
regexes_to_isolateandregex_to_color_mapwhich acts exactly likesubstrings_to_isolateandtex_to_color_mapbut 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