| name | math-typography |
| description | Mathematical rendering with MathTex, Tex, tex_to_color_map, custom equation classes, and formula animation patterns. |
Math Typography
Mathematical typography patterns for Manim animations.
MathTex vs Tex
MathTex - Mathematical Expressions
eq = MathTex(r"E = mc^2")
eq = MathTex(r"E", r"=", r"m", r"c^2")
Tex - Mixed Text and Math
label = Tex(r"The equation $E = mc^2$ is famous")
label = Tex(r"The value is ", r"$x = 5$")
label[1].set_color(BLUE)
Semantic Coloring
tex_to_color_map
formula = MathTex(
r"P(A \mid B) = P(B \mid A)\, P(A) / P(B)",
tex_to_color_map={
r"A": BLUE,
r"B": ORANGE,
r"P": GREEN,
},
)
⚠️ Coloring inside fractions: a tex_to_color_map key that lands inside
\frac{...}{...} (or any {...}) breaks LaTeX compilation. To color variables
in a fraction, use the multi-argument form and color submobjects directly
(see Manual Coloring below), e.g.
MathTex(r"P(A|B)", r"=", r"\frac{P(B|A)P(A)}{P(B)}").set_color_by_tex("frac", GREEN).
Manual Coloring
eq = MathTex(r"y", r"=", r"m", r"x", r"+", r"b")
eq[0].set_color(BLUE)
eq[2].set_color(RED)
eq[3].set_color(GREEN)
eq[5].set_color(PURPLE)
Color by Index
eq = MathTex(r"f(x) = x^2")
eq[0][0].set_color(BLUE)
eq[0][2].set_color(GREEN)
Split Strategies for Animation
Simple Split
eq = MathTex(r"a", r"+", r"b", r"=", r"c")
self.play(Write(eq[0]))
self.play(Write(eq[1:3]))
self.play(Write(eq[3:]))
Grouped Split
eq = MathTex(r"y = ", r"mx", r" + ", r"b")
self.play(Write(eq[0]))
self.play(Write(eq[1]))
self.play(Write(eq[2:]))
Complex Formula Split
frac = MathTex(
r"\frac{",
r"a + b",
r"}{",
r"c",
r"}"
)
Common LaTeX Patterns
Fractions
MathTex(r"\frac{numerator}{denominator}")
MathTex(r"\frac{1}{2}")
MathTex(r"\frac{x^2 + 1}{x - 1}")
Greek Letters
MathTex(r"\alpha, \beta, \gamma")
MathTex(r"\mu, \sigma, \theta")
MathTex(r"\Delta, \Omega, \Pi")
Subscripts and Superscripts
MathTex(r"x_1, x_2, x_n")
MathTex(r"x^2, x^{10}, x^{n+1}")
MathTex(r"x_i^{(k)}")
Sums and Products
MathTex(r"\sum_{i=1}^{n} x_i")
MathTex(r"\prod_{i=1}^{n} x_i")
MathTex(r"\sum_{x \in S} f(x)")
Integrals
MathTex(r"\int_{a}^{b} f(x) \, dx")
MathTex(r"\int\int_D f(x,y) \, dA")
MathTex(r"\oint_C \vec{F} \cdot d\vec{r}")
Matrices
MathTex(r"\begin{bmatrix} a & b \\ c & d \end{bmatrix}")
MathTex(r"\begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix}")
MathTex(r"\begin{vmatrix} a & b \\ c & d \end{vmatrix}")
Aligned Equations
MathTex(r"""
\begin{aligned}
f(x) &= x^2 + 2x + 1 \\
&= (x + 1)^2
\end{aligned}
""")
Cases
MathTex(r"""
f(x) = \begin{cases}
x^2 & x \geq 0 \\
-x^2 & x < 0
\end{cases}
""")
Custom MathTex Classes
Pattern: Structured Formula
class QuadraticFormula(MathTex):
"""Quadratic formula with accessible parts"""
def __init__(self, **kwargs):
super().__init__(
r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}",
**kwargs
)
self.x = self[0][0]
self.equals = self[0][1]
self.negative_b = self[0][2:4]
self.discriminant = self[0][6:13]
self.denominator = self[0][14:16]
Pattern: Parameterized Formula
class BinomialCoeff(MathTex):
"""Binomial coefficient with custom n and k"""
def __init__(self, n, k, **kwargs):
super().__init__(
r"\binom{" + str(n) + r"}{" + str(k) + r"}",
**kwargs
)
self.n_value = n
self.k_value = k
Pattern: Color-Coded Variables
class RegressionEquation(MathTex):
"""Regression with color-coded coefficients"""
def __init__(self, **kwargs):
super().__init__(
r"y", r"=", r"\beta_0", r"+", r"\beta_1", r"x", r"+", r"\epsilon",
**kwargs
)
self.y = self[0].set_color(BLUE)
self.intercept = self[2].set_color(RED)
self.slope = self[4].set_color(GREEN)
self.x = self[5].set_color(ORANGE)
self.error = self[7].set_color(GRAY)
Formula Animation Patterns
Write with Timing
self.play(Write(formula), run_time=2)
self.wait(2)
Part by Part
formula = MathTex(r"E", r"=", r"m", r"c^2")
for i, part in enumerate(formula):
self.play(Write(part), run_time=0.5)
self.wait(0.3)
Transform Between Formulas
eq1 = MathTex(r"a", r"+", r"b", r"=", r"c")
eq2 = MathTex(r"a", r"=", r"c", r"-", r"b")
self.play(Write(eq1))
self.wait(1)
self.play(TransformMatchingTex(eq1, eq2))
Substitution
eq = MathTex(r"y = ", r"x", r"^2")
eq_sub = MathTex(r"y = ", r"3", r"^2")
eq_sub[1].set_color(BLUE)
self.play(Write(eq))
self.wait(1)
self.play(TransformMatchingTex(eq, eq_sub))
Highlight Term
formula = MathTex(r"E = mc^2")
self.play(formula.animate.set_color(YELLOW))
self.wait(0.5)
self.play(formula.animate.set_color(WHITE))
Layout and Alignment
Centering
formula = MathTex(r"x^2 + y^2 = r^2")
Positioning
formula.to_edge(UP, buff=0.5)
formula.to_corner(UL)
formula.move_to([2, 1, 0])
formula.next_to(other_object, DOWN)
Multiple Equations
equations = VGroup(
MathTex(r"f(x) = x^2"),
MathTex(r"f'(x) = 2x"),
MathTex(r"f''(x) = 2"),
).arrange(DOWN, aligned_edge=LEFT)
With Labels
equation = MathTex(r"E = mc^2")
label = Tex(r"Einstein's equation")
label.next_to(equation, DOWN)
group = VGroup(equation, label)
Common Statistical Formulas
MathTex(r"f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}")
MathTex(r"P(A|B) = \frac{P(B|A) P(A)}{P(B)}")
MathTex(r"y_i = \beta_0 + \beta_1 x_i + \epsilon_i")
MathTex(r"\mathcal{L}(\theta) = \prod_{i=1}^n f(x_i | \theta)")
MathTex(r"\mathbb{E}[X] = \sum_x x \cdot P(X = x)")
MathTex(r"\text{Var}(X) = \mathbb{E}[(X - \mu)^2]")
MathTex(r"\rho_{X,Y} = \frac{\text{Cov}(X,Y)}{\sigma_X \sigma_Y}")
Typography Guidelines
Font Sizes
formula.scale(1.2)
formula.scale(1.0)
formula.scale(0.7)
MathTex(r"...", font_size=48)
Spacing
r"\int f(x) \, dx"
r"a \quad b"
r"Figure~1"
Best Practices
- Use raw strings:
r"\frac{a}{b}"
- Split for animation control
- Apply semantic colors consistently
- Test rendering before complex scenes
- Use custom classes for repeated formulas