Math Typography

by choxos

skill

Mathematical rendering with MathTex, Tex, tex_to_color_map, custom equation classes, and formula animation patterns.

Skill Details

Repository Files

1 file in this skill directory


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

# Single equation (entire string is math mode)
eq = MathTex(r"E = mc^2")

# Multiple parts for animation control
eq = MathTex(r"E", r"=", r"m", r"c^2")
# eq[0] = "E", eq[1] = "=", eq[2] = "m", eq[3] = "c^2"

Tex - Mixed Text and Math

# Text with inline math
label = Tex(r"The equation $E = mc^2$ is famous")

# Multiple parts
label = Tex(r"The value is ", r"$x = 5$")
label[1].set_color(BLUE)  # Color the math part

Semantic Coloring

tex_to_color_map

# Automatic coloring of matched substrings
formula = MathTex(
    r"P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}",
    tex_to_color_map={
        r"A": BLUE,
        r"B": ORANGE,
        r"P": GREEN,
    }
)

Manual Coloring

# Color specific parts
eq = MathTex(r"y", r"=", r"m", r"x", r"+", r"b")
eq[0].set_color(BLUE)    # y
eq[2].set_color(RED)     # m
eq[3].set_color(GREEN)   # x
eq[5].set_color(PURPLE)  # b

Color by Index

# Access characters within a part
eq = MathTex(r"f(x) = x^2")
eq[0][0].set_color(BLUE)     # 'f'
eq[0][2].set_color(GREEN)    # 'x' in f(x)

Split Strategies for Animation

Simple Split

# Each term separate
eq = MathTex(r"a", r"+", r"b", r"=", r"c")

# Animate part by part
self.play(Write(eq[0]))     # a
self.play(Write(eq[1:3]))   # + b
self.play(Write(eq[3:]))    # = c

Grouped Split

# Logical groupings
eq = MathTex(r"y = ", r"mx", r" + ", r"b")

self.play(Write(eq[0]))   # y =
self.play(Write(eq[1]))   # mx
self.play(Write(eq[2:]))  # + b

Complex Formula Split

# Fraction with separate parts
frac = MathTex(
    r"\frac{",           # 0: opening
    r"a + b",            # 1: numerator
    r"}{",               # 2: middle
    r"c",                # 3: denominator
    r"}"                 # 4: closing
)

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)}")  # Both

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
        )

        # Store references to parts
        self.x = self[0][0]          # x
        self.equals = self[0][1]     # =
        self.negative_b = self[0][2:4]  # -b
        self.discriminant = self[0][6:13]  # b^2 - 4ac
        self.denominator = self[0][14:16]  # 2a

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

# Simple write
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

# Original
eq = MathTex(r"y = ", r"x", r"^2")

# With value substituted
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")

# Temporary highlight
self.play(formula.animate.set_color(YELLOW))
self.wait(0.5)
self.play(formula.animate.set_color(WHITE))

Layout and Alignment

Centering

# Default: centered on screen
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

# Normal PDF
MathTex(r"f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}")

# Bayes' theorem
MathTex(r"P(A|B) = \frac{P(B|A) P(A)}{P(B)}")

# Linear regression
MathTex(r"y_i = \beta_0 + \beta_1 x_i + \epsilon_i")

# Maximum likelihood
MathTex(r"\mathcal{L}(\theta) = \prod_{i=1}^n f(x_i | \theta)")

# Expected value
MathTex(r"\mathbb{E}[X] = \sum_x x \cdot P(X = x)")

# Variance
MathTex(r"\text{Var}(X) = \mathbb{E}[(X - \mu)^2]")

# Correlation
MathTex(r"\rho_{X,Y} = \frac{\text{Cov}(X,Y)}{\sigma_X \sigma_Y}")

Typography Guidelines

Font Sizes

# Title formulas
formula.scale(1.2)

# Normal formulas
formula.scale(1.0)  # Default

# Annotation formulas
formula.scale(0.7)

# Or use font_size
MathTex(r"...", font_size=48)

Spacing

# Thin space in integrals
r"\int f(x) \, dx"

# Quad space
r"a \quad b"

# No break space
r"Figure~1"

Best Practices

  1. Use raw strings: r"\frac{a}{b}"
  2. Split for animation control
  3. Apply semantic colors consistently
  4. Test rendering before complex scenes
  5. Use custom classes for repeated formulas

Related Skills

Attack Tree Construction

Build comprehensive attack trees to visualize threat paths. Use when mapping attack scenarios, identifying defense gaps, or communicating security risks to stakeholders.

skill

Grafana Dashboards

Create and manage production Grafana dashboards for real-time visualization of system and application metrics. Use when building monitoring dashboards, visualizing metrics, or creating operational observability interfaces.

skill

Matplotlib

Foundational plotting library. Create line plots, scatter, bar, histograms, heatmaps, 3D, subplots, export PNG/PDF/SVG, for scientific visualization and publication figures.

skill

Scientific Visualization

Create publication figures with matplotlib/seaborn/plotly. Multi-panel layouts, error bars, significance markers, colorblind-safe, export PDF/EPS/TIFF, for journal-ready scientific plots.

skill

Seaborn

Statistical visualization. Scatter, box, violin, heatmaps, pair plots, regression, correlation matrices, KDE, faceted plots, for exploratory analysis and publication figures.

skill

Shap

Model interpretability and explainability using SHAP (SHapley Additive exPlanations). Use this skill when explaining machine learning model predictions, computing feature importance, generating SHAP plots (waterfall, beeswarm, bar, scatter, force, heatmap), debugging models, analyzing model bias or fairness, comparing models, or implementing explainable AI. Works with tree-based models (XGBoost, LightGBM, Random Forest), deep learning (TensorFlow, PyTorch), linear models, and any black-box model

skill

Pydeseq2

Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.

skill

Query Writing

For writing and executing SQL queries - from simple single-table queries to complex multi-table JOINs and aggregations

skill

Pydeseq2

Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.

skill

Scientific Visualization

Meta-skill for publication-ready figures. Use when creating journal submission figures requiring multi-panel layouts, significance annotations, error bars, colorblind-safe palettes, and specific journal formatting (Nature, Science, Cell). Orchestrates matplotlib/seaborn/plotly with publication styles. For quick exploration use seaborn or plotly directly.

skill

Skill Information

Category:Skill
Last Updated:1/27/2026