Aligning Text – manim Series: Part 6

You can find the previous post in this series here and the overview of the entire series here.

Important Note:  These posts are based on an earlier version of manim which uses Python 2.7.  The latest version of manim is using Python 3.  To follow along with these posts, use Python 2.7 and the May 9, 2018 commit of manim .

6.0 Aligning Text and Using Braces

This post will show how to use braces to visually group equations or text together but also how to align text elements. We will first write a program to align elements of two equations but in a somewhat clunky fashion; this is not the most elegant way to accomplish this task. After looking at this first version we will rewrite the code in a more concise fashion that lines everything up even better.

Copy and paste the following code and run it.

class UsingBraces(Scene):
#Using braces to group text together
def construct(self):
eq1A = TextMobject("4x + 3y")
eq1B = TextMobject("=")
eq1C = TextMobject("0")
eq2A = TextMobject("5x -2y")
eq2B = TextMobject("=")
eq2C = TextMobject("3")

eq_text = braces.get_text("A pair of equations")

self.add(eq1A, eq1B, eq1C)
self.add(eq2A, eq2B, eq2C),Write(eq_text))

To line up parts of the equations on screen we use next_to() and align_to(). For this example we’ve broken the equation into smaller parts and then used next_to() to place the subparts of each equation next to each other and then align_to() to line up the left side of each part of the equation. You can also use UP, DOWN, and RIGHT to align different edges of the mobjects. We’ve also added a brace to show how to visually group a set of equations. In order to use the braces we must use VGroup() to combine the equations. When we instantiate the braces the first argument is the group and the second argument is where the braces are located relative to the grouping. You can set the text next to the braces using get_text(). This method does not draw the text on the screen, it is only used to set the location of the text relative to the braces so you will still need to add the text to the screen.

class UsingBracesConcise(Scene):
#A more concise block of code with all columns aligned
def construct(self):
for i,item in enumerate(eq2_mob):
eq_text = braces.get_text("A pair of equations"),Write(eq2)),Write(eq_text))

Here is a more concise version of the previous code. Each equation is written out as a list with each part of the equation as a separate string. This allows more control over the vertical alignment of the parts of the two equations. Inside the for loop we use align_to() to line up the left edge of the elements in eq1 and eq2.

Notice that when creating the texmobjects that we passed the variable name of the list with an asterisk in front of it eq1_mob=TexMobject(*eq1_text). The asterisk is a Python command to unpack the list and treat the argument as a comma-separated list. Thus eq1_mob=TexMobject(*eq1_text) is identical to eq1_mob=TexMobject("4","x","+","3","y","=","0").

Things to try:
– Arrange the equations on the screen
– Add some shapes around your equations.


Next up: Plotting graphs in manim

This entry was posted in Just for Fun, Programming and tagged , , , . Bookmark the permalink.

6 Responses to Aligning Text – manim Series: Part 6

  1. Pingback: Mathematical Equations – manim Series: Part 5 | Talking Physics

  2. Pingback: Graphing Functions – manim Series: Part 7 | Talking Physics

  3. Pingback: Learning How To Animate Videos Using manim Series – A Journey | Talking Physics

  4. Alkspin says:

    Slight suggestion:


    can be reduced to


  5. Suppose I have a few equations like “x + 2 = 12 – x”, “2x = 10”, and “x=5”.
    Is there a good way to align the equal signs in these equations, but still have separate TexMObjects for each of them?

    • For what it’s worth, this is what I went with:

      equation0 = TextMobject("x + 3", "=", "{-12}")
      equation1 = TextMobject("{-3}", "=", "{-3}").next_to(equation0, DOWN)
      equation2 = TextMobject("x + 0", "=", "{-15}").next_to(equation1, DOWN)
      equation3 = TextMobject("x", "=", "{-15}").next_to(equation2, 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)
      eq3 = equation3.get_part_by_tex('=').align_to(eq2, LEFT)
      equation0.get_part_by_tex('x + 3').next_to(eq0, LEFT)
      equation0.get_part_by_tex('{-12}').next_to(eq0, RIGHT)
      equation1.get_part_by_tex('{-3}').next_to(eq1, LEFT)
      equation1.get_parts_by_tex('{-3}')[-1].next_to(eq1, RIGHT)
      equation2.get_part_by_tex('x + 0').next_to(eq2, LEFT)
      equation2.get_part_by_tex('{-15}').next_to(eq2, RIGHT)
      equation3.get_part_by_tex('x').next_to(eq3, LEFT)
      equation3.get_part_by_tex('{-15}').next_to(eq3, RIGHT)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.