If you need to write mathematical notation in markdown files, KaTeX is the way to go. It’s fast, renders beautifully, and uses familiar LaTeX syntax.
What is KaTeX?
KaTeX is a math typesetting library that renders LaTeX math notation on the web. It’s faster than MathJax and produces crisp, readable formulas. If you’ve ever used LaTeX in academic papers, you already know the syntax.
KaTeX works in many contexts: Hugo sites, GitHub markdown, Jekyll, documentation sites, and more.
Setting Up KaTeX
Setup varies by platform:
Hugo (with Stack theme or similar):
Many Hugo themes support KaTeX out of the box. If yours doesn’t, add this to layouts/partials/head/custom.html:
| |
GitHub Markdown: GitHub renders KaTeX automatically. Just use the syntax below - no setup needed.
Other platforms: Most static site generators and documentation tools support KaTeX through plugins or themes. Check your platform’s documentation.
Once set up, the syntax below works everywhere.
Basic Syntax
Inline Math
Use single dollar signs for inline math:
| |
Renders as: The quadratic formula is $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.
Display Math (Block)
Use double dollar signs for centered, display-style equations:
| |
Renders as:
$$ E = mc^2 $$
Common Examples
Fractions
| |
$$ \frac{a}{b} \quad \frac{1}{2} \quad \frac{\pi}{4} $$
Exponents and Subscripts
| |
$$ x^2 \quad y^{10} \quad a_1 \quad b_{n+1} $$
Square Roots
| |
$$ \sqrt{2} \quad \sqrt{x^2 + y^2} \quad \sqrt[3]{8} $$
Greek Letters
| |
$$ \alpha, \beta, \gamma, \delta, \epsilon, \theta, \pi, \sigma, \omega $$
Summation and Products
| |
$$ \sum_{i=1}^{n} i = \frac{n(n+1)}{2} $$
| |
$$ \prod_{i=1}^{n} i = n! $$
Integrals
| |
$$ \int_0^1 x^2 , dx = \frac{1}{3} $$
Limits
| |
$$ \lim_{x \to \infty} \frac{1}{x} = 0 $$
Matrices
| |
$$ \begin{bmatrix} a & b \ c & d \end{bmatrix} $$
Systems of Equations
| |
$$ \begin{cases} x + y = 5 \ 2x - y = 1 \end{cases} $$
Real-World Example: Compound Interest
Here’s a practical formula you might actually use:
| |
Renders as:
$$ A = P\left(1 + \frac{r}{n}\right)^{nt} $$
Where:
- $A$ = final amount
- $P$ = principal (initial investment)
- $r$ = annual interest rate (decimal)
- $n$ = compounding frequency per year
- $t$ = time in years
Quick Reference
| What You Want | LaTeX Syntax | Result |
|---|---|---|
| Fraction | \frac{a}{b} | $\frac{a}{b}$ |
| Exponent | x^2 | $x^2$ |
| Subscript | x_1 | $x_1$ |
| Square root | \sqrt{x} | $\sqrt{x}$ |
| Summation | \sum_{i=1}^{n} | $\sum_{i=1}^{n}$ |
| Integral | \int_a^b | $\int_a^b$ |
| Limit | \lim_{x \to \infty} | $\lim_{x \to \infty}$ |
| Pi | \pi | $\pi$ |
| Infinity | \infty | $\infty$ |
| Less/Greater | \leq \geq | $\leq$ $\geq$ |
| Not equal | \neq | $\neq$ |
| Approximately | \approx | $\approx$ |
Common Operators
- Multiplication:
\times→ $\times$ - Division:
\div→ $\div$ - Plus/minus:
\pm→ $\pm$ - Dot product:
\cdot→ $\cdot$
Spacing
Use these for better spacing:
\,— small space\quad— medium space\qquad— large space
Example: $a \, b \quad c \qquad d$ renders as $a , b \quad c \qquad d$
Tips
Escaping special characters: If you need to show actual dollar signs in text, escape them: \$100
Alignment: Use & for alignment in multi-line equations with aligned environment:
| |
$$ \begin{aligned} f(x) &= x^2 + 2x + 1 \ &= (x + 1)^2 \end{aligned} $$
Curly braces: Use \{ and \} for literal braces: ${1, 2, 3}$
Resources
- KaTeX Supported Functions - Complete reference
- Detexify - Draw a symbol to find its LaTeX command
- LaTeX Math Symbols - Comprehensive PDF
That’s all you need to add beautiful math notation to your posts. The syntax might look intimidating at first, but you’ll quickly remember the common patterns.