Search code examples
pythoncolorsjquery-animatemanim

How to change the Color of a MathTex in Manim (animation)?


function14 = MathTex("4", "a", " - ", "2", "b", " + ", "1", "c", " = ", "0", 
font_size=40).move_to([1.5,0,0])

I have this MathTex and i would like to change the color of the 1 item in the Text. It should be an animation where the color changes from white to red. currently it is only white but I dont know how to do color animation. I try to get somthing like this: self.play(MathTex[1].color=RED). Thank you for your time and answers


Solution

  • I also added 3 different ways on how it might be easier for you to write your equations than to put every single character into its own set of quotation marks - it also makes the equations more readable...

    class colorchange(Scene):
        def construct(self):
            function14 = MathTex("4", "a", " - ", "2", "b", " + ", "1", "c", " = ", "0", font_size=40).move_to([1.5,0,0])
            function14a = MathTex(r"4a - 2b + 1c = 0", font_size=40).next_to(function14,DOWN)        
            function14b = MathTex(*r"4a  -  2b  +  1c  =  0".split("  "), font_size=40).next_to(function14a,DOWN)
            function14c = MathTex(*r"4  a  -  2  b  +  1  c  =  0".split("  "), font_size=40).next_to(function14b,DOWN)        
            self.play(Write(function14))
            self.wait()
            self.play(function14[6].animate.set_color(PURE_RED))
    
            self.play(Write(function14a))
            self.wait()
            self.play(function14a[0][6].animate.set_color(PURE_RED))
    
            self.play(Write(function14b))
            self.wait()
            self.play(function14b[4][0].animate.set_color(PURE_RED))
            
            self.play(Write(function14c))
            self.wait()
            self.play(function14c[6].animate.set_color(PURE_RED))
            self.wait()
    
    

    enter image description here

    For more and better help come to the Discord server Where can I find more resources for learning Manim?