Skip to content

Conversation

BinaryQuantumSoul
Copy link

Fixes #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

self.regex_to_tex_color_map = regex_to_tex_color_map or {}
super().__init__(
tex_string=self._make_colored_string(s),
tex_template=kw.pop("tex_template", config["tex_template"]).add_to_preamble(r"\usepackage{xcolor}"),

Check failure

Code scanning / CodeQL

Modification of parameter with default Error

This expression mutates a
default value
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

Tex splitting issue with \frac numerator
1 participant