Search code examples
latexmanim

Manim - Aligning terms in equation


I have a simple manim scene that solves the equation 2x-3=-7. I would like to keep the 2x aligned vertically between each step (as the equals sign has been aligned). Can this be done?

class BasicEquationsSimple(Scene):
    def construct(self):
        equation0 = TexMobject("2x - 3", "=", "{-7}")
        equation1 = TexMobject("2x", "=", "{-4}").next_to(equation0, DOWN)
        equation2 = TexMobject("x", "=", "{-2}").next_to(equation1, DOWN)

        eq0 = equation0.get_part_by_tex('=')
        eq1 = equation1.get_part_by_tex('=').align_to(eq0, LEFT)
        eq2 = equation2.get_part_by_tex('=').align_to(eq1, LEFT)

        equation0.get_part_by_tex('2x - 3').next_to(eq0, LEFT)
        equation0.get_part_by_tex('{-7}').next_to(eq0, RIGHT)
        equation1.get_part_by_tex('2x').next_to(eq1,LEFT)
        equation1.get_part_by_tex('{-4}').next_to(eq1, RIGHT)
        equation2.get_part_by_tex('x').next_to(eq2, LEFT)
        equation2.get_part_by_tex('{-2}').next_to(eq2, RIGHT)

        self.play(Write(equation0,run_time=3))
        self.play(Write(equation1))
        self.play(Write(equation2))

Manim scene


Solution

  • Here's what I ended up going with.

    eq = 
    [
    [TexMobject("{2x}"),TexMobject("{-}"),TexMobject("{4}"),TexMobject("{=}"),TexMobject("{-3}")],
    [TexMobject("{2x}"),"","",TexMobject("{=}"),TexMobject("{1}")],
    [TexMobject("{x}"),"","",TexMobject("{=}"),TexMobject("{1 \\over 2}")]
    ]
    
    lines = len(eq)
    rails = len(eq[1])
    
    for i in range(lines):
       for j in range(rails):
          if(eq[i][j] != ""):
             eq[i][j].move_to([j-0.5*rails+0.5,-i+0.5*lines-0.5,0])