LaTeX in Manim
Manim uses LaTeX to render mathematical expressions and formatted text.
MathTex vs Tex
- MathTex: Automatically wraps content in math mode (
align*environment) - Tex: Raw LaTeX - you control the mode
from manim import *
class LaTeXComparison(Scene):
def construct(self):
# MathTex - auto math mode
math = MathTex(r"E = mc^2")
# Tex - need explicit math delimiters
tex = Tex(r"$E = mc^2$")
# Both render the same
VGroup(math, tex).arrange(DOWN)
self.add(math, tex)Basic MathTex
class MathTexExample(Scene):
def construct(self):
# Simple equation
eq1 = MathTex(r"x^2 + y^2 = z^2")
# Fractions
eq2 = MathTex(r"\frac{a}{b}")
# Square roots
eq3 = MathTex(r"\sqrt{2}")
# Greek letters
eq4 = MathTex(r"\alpha + \beta = \gamma")
# Integrals
eq5 = MathTex(r"\int_0^\infty e^{-x} dx")
# Summations
eq6 = MathTex(r"\sum_{n=1}^{\infty} \frac{1}{n^2}")
equations = VGroup(eq1, eq2, eq3, eq4, eq5, eq6).arrange_in_grid(2, 3)
self.add(equations)Coloring Parts of Equations
Using set_color_by_tex
class ColoredEquation(Scene):
def construct(self):
eq = MathTex(r"e^{i\pi} + 1 = 0")
eq.set_color_by_tex("e", RED)
eq.set_color_by_tex(r"\pi", BLUE)
eq.set_color_by_tex("i", GREEN)
self.add(eq)Using substrings_to_isolate
For precise coloring, isolate substrings first:
class IsolatedColoring(Scene):
def construct(self):
eq = MathTex(
r"e^x = x^0 + x^1 + \frac{1}{2}x^2 + \cdots",
substrings_to_isolate=["x"]
)
eq.set_color_by_tex("x", YELLOW)
self.add(eq)Using index_labels for debugging
class DebugLabels(Scene):
def construct(self):
eq = MathTex(r"\frac{a}{b}")
# Add index labels to see which index is which part
self.add(index_labels(eq[0]))
self.add(eq)Direct indexing
eq = MathTex(r"a + b = c")
eq[0][0].set_color(RED) # 'a'
eq[0][2].set_color(BLUE) # 'b'
eq[0][4].set_color(GREEN) # 'c'Multi-part Equations
Split equations into parts for individual control:
class MultiPartEquation(Scene):
def construct(self):
eq = MathTex("a", "^2", "+", "b", "^2", "=", "c", "^2")
eq[0].set_color(RED) # a
eq[3].set_color(BLUE) # b
eq[6].set_color(GREEN) # c
self.play(Write(eq))Text with Math (Tex)
class MixedContent(Scene):
def construct(self):
# Mix text and math
tex = Tex(r"The area is $A = \pi r^2$")
self.play(Write(tex))Custom LaTeX Packages
class CustomPackage(Scene):
def construct(self):
template = TexTemplate()
template.add_to_preamble(r"\usepackage{mathrsfs}")
eq = Tex(
r"$\mathscr{L}$",
tex_template=template
)
self.add(eq)Equation Alignment
class AlignedEquations(Scene):
def construct(self):
eqs = MathTex(
r"a &= b + c \\",
r"d &= e + f + g \\",
r"h &= i"
)
self.add(eqs)Common LaTeX Symbols
# Greek letters
MathTex(r"\alpha \beta \gamma \delta \epsilon")
MathTex(r"\Gamma \Delta \Theta \Lambda \Pi")
# Operators
MathTex(r"\times \div \pm \mp \cdot")
# Relations
MathTex(r"\leq \geq \neq \approx \equiv")
# Arrows
MathTex(r"\rightarrow \leftarrow \Rightarrow \Leftrightarrow")
# Sets
MathTex(r"\in \notin \subset \supset \cup \cap")
# Calculus
MathTex(r"\int \iint \oint \partial \nabla")Font Size
# Using font_size parameter
eq = MathTex(r"E = mc^2", font_size=72)
# Using scale
eq = MathTex(r"E = mc^2").scale(2)Best Practices
- Use raw strings - Always use
r"..."for LaTeX - Use MathTex for pure math - Simpler than adding
$...$ - Use Tex for mixed content - When combining text and math
- Split for animation control - Separate parts you'll animate differently
- Use substrings_to_isolate - For reliable coloring of repeated elements