Manim - 等式中的对齐项

Manim - Aligning terms in equation

我有一个简单的 manim 场景可以求解方程 2x-3=-7。我想在每个步骤之间保持 2x 垂直对齐(因为等号已对齐)。这能做到吗?

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))

这不是最好的解决方案,但它确实有效...

from manimlib.imports import *

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)

        equation1.get_part_by_tex('2x').move_to((1.15*LEFT)+(0.6*DOWN))
        equation2.get_part_by_tex('x').move_to((1.05*LEFT)+(1.2*DOWN))

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

希望对您有所帮助!

这是我最终的结果。

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])

如果您使用的是社区版,还有另一种更简单的方法(使用 align function):

class BasicEquationsSimple(Scene):
    def construct(self):
        eqns = MathTex(r"2x - 3 & = -7 \ 2x & = -4 \ x & = -2")
        self.play(Write(eqns))

也许这也适用于 Cairo 和 OpenGL 版本(将 MathTex 替换为 TexMobject)...