Can I override Manim color defaults for Tex and use xcolor directly?

Viewed 169

I'm experimenting with the Manim math presentation (aka: youtube 3b1b) python system, and find color control to be pretty tricky. Here's an example:


class color1(Scene):
    def construct(self):

        eq1 = MathTex( r'&{\left(\vec{u} \cdot \vec{v} \right)}^2 \\',
                       '&(', r'\vec{u}', r'\cdot', r'\vec{v}', r'){}^2 \\' )
        eq1.set_color_by_tex_to_color_map( { "{u}": RED, "{v}": YELLOW } )

        eq2 = MathTex( r'&{\color{blue}{\left(\vec{u} \cdot \vec{v} \right)}^2} \\' )

        for item in eq1:
           self.play( Write( item ) )

        eq2.shift( 2 * DOWN )
        for item in eq2:
           self.play( Write( item ) )

        self.wait( )

which produces output like:

color example manim

The rules match to a whole string, so if you have two things (like \vec{u} and \vec{v}) that you'd like to match, you can't have them both in one string, and instead seemingly have to separate your equation lines into a set of separate strings so that the color matching rules will work. Also, if I break up one equation line into many different MathTex parameters to color them separately, the rendering in self.play is really really slow (perhaps that can be worked around though.)

I was hoping that I could just use the xcolor syntax to set colors, but that seems to be overriden by Manim's default colors (i.e. that library doesn't use black by default, but white, so I assume that takes precedence.) See the third line in the example above to see how xcolor {color{...}...} didn't work.

Is there a way to avoid setting a default color in Manim latex text, so that xcolor markup will work in that context?

If that is not possible, is there a way to deal with the really slow rendering of self.play when an equation is broken up into many pieces for coloring?

EDIT: Figured out a work around for the slow play issue for separately colored items. That appears to be due to the use of play(Write(...)), and something like this does not have those delays:

        eq3 = MathTex( '(', r'\vec{u}', r'\cdot', r'\vec{v}', r'){}^2' )
        eq3.set_color_by_tex_to_color_map( { "{u}": RED, "{v}": YELLOW } )
       
        for item in eq3:
           self.add( item )
0 Answers
Related