Note 2 for Manim Project
Lesson 2 - Text
Table of Contents
Recap
In our previous lesson, we covered how to install and create a basic animation using geometric shapes in Manim.
However, our animation is missing something very important… text!
In this lesson, we’ll answer:
- How do we add text in Manim?
- How can we use LaTeX to make equations?
- How do we animate text?
- What are some examples of text being used?
- and much more!
Before starting, make sure you’ve installed Manim and a LaTeX distribution.
Remember, if you’re ever confused about anything in Manim or Python, refer to the Manim documentation, Python documentation, or just Google it!
Now, let’s get started!
LaTeX
Before we start inputting text into Manim, we need to get familiar with the primary way to write digital mathematical statements: .
If you’re already familar with , feel free to skip to Text in Manim. Otherwise, here are the basics:
The Basics
is a markup language used for creating mathematical and scientific documents.
To write equations in , you must first enter math mode. This can be done by surrounding the desired area with a $
on both sides.
1 | This is normal text. |
The above code renders as:
This is normal text.
I can’t write good equations here :(
As you can see, the resulting snippet inside math mode doesn’t look the best. That’s because ’s focus on math extends to regular words too, resulting in every character being interpreted in variable font.
Let’s write some more appropriate text:
1 | 2x + 2y = 2(x+y) |
This is rendered as:
2x + 2y = 2(x+y)
Special Symbols and Functions
That looks so much better! However, it’s nothing that special… yet. has the ability to render special characters, such as , , , , , , , and more.
To use a symbol, type \
and then the symbol’s name in math mode. To use a function, type \
, its name, and any inputs it needs. Here are some examples:
1 | $\pi = 3.141592\ldots$ |
1 | $\sqrt{a + b} \neq \sqrt{a} \cdot \sqrt{b}$ |
1 | $(a+b)^n = \sum^n_{i=0} {n\choose{i}}a^ib^{n-i}$ |
Conclusion
That about wraps it up for the basics. If you’re interested in learning more about , I recommend using Overleaf to live render your code and refer to their online docs.
Anyways, let’s get into Manim…
Text in Manim
In Manim, Text is writing using the Text
, MathText
, and Tex
objects.
Text
The Text
object is used for plain text and cannot use formatting. This is used mainly just to write sentences, such as:
1 | from manim import * |
MathTex
The MathTex
object is used for mathematical equations and uses . This can be used for inputting equations into an animation.
1 | from manim import * |
Tex
The Tex
object is a mix of the Text
and MathTex
objects. Tex
can write both plain text and .
By default,
Tex
writes in plain text.
To use , surround the desired region with$
.
1 | from manim import * |
Important:
All Manim text can be written as chunks instead of one singular string. For example, the below snippets perform the same action:
1 Tex("This is written in chunks ($\LaTeX$ works too!)" )
1 Tex("This is", "written ", "i", "n", " chunks ($\LaTeX$ works too!)" )This is used to modify specific chunks of a Text object.
Modifying Text
Text in Manim can be modified in a lot of ways, but there’s two main ways to do so.
During Initialization
During the creation of a Text object, you can directly pass in arguments for the object’s variables. A Text object’s variables are its individual traits, which includes (but is not limited to):
- Font Size (
font_size
) - Color (
color
) - Fill Opacity (
fill_opacity
) - Slant (
slant
) - Line Spacing (
line_spacing
) - and many many more…
This can be done by passing in more arguments when initializing the object. In other words:
No Modifications | Modifications |
---|---|
Tex("orange") |
Tex("orange", color = ORANGE, font_size = 92, ...) |
After Initialization
The second way to modify text in Manim is by calling methods of the text object. A method is a function that can only be run with a specific type of object. In this case, we’ll be using some special methods of the Text
, Tex
, and MathTex
objects.
Using these methods, you can change any of the variables similar to during initialization, but are also exclusively able to:
- Move Text (
.shift()
) - Scale Text (
.scale()
) - Change Colors of Specific Words (
.set_color_by_tex()
)
Many custom Manim variables and functions take special inputs, such as the vectors
UP
andRIGHT
in.scale(UP, RIGHT)
orYELLOW
incolor = YELLOW
.These special inputs may not be common knowledge, and we recommend you look at some examples and projects to get accustomed to them.
Although methods are used for altering objects after their creation, they are also able to be used during initialization.
The following two snippets produce the same result:
1 | from manim import * |
1 | from manim import * |
Animations
Text
objects in Manim can be animated, like any other Manim object. You’re able to use any general animation with Text, but also the exclusive Write
animation, which looks like this:
For advanced users, you’re able to create your own custom animations. If you’re interested in doing so, check out this article.
Examples
That’s about it for the basics of Text in Manim! I hope you’ve learned a thing or two from this post.
To end off, Manim, like any other new tool, requires lots and lots of practice. So, here’s a few examples of Text being used in Manim:
Positioning Text
1 | from manim import * |
Set Colors
1 | from manim import * |
Set Colors + Transform
1 | from manim import * |
Set Colors Efficiently
1 | from manim import * |
More Resources
- Overleaf - (Online editor)
- Manim - Text & LaTeX Equations - (Informative Video)