Technical Specification Snapshot
| Parameter | Description |
|---|---|
| Core topic | Partial formula coloring in Manim |
| Primary languages | Python, LaTeX |
| Render object | MathTex |
| Coloring approaches | Object splitting, native xcolor coloring |
| Dependencies | manim, LaTeX environment, xcolor package |
| Source type | Manim tutorial article |
| Star count | Not provided in the original content |
| Typical use cases | Math formula highlighting, instructional animation, step-by-step demonstrations |
This tutorial focuses on the problem of partial formula coloring in ManimCE. It explains how to assign different colors to different segments of a
MathTexexpression and compares the practical boundaries of two approaches: splitting the expression into objects and using native LaTeX coloring. The core pain point is that mathematical relationships are often hard to highlight clearly in animation when everything appears in a single color. Keywords: ManimCE, MathTex, LaTeX coloring.
This tutorial solves both formula highlighting and animation control
In mathematical animation, a formula must do more than render correctly. It also needs to communicate structural relationships. For example, in the Pythagorean theorem, if a^2, b^2, and c^2 correspond to different geometric regions, a monochrome formula weakens the viewer’s understanding of that mapping.
In ManimCE, two common approaches are used. One splits the formula into multiple independent objects and colors them one by one. The other writes color commands directly inside the LaTeX string. Both approaches work, but they provide different levels of control.
Manim’s object-splitting approach works best when you need fine-grained animation control
The core idea is to pass multiple segments into MathTex as separate arguments. Each segment becomes an indexable subobject, so you can set its color, position, and scale independently, or even attach a separate animation to each term.
from manim import *
class LatexColorSplit(Scene):
def construct(self):
# Split the formula into multiple independent segments
tex = MathTex("a^2", "+", "b^2", "=", "c^2")
# Access subobjects by index and assign colors
tex[0].set_color(YELLOW) # Set a^2 to yellow
tex[2].set_color(GREEN) # Set b^2 to green
tex[4].set_color(RED) # Set c^2 to red
self.play(Write(tex))
self.wait()
This code splits the formula into five subobjects, applies different colors to the key terms, and then writes the whole expression to the screen.

AI Visual Insight: The image shows the Pythagorean theorem rendered with partial coloring after the formula has been split into segments. a^2, b^2, and c^2 appear in yellow, green, and red respectively, confirming that MathTex subobject indexing maps successfully to different mathematical terms. This setup is ideal for item-by-item animation control later.
The main advantage of splitting is that every formula segment becomes a real object
When you write MathTex("a^2", "+", "b^2", "=", "c^2"), Manim does not simply concatenate strings internally. It generates a structure that contains multiple child elements. Indexes , 2, and 4 correspond to the three squared terms.
That means you can do more than recolor them. You can also apply FadeIn, Transform, or Indicate to each term independently. For teaching animations, derivation walkthroughs, and step-by-step presentation videos, this object-oriented control is extremely important.
Native LaTeX coloring works better for complex static formulas
If the formula structure is complex—for example, with fractions, matrices, or nested subscripts and superscripts—manually splitting the string can become tedious. In that case, the more natural solution is to use LaTeX’s \color{} command directly and mark colors inside the formula source.
from manim import *
class LatexColorNative(Scene):
def construct(self):
# Create a template and enable the xcolor package
my_template = TexTemplate()
my_template.add_to_preamble(r"\usepackage{xcolor}")
# Declare colors directly inside the LaTeX string
tex = MathTex(
r"{\color{yellow} a^2} + {\color{green} b^2} = {\color{red} c^2}",
tex_template=my_template,
)
self.play(Write(tex))
self.wait()
This code enables the xcolor package and applies color directly inside the LaTeX source for the formula.

AI Visual Insight: The image shows a colored formula rendered directly through LaTeX \color{} commands. The visual result is similar to the splitting approach, but the color information is embedded in the formula source itself, which makes it a better fit for static emphasis inside more complex structures.
The key prerequisite for native LaTeX coloring is loading the required package
Many people fail the first time they try this approach, but the issue is usually not syntax. The real problem is that the template does not explicitly load xcolor. For compilation efficiency, Manim does not enable every LaTeX package by default, so you need to extend the preamble manually.
Once the template is configured correctly, the readability of complex formulas improves significantly. This is especially true when coloring numerators, denominators, summation terms, or symbols inside nested parentheses. In those cases, the LaTeX-based approach is often more stable than index-based targeting and also aligns better with standard academic typesetting.
The right choice depends on animation requirements, not syntax preference
If your goal is to animate the formula later, choose the splitting approach first. Because every segment is an object, it is easier to control appearance order, motion paths, and emphasis timing.
If your goal is to display a complex formula statically, choose native LaTeX coloring first. It reduces the cost of manual splitting and index maintenance while preserving the integrity of the formula structure.
from manim import *
class LatexColorCompare(Scene):
def construct(self):
# Method 1: Splitting, ideal for per-term control
tex01 = MathTex("a^2", "+", "b^2", "=", "c^2")
tex01[0].set_color(YELLOW) # Highlight a^2
tex01[2].set_color(GREEN) # Highlight b^2
tex01[4].set_color(RED) # Highlight c^2
# Method 2: Native LaTeX coloring, ideal for complex static formulas
my_template = TexTemplate()
my_template.add_to_preamble(r"\usepackage{xcolor}")
tex02 = MathTex(
r"{\color{yellow} a^2} + {\color{green} b^2} = {\color{red} c^2}",
tex_template=my_template,
).next_to(tex01, DOWN)
self.play(Write(tex01))
self.play(Write(tex02))
self.wait()
This example compares both formula coloring strategies on the same screen, so you can directly observe the difference in syntax and rendered output.
You can make the practical decision with one simple rule
If you need derivation animation, term-level highlighting, stepwise entry, or local transforms, the splitting approach is almost always the safer option. It matches Manim’s object model and is easier to debug.
If you are working with large formulas, only need the final display, and want the source code to stay close to standard LaTeX, native coloring is more efficient. It keeps the color description inside the formula text, which makes complex expressions easier to maintain.
FAQ
Q1: Why does \color{} not show any color in my formula?
A: In most cases, you did not add \usepackage{xcolor} to the TexTemplate. Extend the preamble first, then pass that template to MathTex.
Q2: Why do the indexes in the splitting approach keep pointing to the wrong parts?
A: MathTex generates subobjects based on the segments you pass in. The most reliable method is to pass each controllable part as a separate argument instead of relying on automatic splitting of a complex string.
Q3: Which approach is better for step-by-step teaching animations?
A: Prefer the splitting approach. It turns each term into an independent object, which makes it easy to write, move, scale, and highlight items one by one. That is why it is the most common solution in math explainer videos.
AI Readability Summary: This article systematically reconstructs the two mainstream approaches to partial formula coloring in Manim: object-level coloring based on MathTex splitting and native coloring based on LaTeX xcolor. It compares their underlying principles, use cases, animation control capabilities, and maintainability for complex formulas, and includes complete runnable examples you can use directly.