LaTeX vs KaTeX — Math Notation in the Browser
A deep-dive comparison of LaTeX and KaTeX for rendering mathematical expressions in web documents and PDFs, with syntax examples and performance notes.
If you’ve ever needed to write a mathematical formula in a document, you’ve likely encountered LaTeX — the gold standard for typesetting math. KaTeX is a modern, browser-native implementation of LaTeX math rendering. This guide explains the difference and how to use KaTeX in your Markdown documents.
What Is LaTeX?
LaTeX (pronounced “lah-tech”) is a document preparation system designed by Leslie Lamport in 1984, built on Donald Knuth’s TeX typesetting system. It is the standard for academic publishing in mathematics, physics, engineering, and economics.
LaTeX produces beautifully typeset mathematical notation, but it requires a local LaTeX installation and compiles documents into PDFs — not HTML.
What Is KaTeX?
KaTeX is a JavaScript library developed by Khan Academy that renders a large subset of LaTeX math syntax directly in the browser. It is:
- Fast — renders synchronously, with no layout reflow
- Server-side renderable — generates static HTML, no JavaScript required at view time
- High fidelity — output is visually very close to native LaTeX
KaTeX does not require a LaTeX installation. It runs entirely in the browser or on Node.js.
Syntax: KaTeX in Markdown
Inline Math
Wrap expressions in single dollar signs for inline math:
The quadratic formula is $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.
Renders as: The quadratic formula is $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.
Block Math
Wrap expressions in double dollar signs for display (block) math:
$$
\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}
$$
Block math is centred and displayed on its own line.
Common KaTeX Expressions
Fractions
$\frac{numerator}{denominator}$
$\frac{d}{dx}\left(\frac{f(x)}{g(x)}\right) = \frac{f'g - fg'}{g^2}$
Superscripts and Subscripts
$x^2$ (superscript)
$x_i$ (subscript)
$x_i^2$ (both)
$x_{i,j}^{n}$ (multi-character)
Greek Letters
$\alpha, \beta, \gamma, \delta, \epsilon$
$\pi, \sigma, \omega, \lambda, \mu$
$\Sigma, \Pi, \Omega, \Lambda$
Summation and Integrals
$$\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$$
$$\int_0^1 x^2\,dx = \frac{1}{3}$$
$$\oint_C \vec{F} \cdot d\vec{r}$$
Matrices
$$
\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
\begin{pmatrix}
x \\
y
\end{pmatrix}
=
\begin{pmatrix}
ax + by \\
cx + dy
\end{pmatrix}
$$
Cases (Piecewise Functions)
$$
f(x) = \begin{cases}
x^2 & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}
$$
LaTeX vs KaTeX Feature Comparison
| Feature | LaTeX | KaTeX |
|---|---|---|
| Runs in browser | ❌ | ✅ |
| Requires installation | ✅ | ❌ |
| Full LaTeX support | ✅ | ⚠️ Subset |
| PDF output | ✅ Native | ✅ Via headless browser |
| Rendering speed | Slow (compile) | Very fast |
| Custom macros | ✅ Full | ✅ \newcommand |
| TikZ diagrams | ✅ | ❌ (use TikZ via Kroki) |
| Chemical equations | ✅ | ✅ (mhchem extension) |
Custom Macros
KaTeX supports \newcommand-style macros defined in the document configuration. You can define shorthand for frequently used expressions:
When KaTeX Is Not Enough
KaTeX covers ~95% of math notation needs. It does not support:
- TikZ (complex geometric diagrams) — use the TikZ guide for Kroki-based rendering instead
- PSTricks (PostScript-based graphics)
- Custom LaTeX packages not part of the core
For the rare cases requiring full LaTeX, export your formula from a LaTeX system as an SVG and embed it as an image.