## 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")
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.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

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. Alkspin says:

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)

2. 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)
``````

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