Coding Guide in MathJax (LaTeX)


LaTeX (MathJax) Basic Tutorials and Coding Guide
✅ Useful code snippets and explanations for understanding and documenting with MathJax (LaTeX). Perfect for students, educators, and programmers! 😻
Just click the "copy code" button below 'markdown'!
Summary Overview
| Group | Section | What you'll learn | Key commands |
|---|---|---|---|
| Getting started | 1.1 Inline vs Display Maths | maths inside a sentence or on its own line | $ $, $$ $$ |
| 1.2 Superscripts and Subscripts | powers, indices and grouping | ^, _, { } | |
| 1.3 Fractions | fractions in every size | \frac, \dfrac, \tfrac, \cfrac | |
| 1.4 Roots | square roots and n-th roots | \sqrt{}, \sqrt[n]{} | |
| Building blocks | 1.5 Greek Letters | symbols used in ML and statistics | \alpha, \theta, \sigma, \lambda |
| 1.6 Brackets that Resize | brackets that fit tall formulas | \left( \right), \{ \}, \lvert \rvert | |
| 1.7 Operators and Relations | the punctuation of maths | \times, \le, \neq, \approx | |
| 1.8 Big Operators with Limits | sums, products, integrals and limits | \sum, \prod, \int, \lim | |
| 1.9 Function Names | upright names such as log and max | \log, \exp, \operatorname{} | |
| 1.10 Fonts and Accents | bold vectors, hats and bars | \mathbf, \mathbb, \hat, \bar | |
| 1.11 Sets and Logic | writing definitions | \in, \forall, \cup, \mid | |
| Layout | 1.12 Ellipses | the different kinds of dots | \ldots, \cdots, \vdots, \ddots |
| 1.13 Space Commands | spaces from tiny to wide, and line breaks | \,, \;, \quad, \\ | |
| 1.14 Matrix Environments | matrix bracket styles | pmatrix, bmatrix, vmatrix | |
| 1.15 Special Characters | printing symbols such as the percent and dollar signs | \%, \$, \_, \& | |
| 1.16 Boxes, Colour and Equation Numbers | boxed answers, colour and numbered equations | \boxed{}, \color{}, \tag{} |
1.1 Inline vs Display Maths
1.1.1 Inline Maths
Inline Maths: one dollar sign on each side
Example:
The area of a circle is
Inline maths sits inside a sentence. On this website, \( \) does not display, so always use dollar signs.
Where it's used: symbols and short formulas inside explanations.
The area of a circle is $\pi r^2$, where $r$ is the radius.1.1.2 Display Maths
Display Maths: two dollar signs on their own lines
Example:
Display maths is centred on its own line, and big symbols like sums get more room.
Where it's used: main formulas and step-by-step solutions.
$$
A = \pi r^2
$$1.2 Superscripts and Subscripts
1.2.1 Superscripts and Subscripts
Superscript "^" and Subscript "_"
Example:
^ raises the next character and _ lowers it. You can use both on the same symbol.
Where it's used: powers, and indices such as the
$$
x^2 \qquad x_i \qquad x_i^2
$$1.2.2 Group with Curly Brackets
Group with Curly Brackets "{ }"
Example:
Without curly brackets, only the first character is raised or lowered, so x^10 shows as { }.
Where it's used: any power or index with more than one character. This is the most common beginner mistake.
$$
x^{10} \quad \text{vs} \quad x^10 \qquad x_{ij} \qquad e^{-x^2}
$$1.2.3 Primes and Training Example Notation
Primes and Nested Indices
Example:
Use ' for derivatives. In Machine Learning,
Where it's used: derivatives, and Machine Learning notation for datasets.
$$
f'(x) \qquad f''(x) \qquad x_j^{(i)}
$$1.3 Fractions
1.3.1 Basic Fraction
Fraction: "\frac{}{}"
Example:
The first { } is the numerator and the second is the denominator.
Where it's used: everywhere: rates, probabilities, averages and formulas.
$$
\frac{a}{b} \qquad \frac{x + 1}{x - 1}
$$1.3.2 Display and Text Size
Fraction Sizes: "\dfrac" and "\tfrac"
Example:
Inline:
\frac changes size depending on where it is. \dfrac always uses the full size, and \tfrac always uses the small size.
Where it's used: making fractions easier to read inside sentences and tables.
Inline: $\frac{1}{2}$ and $\dfrac{1}{2}$
$$
\frac{1}{2} \qquad \tfrac{1}{2}
$$1.3.3 Continued Fractions
Continued Fraction: "\cfrac"
Example:
\cfrac keeps every level full size, while a \frac inside a \frac gets smaller and harder to read.
Where it's used: dividing by a fraction, like the multiplicative inverse steps in the practice sheets.
$$
x = \cfrac{1}{\cfrac{9}{20}} \qquad \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{2}}}
$$1.4 Roots
1.4.1 Square Root
Square Root: "\sqrt{}"
Example:
Where it's used: standard deviation, distances between points, and vector norms.
$$
\sqrt{x} \qquad \sqrt{x^2 + y^2}
$$1.4.2 n-th Root
n-th Root: "\sqrt[n]{}"
Example:
The number in square brackets [ ] sets the root.
Where it's used: cube roots and fractional exponents.
$$
\sqrt[3]{8} = 2 \qquad x^{\frac{a}{b}} = \sqrt[b]{x^a}
$$1.5 Greek Letters Cheat Sheet
These symbols appear everywhere in Machine Learning, AI and Data Science papers.
| Symbol | Code | Common meaning in ML and statistics |
|---|---|---|
\alpha | learning rate; significance level in hypothesis tests | |
\beta | regression coefficients | |
\theta | model parameters | |
\lambda | regularisation strength; eigenvalue | |
\mu | mean | |
\sigma | standard deviation; sigmoid function | |
\Sigma | covariance matrix (not the same as the sum sign \sum) | |
\epsilon | error or noise term | |
\eta | learning rate (another common symbol) | |
\gamma | discount factor in reinforcement learning | |
\delta | a small change; error term in backpropagation | |
\Delta | change in a value, such as | |
\phi | feature map or basis function | |
\pi | the number pi; policy in reinforcement learning | |
\rho | correlation coefficient | |
\tau | time step; temperature in softmax | |
\chi | chi-squared test, written | |
\nabla | gradient | |
\partial | partial derivative |
Capital letters start with a capital: \Gamma, \Delta, \Theta, \Lambda, \Sigma, \Pi, \Phi and \Omega. Some letters also have a second shape: \varepsilon gives \varphi gives
1.6 Brackets that Resize
1.6.1 Automatic Sizing
Resizing Brackets: "\left" and "\right"
Example:
\left and \right make brackets grow to fit what is inside. They always come in pairs.
Where it's used: brackets around fractions, sums and other tall expressions.
$$
\left( \frac{a}{b} \right)^2 \quad \text{vs} \quad ( \frac{a}{b} )^2
$$1.6.2 Bracket Types
| Looks like | Code | Use it for |
|---|---|---|
( ) | grouping | |
[ ] | intervals and matrices | |
\{ \} | sets | |
\langle \rangle | inner products | |
\lvert \rvert | absolute value | |
\lVert \rVert | norms of vectors | |
\lfloor \rfloor | rounding down (floor) | |
\lceil \rceil | rounding up (ceiling) |
1.7 Operators and Relations
These are the punctuation of maths.
| Symbol | Code | Meaning |
|---|---|---|
\times | multiplication | |
\cdot | multiplication (dot) | |
\div | division | |
\pm | plus or minus | |
\neq | not equal to | |
\approx | approximately equal to | |
\equiv | identical to | |
:= | defined as, or update in an algorithm | |
\le | less than or equal to | |
\ge | greater than or equal to | |
\ll | much less than | |
\propto | proportional to | |
\sim | distributed as | |
\to | tends to, or maps to | |
\Rightarrow | implies | |
\iff | if and only if |
1.8 Big Operators with Limits
1.8.1 Sum, Product, Integral and Limit
Big Operators
Example:
Use _ for the lower limit and ^ for the upper limit.
Where it's used: totals and averages (sum), likelihoods (product), areas and probabilities (integral), and derivatives (limit).
$$
\sum_{i=1}^{n} x_i \qquad \prod_{i=1}^{n} p_i \qquad \int_{0}^{\infty} e^{-x} \, dx \qquad \lim_{x \to 0} \frac{\sin x}{x}
$$1.8.2 Limits in Inline Maths
Forcing Limits: "\limits"
Example:
Inline:
In inline maths, limits move to the side to keep the line short. \limits puts them back above and below.
Where it's used: sums inside sentences, when you want the limits in the usual place.
Inline: $\sum_{i=1}^{n} x_i$ and $\sum\limits_{i=1}^{n} x_i$1.9 Function Names
1.9.1 Built-in Function Names
Upright Function Names
Example:
Commands like \log, \ln, \exp, \sin, \cos, \tan, \max, \min and \det print upright names with correct spacing. Typing plain log gives slanted letters, as if you were multiplying
Where it's used: log-likelihood, exponential growth, and choosing the largest or smallest value.
$$
\log(x) \qquad \ln(x) \qquad \exp(x) \qquad \sin(\theta) \qquad \max(a, b) \quad \text{vs} \quad log(x)
$$1.9.2 Your Own Function Names
Custom Names: "\operatorname{}"
Example:
Use \operatorname{} for names without a built-in command. It adds the same spacing as \log.
Where it's used: Machine Learning functions such as softmax and ReLU, and statistics such as Var and Cov.
$$
\operatorname{softmax}(\mathbf{z}) \qquad \operatorname{ReLU}(x) \qquad \operatorname{Var}(X)
$$1.10 Fonts and Accents
| Looks like | Code | Use it for |
|---|---|---|
\mathbf{x} | vectors and matrices | |
\boldsymbol{\beta} | bold Greek letters | |
\mathbb{R} | number sets such as | |
\mathcal{N} | distributions and loss functions | |
\mathrm{d}x | upright letters, such as the d in | |
\hat{y} | predictions and estimates | |
\bar{x} | the mean | |
\tilde{x} | approximations or noisy values | |
\vec{v} | vectors, in physics style | |
\dot{x} | rate of change over time | |
\widehat{xy} | a hat over several letters |
1.11 Sets and Logic
1.11.1 Writing Definitions
Sets and Logic in a Formula
Example:
This reads: "for all
Where it's used: definitions, proofs, and describing datasets and feature spaces.
$$
\forall x \in \mathbb{R}, \; x^2 \ge 0 \qquad A = \{ x \in \mathbb{N} \mid x < 10 \}
$$| Symbol | Code | Meaning |
|---|---|---|
\in | is an element of | |
\notin | is not an element of | |
\subset | is a subset of | |
\subseteq | is a subset of, or equal to | |
\cup | union | |
\cap | intersection | |
\setminus | set difference | |
\emptyset | empty set | |
\forall | for all | |
\exists | there exists | |
\neg | not | |
\land | and | |
\lor | or |
1.12 Ellipses in Different Use Cases
1.12.1 Ellipsis Centered : "\cdots"
Centered Ellipsis : "\cdots"
Example Use Case: Sequences in Summation
- In the contexts where terms are arranged in a sequence horizontally (such as in summation formulas),
can be used to indicate continuation.
$$
\sum_{i=1}^n a_i = a_1 + a_2 + \cdots + a_n
$$Other Use Cases of Centered Ellipsis:
Products:
- When writing a product of several factors,
shows that the multiplication continues.
Product of several factors, for example:
- This suggests a multiplication of the terms from
to .
markdown$$ a_1 \cdot a_2 \cdots a_n $$- When writing a product of several factors,
Matrices:
- In matrix notation,
can be used to indicate the continuation of entries in a row.
Matrix Notation example:
- This shows that the entries continue horizontally.
markdown$$ \begin{bmatrix} 1 & 2 & \cdots & n \\ a & b & \cdots & z \end{bmatrix} $$- In matrix notation,
1.12.2 Ellipsis Horizontal : "\ldots"
Horizontal Ellipsis : "\ldots" for "lowered dots"
Example
$$
1, 2, \ldots, n
$$1.12.3 Ellipsis Vertical : "\vdots"
Vertical Ellipsis: "\vdots"
Example:
$$
\begin{pmatrix} a \\ \vdots \\ c \end{pmatrix}
$$1.12.4 Ellipsis Diagonal : "\ddots"
Diagonal Ellipsis: "\ddots"
Example:
This is the identity matrix \ddots shows that the 1s continue down the diagonal.
$$
I = \begin{pmatrix} 1 & 0 & \cdots & 0 \\ 0 & 1 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & 1 \end{pmatrix}
$$1.12.5 Smart Dots
Smart Dots: "\dots"
Example:
\dots chooses the height for you: low dots after commas, and centred dots between operators like \dotsc (commas), \dotsb (operators like \dotsm (multiplication) and \dotsi (integrals).
Where it's used: everyday writing, when you are not sure whether to use \ldots or \cdots.
$$
x_1, x_2, \dots, x_n \qquad x_1 + x_2 + \dots + x_n
$$1.12.6 Ellipsis in Sets, Lists and Indices
Ellipsis in Sets, Lists and Indices
Example:
Use \{ and \} for curly brackets. Plain { and } are used for grouping in LaTeX, so they do not show on the page.
Where it's used: describing a dataset as a set of samples, functions with many inputs, and index ranges in sums and loops.
$$
S = \{ x_1, x_2, \ldots, x_n \} \qquad f(x_1, \ldots, x_n) \qquad i = 1, 2, \ldots, n
$$1.12.7 Vertical Dots in a System of Equations
Vertical Dots in Equations: "\vdots"
Example:
\vdots skips the rows in the middle, while \cdots skips the terms within each row.
Where it's used: writing out a system of linear equations, which is the long form of the matrix equation
$$
\begin{aligned}
a_{11} x_1 + a_{12} x_2 + \cdots + a_{1n} x_n &= b_1 \\
a_{21} x_1 + a_{22} x_2 + \cdots + a_{2n} x_n &= b_2 \\
&\;\vdots \\
a_{m1} x_1 + a_{m2} x_2 + \cdots + a_{mn} x_n &= b_m
\end{aligned}
$$1.12.8 Which Dots Should I Use?
| Command | Looks like | Use it for | Example |
|---|---|---|---|
\ldots | lists separated by commas | ||
\cdots | between operators like | ||
\vdots | skipping rows in vectors, matrices and systems of equations | see 1.12.3 and 1.12.7 | |
\ddots | skipping along the diagonal of a matrix | see 1.12.4 | |
\dots | when unsure, it chooses the height automatically | see 1.12.5 |
1.13 Space Commands
1.13.1 Wide Space (Quad)
Space (Quad): "\quad"
Example:
\quad adds a wide space, about the width of the letter M. For smaller gaps, use \, for a thin space or \ for a normal space.
$$
x + y \quad z
$$1.13.2 Double Wide Space (Double Quad)
Double Space (Double Quad): "\qquad"
Example:
$$
x + y \qquad z
$$1.13.3 Thin, Medium and Thick Spaces
Thin, Medium and Thick Spaces
Example:
\, adds a thin space, \: a medium space and \; a thick space. They are all smaller than \quad.
Where it's used: \, before \; to give an operator like
$$
\int_{0}^{1} x^2 \, dx \qquad 9.8 \, \text{m/s}^2 \qquad \theta^{*} = \underset{\theta}{\arg\min} \; J(\theta)
$$1.13.4 Normal Space and Words in Maths
Normal Space and Words in Maths
Example:
LaTeX ignores normal spaces in maths, so typing a b shows \ (backslash and a space) for a normal space, or put the spaces inside \text{ }.
Where it's used: conditions like "if
$$
\text{if } x > 0 \qquad x \ \text{and} \ y
$$1.13.5 Negative Space
Negative Space
Example:
\! removes a thin space and pulls symbols closer together. For double integrals, \iint is a ready-made shortcut.
Where it's used: tightening a gap that looks too wide, such as between two integral signs.
$$
\int\!\!\int_{D} f(x, y) \, dx \, dy \qquad \text{vs} \qquad \int\int_{D} f(x, y) \, dx \, dy
$$1.13.6 Custom Width
Custom Width: "\hspace{}"
Example:
\hspace{} adds a space of the exact width you choose, using units such as cm, em or pt.
Where it's used: lining up symbols when none of the standard spaces fit.
$$
a \hspace{1cm} b \qquad a \hspace{2em} b
$$1.13.7 Line Breaks and Vertical Spacing
Line Breaks and Vertical Spacing
Example:
Inside aligned, \\ starts a new line, \\[2ex] adds extra space before the next line, and & lines up the equals signs.
Where it's used: step-by-step derivations and worked solutions, like the ones in the practice sheets.
$$
\begin{aligned}
f(x) &= (x + 1)^2 \\
&= x^2 + 2x + 1 \\[2ex]
f'(x) &= 2x + 2
\end{aligned}
$$1.13.8 Space Commands at a Glance
From smallest to largest. One em is about the width of the letter M.
| Command | Name | Size | Example |
|---|---|---|---|
\! | negative thin space | −3/18 em | |
| none | no space | 0 | |
\, | thin space | 3/18 em | |
\: | medium space | 4/18 em | |
\; | thick space | 5/18 em | |
\ | normal space | about 1/3 em | |
\quad | quad | 1 em | |
\qquad | double quad | 2 em |
1.14 Matrix Environments
1.14.1 Bracket Styles
Matrix Bracket Styles
Example:
& separates columns and \\ starts a new row. From left to right: matrix (no brackets), pmatrix (round), bmatrix (square), vmatrix (straight lines, for determinants) and Bmatrix (curly).
Where it's used: vectors, weight matrices and determinants. bmatrix is the most common style in Machine Learning.
$$
\begin{matrix} a & b \\ c & d \end{matrix} \quad \begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad \begin{bmatrix} a & b \\ c & d \end{bmatrix} \quad \begin{vmatrix} a & b \\ c & d \end{vmatrix} \quad \begin{Bmatrix} a & b \\ c & d \end{Bmatrix}
$$1.15 Special Characters
Some characters have a special job in LaTeX, so you need a backslash to print them.
1.15.1 Printing Special Characters
Escaping Special Characters
Example:
Where it's used: percentages, sets, and any formula that mentions these characters.
$$
\text{accuracy} = 95\% \qquad S = \{ a, b, c \} \qquad \text{Q\&A}
$$| Code | Prints | Why it needs a backslash |
|---|---|---|
\% | percent sign | % starts a comment in LaTeX |
\$ | dollar sign | a dollar sign starts and ends maths |
\_ | underscore | _ makes a subscript |
\# | hash sign | # is used inside commands |
\& | ampersand | & separates columns |
\{ \} | curly brackets | { } group things together |
\backslash | backslash | \ starts every command |
1.16 Boxes, Colour and Equation Numbers
1.16.1 Box a Final Answer
Boxed Answer: "\boxed{}"
Example:
Where it's used: highlighting the final answer of a worked solution.
$$
15 \cdot \frac{4}{13} \cdot \frac{26}{15} = \boxed{8}
$$1.16.2 Colour
Colour: "\color{}"
Example:
Put \color{} and the part you want to colour inside { }, so the colour stops there.
Where it's used: pointing out the part of a formula you are explaining.
$$
f(x) = {\color{red} 3}x^2 + {\color{blue} 2}x + 1
$$1.16.3 Equation Numbers
Equation Number: "\tag{}"
Example:
Where it's used: numbering important equations so you can refer back to them, such as "from Equation (1)".
$$
\hat{y} = \mathbf{w}^{\top} \mathbf{x} + b \tag{1}
$$More LaTeX Quick Code
Ready for formulas? Each subject has its own page: