Skip to content

LaTeX Labels

cplots handles LaTeX in axis labels, titles, and legend entries across all backends through the latex_labels flag.

The problem

Backends differ in their LaTeX support:

Backend LaTeX support
matplotlib Native — renders via TeX or mathtext
plotly (HTML) Via MathJax — requires raw LaTeX strings
altair None — Vega-Lite does not support LaTeX

A label like r"$k\ [h\,\mathrm{Mpc}^{-1}]$" renders correctly in matplotlib and plotly (with MathJax) but appears as raw markup in altair.

The solution

Write labels in LaTeX. matplotlib (and ultraplot) always render them with mathtext. For plotly and altair, latex_labels decides what happens:

  • latex_labels=False (the default): the label is converted to plain Unicode, so $k\;[h\,\mathrm{Mpc}^{-1}]$ shows as k [h Mpc⁻¹].
  • latex_labels=True: the string is passed through unchanged, and plotly renders it with MathJax in HTML output.

The built-in default labels are already LaTeX ($k$, $P(k)$, $\ell$, $C_\ell$, $z$, $H$, and unit-bearing labels from the CLASS/CAMB adapters), so they render as math in matplotlib and as Unicode on plotly/altair.

When latex_labels=False (the default), plotly and altair receive the label stripped to plain Unicode. The stripping logic handles:

  • Greek letters (\alpha → α)
  • Super/subscripts (^{-1} → ⁻¹, _{m} → ₘ)
  • Math mode delimiters ($...$ removed)
  • \frac{a}{b} → a/b
  • Spacing commands (\;, \,, \quad) → a space, \! removed

strip_latex directly

Multi-panel figures

Pass latex_labels=True on the Figure to propagate to all panels:

For plotly, pass backend="plotly" to get MathJax rendering in the browser:

cplots.Figure.grid(
    [[p_tt, p_z1]],
    latex_labels=True,
).render(backend="plotly")