*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") eq1B.next_to(eq1A,RIGHT) eq1C.next_to(eq1B,RIGHT) eq2A.shift(DOWN) eq2B.shift(DOWN) eq2C.shift(DOWN) eq2A.align_to(eq1A,LEFT) eq2B.align_to(eq1B,LEFT) eq2C.align_to(eq1C,LEFT) eq_group=VGroup(eq1A,eq2A) braces=Brace(eq_group,LEFT) eq_text = braces.get_text("A pair of equations") self.add(eq1A, eq1B, eq1C) self.add(eq2A, eq2B, eq2C) self.play(GrowFromCenter(braces),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): eq1_text=["4","x","+","3","y","=","0"] eq2_text=["5","x","-","2","y","=","3"] eq1_mob=TexMobject(*eq1_text) eq2_mob=TexMobject(*eq2_text) eq1_mob.set_color_by_tex_to_color_map({ "x":RED_B, "y":GREEN_C }) eq2_mob.set_color_by_tex_to_color_map({ "x":RED_B, "y":GREEN_C }) for i,item in enumerate(eq2_mob): item.align_to(eq1_mob[i],LEFT) eq1=VGroup(*eq1_mob) eq2=VGroup(*eq2_mob) eq2.shift(DOWN) eq_group=VGroup(eq1,eq2) braces=Brace(eq_group,LEFT) eq_text = braces.get_text("A pair of equations") self.play(Write(eq1),Write(eq2)) self.play(GrowFromCenter(braces),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

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

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

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

Slight suggestion:

eq1_text=[“4″,”x”,”+”,”3″,”y”,”=”,”0″]

eq1_mob=TexMobject(*eq1_text)

can be reduced to

eq1_text=”4x+3y=0″

eq1_mob=TexMobject(*eq1_text)

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: