MathJax Examples: Copy-Ready Math Syntax Cheat Sheet

November 11, 2026 · 11 min read

MathJax Examples: Copy-Ready Syntax for Fractions, Matrices and Cases

These MathJax examples cover the math you write most: fractions, roots, growing brackets, matrices, aligned equations, piecewise functions, fonts, color and your own macros. Treat this page as a MathJax cheat sheet for versions 3 and 4. Each table marks whether the same code also renders in KaTeX, the engine behind many Markdown apps and our editor.

MathJax Delimiters at a Glance: Inline and Display Math

MathJax, the JavaScript library that renders LaTeX math in the browser, looks for math between delimiters. Its defaults differ from what most Markdown tools expect, per the MathJax delimiter docs:

DelimiterModeOn by default in MathJax 3 and 4?Our editor's preview
\(...\)InlineYesNot rendered
\[...\]DisplayYesNot rendered
$$...$$DisplayYesRenders (KaTeX)
$...$InlineNo, the site's config has to add itRenders (KaTeX)

Single dollar signs are a config choice, and Math Stack Exchange, GitHub and Jupyter all switch them on. MathJax also picks up a bare \begin{...} environment with no delimiter around it.

Each MathJax example below uses $ and $$, so it pastes straight into most Markdown tools. On a MathJax page that hasn't enabled dollars, swap them for \( and \[. For how math works in Markdown generally, see our guide to writing equations in Markdown. To add MathJax to your own page and switch dollar delimiters on, follow our MathJax LaTeX setup guide.

How Do You Write Fractions, Roots and Exponents in MathJax?

Fractions take two brace groups, and exponents or subscripts longer than one character need braces too. Here's the core set, with the rendered result beside each command:

You typeYou getWorks in KaTeX?
\frac{a}{b}ab\frac{a}{b}Yes
\dfrac{a}{b} (full size inline)ab\dfrac{a}{b}Yes
\tfrac{a}{b} (small)ab\tfrac{a}{b}Yes
\cfrac{1}{1+\cfrac{1}{x}}11+1x\cfrac{1}{1+\cfrac{1}{x}}Yes
\sqrt{x} and \sqrt[3]{x}x\sqrt{x} and x3\sqrt[3]{x}Yes
x^{10}, x_{i,j}x10x^{10}, xi,jx_{i,j}Yes
\binom{n}{k}(nk)\binom{n}{k}Yes
a \equiv b \pmod{n}a≡b(modn)a \equiv b \pmod{n}Yes
\sum_{i=1}^{n} i^2∑i=1ni2\sum_{i=1}^{n} i^2Yes
\int_a^b f(x)\,dx∫abf(x) dx\int_a^b f(x)\,dxYes
\lim_{x \to 0} \frac{\sin x}{x}lim⁡x→0sin⁡xx\lim_{x \to 0} \frac{\sin x}{x}Yes

The classic trap is x^10. Without braces, only the first token goes up, so you get x10x^10 instead of x10x^{10}. Stacked powers need explicit grouping as well: x^{y^z} is a tower, while {x^y}^z raises the whole xyx^y. Our LaTeX subscript and superscript guide covers primes and the double-script error. The size rules for \frac, \dfrac and \tfrac are in our LaTeX fraction guide.

Greek letters are the name after a backslash: \alpha, \beta, \Gamma, \varepsilon give α\alpha, β\beta, Γ\Gamma, ε\varepsilon. Capitalize the name for uppercase forms that differ from Latin letters, such as \Omega.

Accents, relations and arrows follow suit. \hat{x}, \bar{x}, \vec{v} and \dot{x} give x^\hat{x}, xˉ\bar{x}, v⃗\vec{v}, x˙\dot{x}. \le, \ne, \approx, \Rightarrow and \infty give ≤\le, ≠\ne, ≈\approx, ⇒\Rightarrow, ∞\infty.

Brackets That Grow: \left, \right, \middle and \big

Plain parentheses stay at text height, so they look cramped around a fraction. Prefix them with \left and \right and MathJax sizes them to fit the content:

$$
\left( \frac{1}{2} \right)^2
\qquad
\left\{ x \in \mathbb{R} \;\middle|\; x > 0 \right\}
\qquad
\left. \frac{df}{dx} \right|_{x=0}
$$

The first expression wraps the half in tall parentheses. The second builds set-builder notation with literal braces, written \{ and \}, and a divider that \middle stretches to match. The third uses \left. as an invisible delimiter, so only the evaluation bar on the right appears.

When you want a fixed size, use \big(, \Big(, \bigg( and \Bigg(, which give four steps from slightly larger to very tall. Other pairs work with both approaches: \langle x \rangle, \lceil x \rceil, \lfloor x \rfloor and \lVert v \rVert for a norm. Like most MathJax examples on this page, every command in this section renders in KaTeX too.

How Do You Write a Matrix in MathJax?

Choose the environment by the brackets you want, separate columns with & and end each row with \\:

EnvironmentBracketsWorks in KaTeX?
matrixNoneYes
pmatrixParenthesesYes
bmatrixSquare bracketsYes
BmatrixCurly bracesYes
vmatrixSingle vertical bars (determinant)Yes
VmatrixDouble vertical bars (norm)Yes
smallmatrixNone, sized for inline textYes

A 3-by-3 identity matrix and a general m-by-n matrix with dots look like this:

$$
I_3 = \begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{bmatrix}
\qquad
A = \begin{pmatrix}
a_{11} & \cdots & a_{1n} \\
\vdots & \ddots & \vdots \\
a_{m1} & \cdots & a_{mn}
\end{pmatrix}
$$

That renders the identity matrix in square brackets beside a parenthesized matrix whose middle row and column collapse into vertical, diagonal and horizontal dots. For an augmented matrix, use an array with a bar in its column spec, \left[ \begin{array}{cc|c} 1 & 2 & 5 \\ 3 & 4 & 6 \end{array} \right]. It draws a vertical rule before the last column.

One warning: some Markdown processors treat \\ as an escaped backslash and pass a single one through. If your rows collapse into one line, the Markdown layer is the cause, not MathJax. Test in the tool you publish with.

Aligned Equations and Piecewise Functions with align and cases

For a derivation, put & before the symbol you want lined up, usually =. MathJax supports the amsmath environments, including align, align*, aligned, gather* and cases:

$$
\begin{aligned}
(x+2)^2 &= (x+2)(x+2) \\
&= x^2 + 4x + 4
\end{aligned}
$$

$$
f(n) = \begin{cases}
n/2 & \text{if } n \text{ is even} \\
3n+1 & \text{if } n \text{ is odd}
\end{cases}
$$

The first block stacks two lines with their equals signs in one column. The second draws a single left brace with two rows, each pairing a formula with its condition. Swap aligned for align* and you get the same layout as a standalone environment, and gather* centers lines without aligning them.

We prefer aligned inside $$ over a bare align*. The same source is valid in MathJax, KaTeX and a real LaTeX document. By contrast, align* inside display math is an error in LaTeX, and KaTeX rejects it in inline math. Our KaTeX vs LaTeX comparison covers what else KaTeX leaves out.

Numbering is a page setting. MathJax only numbers align rows when the site sets tags to 'ams' or 'all'. A manual \tag{1} in display math works in both engines.

Text, Fonts and Color Inside MathJax Math

Math mode ignores spaces and sets letters in italic, so words need \text. Put the space inside the braces: \text{if }x keeps the gap, while \text{if}x glues the word to the variable. For a named function, \operatorname{argmax}_x gives upright type with the right spacing. For manual gaps, \, \; \quad and \qquad insert a thin space, a thick space, one em and two em.

You typeYou getWorks in KaTeX?
\mathbb{R}, \mathbb{Z}R\mathbb{R}, Z\mathbb{Z}Yes (capital letters only, plus k)
\mathcal{L}L\mathcal{L}Yes
\mathfrak{g}g\mathfrak{g}Yes
\mathbf{v}, \boldsymbol{\alpha}v\mathbf{v}, α\boldsymbol{\alpha}Yes
\mathrm{d}xdx\mathrm{d}xYes
{\color{red} x} + yx+y{\color{red} x} + yYes
\textcolor{blue}{y}y\textcolor{blue}{y}Yes
\boxed{E = mc^2}E=mc2\boxed{E = mc^2}Yes
\cancel{x}x\cancel{x}Yes
\cancelto{0}{x}Strike with an arrow to 0No

Color is where old tutorials mislead you. In MathJax 2, \color{red}{x} colored only its argument. Since version 3, \color is a switch, as in LaTeX, and recolors everything after it in the same group. The color extension docs note that sites can load colorv2 to get the old behavior back.

How Do You Define Your Own Commands in MathJax?

Use \newcommand inside math delimiters. This is handy for Anki cards or long notes where you type the same symbol often:

$$
\newcommand{\RR}{\mathbb{R}}
\newcommand{\seq}[2]{(#1_#2)_{#2 \in \mathbb{N}}}
f : \RR \to \RR, \quad \seq{u}{n}
$$

The first line makes \RR print the blackboard-bold R. The second takes two arguments, so \seq{u}{n} renders as (un)n∈N(u_n)_{n \in \mathbb{N}}. Pick fresh names: KaTeX already defines \R, so \newcommand{\R} fails there with a redefinition error. Use \renewcommand to replace an existing macro, or the TeX-style \def\RR{\mathbb{R}}.

In MathJax, a definition stays available for the rest of the page, per the macro docs. Site owners can also predefine macros in the MathJax configuration. KaTeX supports \newcommand too, but a macro only carries into later expressions when the host app shares its macro store between renders. In our editor, define the macro in the same $$ block that uses it.

MathJax-Only Commands That KaTeX Won't Render

Now the limitation. Our editor and our LaTeX equation editor render math with KaTeX, not MathJax. These MathJax features fail there and in other KaTeX-based apps, per the KaTeX support table:

MathJax commandWhat it does in MathJaxKaTeXPortable alternative
\(...\), \[...\]Default inline and display delimitersDepends on the app. Not in our preview$...$ and $$...$$
\require{cancel}Loads an extension. Autoload makes it rarely needed in v3 and 4Not supportedDrop it
\class, \cssId, \styleAdd a CSS class, an id or inline stylesNot supported\color or \textcolor
\bbox[yellow,5px]{x}Background color and paddingNot supported\boxed{x} or \colorbox
\enclose{circle}{x}Circles, boxes and strikesNot supported\boxed{x} or \cancel{x}
\unicode{x263A}Character by code pointNot supportedA named symbol command
\label and \eqrefNumbered cross-referencesNot supported\tag{1}, then type (1)
\DeclareMathOperatorDefines an operator nameNot supported\operatorname{rank}

Apart from \cancelto and this table, the MathJax examples on this page use commands KaTeX supports, as long as you wrap them in $ or $$. Edit the sample below and the preview updates as you type:

MathJax examples, rendered here with KaTeX

Inline: ab\dfrac{a}{b}, x3\sqrt[3]{x}, (nk)\binom{n}{k}, ⌊x⌋\left\lfloor x \right\rfloor

f(n)={n/2if n is even3n+1if n is oddf(n) = \begin{cases} n/2 & \text{if } n \text{ is even} \\ 3n+1 & \text{if } n \text{ is odd} \end{cases} B=[102013]B = \begin{bmatrix} 1 & 0 & 2 \\ 0 & 1 & 3 \end{bmatrix} sin⁡2θ+cos⁡2θ=1tan⁡θ=sin⁡θcos⁡θ\begin{aligned} {\color{teal} \sin^2\theta + \cos^2\theta} &= 1 \\ \boxed{\tan\theta} &= \frac{\sin\theta}{\cos\theta} \end{aligned}
67 words446 characters24 lines
Markdown

MathJax Examples: Quick Answers

Bookmark these MathJax examples and copy the block you need. Stick to $ and $$, and skip \cancelto and the extensions in the last table. Then the same code should render on GitHub, in Jupyter, in KaTeX-based apps and on MathJax pages with dollar delimiters turned on. To check a formula before you publish it, build it in the LaTeX equation editor or paste it into the editor and watch the preview. For notebook-specific math, see the Jupyter Markdown cheat sheet.