Mathematical Equations – manim Series: Part 5

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

Note:  This tutorial series was created using Python 2.7 and the May 9, 2018 commit of manim, which can be found here.  I hope to create posts on the updated version sometime in the future.

5.0 Mathematical Equations

A math animation package wouldn’t be much use if you couldn’t include nice looking equations. The best way I know of to typeset equations is using LaTeX (\LaTeX) , which manim makes use of. If you’d like to learn more about typesetting with LaTeX I’d recommend the tutorials at ShareLaTeX for a basic intro, but you don’t need to know much about LaTeX to use manim. You can find a list of commonly used symbols here, which is about all you need to know for manim.

Copy and paste the code into a .py file and use extract_scene.py to run the following scene:

class BasicEquations(Scene):
#A short script showing how to use Latex commands
def construct(self):
eq1=TextMobject("$\\vec{X}_0 \\cdot \\vec{Y}_1 = 3$")
eq1.shift(2*UP)
eq2=TexMobject("\\vec{F}_{net} = \\sum_i \\vec{F}_i")
eq2.shift(2*DOWN)

self.play(Write(eq1))
self.play(Write(eq2))

In LaTeX you normally enclose an equation with dollar signs $$ to denote an equation and that works here as well. The main difference is that, due to how manim parses the text, an extra backslash must be included in front of all LaTeX commands. For instance Greek letters can be created in LaTeX by typing out the name of the letter preceded by a backslash; lower case alpha \alpha would be $\alpha$, the angle theta \theta would be $\theta$. In manim, however, a double backslash is needed so \alpha would be $\\alpha$ and \theta would be written as $\\theta$.

You can place a vector arrow over a variable such as \vec{A} using \vec{A} or, since manim requires double backslashes, \\vec{A}. Whatever you place inside the brackets will show up on screen with an arrow over it. Subscripts are denoted by the underscore so \vec{X}_0 would be written as $\vec{X}_0$. If the subscript consists of more than a single character you can enclose the subscript in brackets. Thus \vec{F}_{net} in manim would be $\\vec{F}_{net}$.

It can get tedious having to always include the dollar signs so the TexMobject class assumes all strings are math objects. TEX (\TeX) is the typesetting language that LaTeX is based on so I assume TexMobject is named for TEX. The main difference between TextMobject() and TexMobject is the text math object assumes everything is plain text unless you specify an equation with dollar signs while the Tex math object assumes everything is an equation unless you specify something is plain text using \\text{}.

When mobjects of any sort are created the default position seems to be the center of the screen. Once created you can use shift() or move_to to change the location of the mobjects. For this example above I’ve moved the equations either two MUnits up or two MUnits down (remember that the MUnit or math unit is what I call the measure of length inside manim). Since the screen height is set to a default of 8 MUnits, a 2 MUnit shift corresponds to about a quarter of the screen height.

The Write() method, which is a sublcass of ShowCreation, takes a TextMobject or TexMobject and animates writing the text on the screen. You can also pass a string to Write() and it will create the TextMobject for you. Write() needs to be inside of play() in order to animate it.

5.1 Coloring Equations

class ColoringEquations(Scene):
#Grouping and coloring parts of equations
def construct(self):
line1=TexMobject("\\text{The vector }", "\\vec{F}_{net}", "\\text{ is the net force on object of mass }")
line1.set_color_by_tex("force", BLUE)
line2=TexMobject("m", "\\text{ and acceleration }", "\\vec{a}", ". ")
line2.set_color_by_tex_to_color_map({
"m": YELLOW,
"{a}": RED
})
sentence=VGroup(line1,line2)
sentence.arrange_submobjects(DOWN, buff=MED_LARGE_BUFF)
self.play(Write(sentence))

For this example we have broken our text into blocks of plain text and equations. This allows us to color the equations using either set_color_by_tex() or set_color_by_tex_to_color_map(). The set_color_by_tex() method takes the individual string you want colors and the color as arguments. It looks like you only have to specify part of a string to match but the entire string gets colored. For instance, if we type in line1.set_color_by_tex("F",BLUE), the only place a capital F occurs is in the force variable so the whole net force variable is colored blue. If instead we try line1.set_color_by_tex("net",BLUE), the term net appears in two places in line1 so both the force variable and the string is the net force on object of mass are colored blue. If you want to change the color of multiple elements within a list of texmobjects you can use set_color_by_tex_to_color_map() and a dictionary. The key for the dictionary should be the text we want colored (or a unique part of the string) and the value should be the desired color.

Notice that, since we are using a texmobject and not a textmobject, we have to enclose plain text in the LaTeX command \\text{}. If you don’t do this the text is assumed to be part of an equation so the font and spacing are of the text looks funny. Thus “the net force on object of mass” would look like the net force on object of mass. The equation environment doesn’t recognize spaces between words, uses a different font, and spaces the letters differently than normal text.

By grouping the two lines together with VGroup(), we can use the arrange_submobjects() method to space out the two lines. The first argument is the direction you want the objects spaced out and buff (I assume) is the buffer distance between the mobjects. There are several default buffer distances defined in constants.py but you can also a single number. The smallest default buffer is SMALL_BUFF=0.1 and the largest is LARGE_BUFF=1. Although I didn’t dive into the code, I think the way the buffers work is as a multiplicative factor of one of the main directional vectors (e.g. UP, DOWN, LEFT, RIGHT) so that specifying SMALL_BUFF and LEFT would be equivalent to $0.1*(-1,0,0) = (-0.1,0,0)$.

Things to try:
– Create your own equations using the symbols here.
– Try changing the colors of different parts of the equations

 

In the next post in this series we’ll look at how to align text.

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

5 Responses to Mathematical Equations – manim Series: Part 5

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

  2. Nick Gonzales says:

    This was great!! I can’t thank you enough for this. It goes into just the right amount of detail. If you would do one on graphing I would be forever appreciated. Graphing lines, parabolas, etc would be more useful for my purposes than those really cool coordinate transforms that 31b1 does, but anything to get started. Crossing my fingers; it took some work, but everything in the tutorials so far has been working.

  3. Pingback: Aligning Text – manim Series: Part 6 | Talking Physics

  4. The reason manim requires double backslashes is that in a normal Python string, backslash is an escape character. In order to include a backslash in a string, you have to escape that backslash, meaning you type a double backslash.

    As an alternative to double backslashes, you can also mark a string as raw by putting a lowercase r in front of the opening quotation mark. For example, r”\frac{\vec{A}}{\vec{B}}” is the same as “\frac{\vec{A}}{\vec{B}}” in Python, but perhaps a little easier to type and read.

    • Hah, the double backslashes in my comment didn’t show up :).
      What I meant was:
      r”\frac{\vec{A}}{\vec{B}}” is the same as “\\frac{\\\vec{A}}{\\vec{B}}”

      (There’s no way to preview your reply so I don’t know if this will work!)

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.