Skip to contents

Introduction

Mathematical notation is at the heart of manimR. This vignette covers how to work with LaTeX expressions, animate equations, and create beautiful math visualizations.

Basic LaTeX

Add LaTeX expressions with add_latex():

library(manimR)

scene("basic_latex") %>%
  add_latex("E = mc^2") %>%
  write_in() %>%
  render()

Note that you don’t need the $ delimiters—manimR handles that automatically.

Common Mathematical Expressions

Fractions

scene("fractions") %>%
  add_latex("\\frac{a}{b} + \\frac{c}{d} = \\frac{ad + bc}{bd}") %>%
  write_in(duration = 2) %>%
  render()

Integrals

scene("integrals") %>%
  add_latex("\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}") %>%
  write_in(duration = 3) %>%
  render()

Summations

scene("summations") %>%
  add_latex("\\sum_{n=1}^{\\infty} \\frac{1}{n^2} = \\frac{\\pi^2}{6}") %>%
  write_in(duration = 2) %>%
  render()

Matrices

scene("matrices") %>%
  add_latex("\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}") %>%
  write_in() %>%
  render()

Limits

scene("limits") %>%
  add_latex("\\lim_{x \\to 0} \\frac{\\sin x}{x} = 1") %>%
  write_in() %>%
  render()

Equation Transformations

One of manimR’s most powerful features is animating between equations:

scene("equation_transform") %>%
  add_latex("(x + y)^2") %>%
  write_in() %>%
  pause(1) %>%
  transform_to("x^2 + 2xy + y^2", duration = 2) %>%
  pause(1) %>%
  render()

Multi-Step Derivations

scene("derivation") %>%
  # Start with the integral
  add_latex("\\int x^2 dx") %>%
  write_in() %>%
  pause(1) %>%

  # Apply the power rule
  transform_to("\\frac{x^{2+1}}{2+1} + C", duration = 1.5) %>%
  pause(0.5) %>%

  # Simplify
  transform_to("\\frac{x^3}{3} + C", duration = 1) %>%
  pause(1) %>%
  render()

Highlighting Terms

Draw attention to specific parts of an equation:

scene("highlighting") %>%
  add_latex("E = mc^2") %>%
  write_in() %>%
  pause(0.5) %>%

  # Highlight mass
  highlight_term("m", color = "yellow") %>%
  pause(0.5) %>%

  # Highlight speed of light squared
  highlight_term("c^2", color = "#FF6B6B") %>%
  pause(1) %>%
  render()

Positioning Equations

Place multiple equations on screen:

scene("positioning") %>%
  # Title
  add_text("Quadratic Formula", position = c(0, 3, 0), size = 48) %>%
  write_in() %>%

  # Standard form
  add_latex("ax^2 + bx + c = 0", position = c(0, 1, 0)) %>%
  write_in() %>%
  pause(1) %>%

  # Solution
  add_latex("x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}",
            position = c(0, -1, 0), size = 1.2) %>%
  write_in(duration = 2) %>%
  pause(1) %>%

  # Highlight discriminant
  highlight_term("b^2 - 4ac", color = "yellow") %>%
  render()

Color in LaTeX

Use color within your LaTeX expressions:

scene("colored") %>%
  add_latex("\\color{red}{a}^2 + \\color{blue}{b}^2 = \\color{green}{c}^2") %>%
  write_in() %>%
  render()

Best Practices

1. Escape Backslashes

In R strings, backslashes must be escaped:

# Correct
add_latex("\\frac{1}{2}")

# Wrong - will cause errors
# add_latex("\frac{1}{2}")

2. Use Raw Strings for Complex LaTeX

For complex expressions, use raw strings:

complex_eq <- r"(\int_0^{\pi} \sin^2(x) dx = \frac{\pi}{2})"
scene("raw") %>%
  add_latex(complex_eq) %>%
  write_in() %>%
  render()

3. Build Expressions Incrementally

For complex animations, build up expressions:

scene("incremental") %>%
  add_latex("f(x)") %>%
  write_in() %>%
  pause(0.5) %>%

  transform_to("f(x) = ") %>%
  pause(0.3) %>%

  transform_to("f(x) = x^2") %>%
  pause(0.5) %>%

  transform_to("f(x) = x^2 + 1") %>%
  render()

LaTeX Requirements

For full LaTeX support, you need LaTeX installed on your system:

# Install TinyTeX (recommended)
install.packages("tinytex")
tinytex::install_tinytex()

# Or install full TeX Live/MiKTeX from:
# https://www.latex-project.org/get/

Without LaTeX, manimR will render mathematical notation using basic text rendering, which may not look as polished.

Next Steps