Skip to content
Kittycat.tech logo

Coding Guide in MathJax (LaTeX)

Contributors:
Erica Madebeykin, MBA
Erica Madebeykin, MBA
🇬🇧MBA Merit
🇬🇧MSc Comp Sci student
Start Creation Date: October 24, 2024 🔄 Continuously updated

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 ​

GroupSectionWhat you'll learnKey commands
Getting started1.1 Inline vs Display Mathsmaths inside a sentence or on its own line$ $, $$ $$
1.2 Superscripts and Subscriptspowers, indices and grouping^, _, { }
1.3 Fractionsfractions in every size\frac, \dfrac, \tfrac, \cfrac
1.4 Rootssquare roots and n-th roots\sqrt{}, \sqrt[n]{}
Building blocks1.5 Greek Letterssymbols used in ML and statistics\alpha, \theta, \sigma, \lambda
1.6 Brackets that Resizebrackets that fit tall formulas\left( \right), \{ \}, \lvert \rvert
1.7 Operators and Relationsthe punctuation of maths\times, \le, \neq, \approx
1.8 Big Operators with Limitssums, products, integrals and limits\sum, \prod, \int, \lim
1.9 Function Namesupright names such as log and max\log, \exp, \operatorname{}
1.10 Fonts and Accentsbold vectors, hats and bars\mathbf, \mathbb, \hat, \bar
1.11 Sets and Logicwriting definitions\in, \forall, \cup, \mid
Layout1.12 Ellipsesthe different kinds of dots\ldots, \cdots, \vdots, \ddots
1.13 Space Commandsspaces from tiny to wide, and line breaks\,, \;, \quad, \\
1.14 Matrix Environmentsmatrix bracket stylespmatrix, bmatrix, vmatrix
1.15 Special Charactersprinting symbols such as the percent and dollar signs\%, \$, \_, \&
1.16 Boxes, Colour and Equation Numbersboxed 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 πr2, where r is the radius.

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.

markdown
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:

A=πr2

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.

markdown
$$
A = \pi r^2
$$

1.2 Superscripts and Subscripts ​

1.2.1 Superscripts and Subscripts ​

Superscript "^" and Subscript "_"

Example:

x2xixi2

^ 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 i-th value xi.

markdown
$$
x^2 \qquad x_i \qquad x_i^2
$$

1.2.2 Group with Curly Brackets ​

Group with Curly Brackets "{ }"

Example:

x10vsx10xije−x2

Without curly brackets, only the first character is raised or lowered, so x^10 shows as x10. Put anything longer than one character inside { }.

Where it's used: any power or index with more than one character. This is the most common beginner mistake.

markdown
$$
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:

f′(x)f″(x)xj(i)

Use ' for derivatives. In Machine Learning, xj(i) is the j-th feature of the i-th training example.

Where it's used: derivatives, and Machine Learning notation for datasets.

markdown
$$
f'(x) \qquad f''(x) \qquad x_j^{(i)}
$$

1.3 Fractions ​

1.3.1 Basic Fraction ​

Fraction: "\frac{}{}"

Example:

abx+1x−1

The first { } is the numerator and the second is the denominator.

Where it's used: everywhere: rates, probabilities, averages and formulas.

markdown
$$
\frac{a}{b} \qquad \frac{x + 1}{x - 1}
$$

1.3.2 Display and Text Size ​

Fraction Sizes: "\dfrac" and "\tfrac"

Example:

Inline: 12 and 12

1212

\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.

markdown
Inline: $\frac{1}{2}$ and $\dfrac{1}{2}$

$$
\frac{1}{2} \qquad \tfrac{1}{2}
$$

1.3.3 Continued Fractions ​

Continued Fraction: "\cfrac"

Example:

x=192011+11+12

\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.

markdown
$$
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:

xx2+y2

Where it's used: standard deviation, distances between points, and vector norms.

markdown
$$
\sqrt{x} \qquad \sqrt{x^2 + y^2}
$$

1.4.2 n-th Root ​

n-th Root: "\sqrt[n]{}"

Example:

83=2xab=xab

The number in square brackets [ ] sets the root.

Where it's used: cube roots and fractional exponents.

markdown
$$
\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.

SymbolCodeCommon meaning in ML and statistics
α\alphalearning rate; significance level in hypothesis tests
β\betaregression coefficients
θ\thetamodel parameters
λ\lambdaregularisation strength; eigenvalue
μ\mumean
σ\sigmastandard deviation; sigmoid function
Σ\Sigmacovariance matrix (not the same as the sum sign \sum)
ϵ\epsilonerror or noise term
η\etalearning rate (another common symbol)
γ\gammadiscount factor in reinforcement learning
δ\deltaa small change; error term in backpropagation
Δ\Deltachange in a value, such as Δx
ϕ\phifeature map or basis function
π\pithe number pi; policy in reinforcement learning
ρ\rhocorrelation coefficient
τ\tautime step; temperature in softmax
χ\chichi-squared test, written χ2
∇\nablagradient
∂\partialpartial 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 ε and \varphi gives φ.

1.6 Brackets that Resize ​

1.6.1 Automatic Sizing ​

Resizing Brackets: "\left" and "\right"

Example:

(ab)2vs(ab)2

\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.

markdown
$$
\left( \frac{a}{b} \right)^2 \quad \text{vs} \quad ( \frac{a}{b} )^2
$$

1.6.2 Bracket Types ​

Looks likeCodeUse it for
(x)( )grouping
[x][ ]intervals and matrices
{x}\{ \}sets
⟨x,y⟩\langle \rangleinner products
|x|\lvert \rvertabsolute value
‖x‖\lVert \rVertnorms of vectors
⌊x⌋\lfloor \rfloorrounding down (floor)
⌈x⌉\lceil \rceilrounding up (ceiling)

1.7 Operators and Relations ​

These are the punctuation of maths.

SymbolCodeMeaning
×\timesmultiplication
⋅\cdotmultiplication (dot)
÷\divdivision
±\pmplus or minus
≠\neqnot equal to
≈\approxapproximately equal to
≡\equividentical to
:=:=defined as, or update in an algorithm
≤\leless than or equal to
≥\gegreater than or equal to
≪\llmuch less than
∝\proptoproportional to
∼\simdistributed as
→\totends to, or maps to
⇒\Rightarrowimplies
⟺\iffif and only if

1.8 Big Operators with Limits ​

1.8.1 Sum, Product, Integral and Limit ​

Big Operators

Example:

∑i=1nxi∏i=1npi∫0∞e−xdxlimx→0sin⁡xx

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).

markdown
$$
\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: ∑i=1nxi and ∑i=1nxi

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.

markdown
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:

log⁡(x)ln⁡(x)exp⁡(x)sin⁡(θ)max(a,b)vslog(x)

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 l, o and g.

Where it's used: log-likelihood, exponential growth, and choosing the largest or smallest value.

markdown
$$
\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:

softmax(z)ReLU(x)Var(X)

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.

markdown
$$
\operatorname{softmax}(\mathbf{z}) \qquad \operatorname{ReLU}(x) \qquad \operatorname{Var}(X)
$$

1.10 Fonts and Accents ​

Looks likeCodeUse it for
x\mathbf{x}vectors and matrices
β\boldsymbol{\beta}bold Greek letters
R\mathbb{R}number sets such as R, N and Z
N\mathcal{N}distributions and loss functions
dx\mathrm{d}xupright letters, such as the d in dx
y^\hat{y}predictions and estimates
x¯\bar{x}the mean
x~\tilde{x}approximations or noisy values
v→\vec{v}vectors, in physics style
x˙\dot{x}rate of change over time
xy^\widehat{xy}a hat over several letters

1.11 Sets and Logic ​

1.11.1 Writing Definitions ​

Sets and Logic in a Formula

Example:

∀x∈R,x2≥0A={x∈N∣x<10}

This reads: "for all x in the real numbers, x2 is at least 0", and "A is the set of natural numbers less than 10".

Where it's used: definitions, proofs, and describing datasets and feature spaces.

markdown
$$
\forall x \in \mathbb{R}, \; x^2 \ge 0 \qquad A = \{ x \in \mathbb{N} \mid x < 10 \}
$$
SymbolCodeMeaning
∈\inis an element of
∉\notinis not an element of
⊂\subsetis a subset of
⊆\subseteqis a subset of, or equal to
∪\cupunion
∩\capintersection
∖\setminusset difference
∅\emptysetempty set
∀\forallfor all
∃\existsthere exists
¬\negnot
∧\landand
∨\loror

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.
∑i=1nai=a1+a2+⋯+an
markdown
 $$
 \sum_{i=1}^n a_i = a_1 + a_2 + \cdots + a_n
 $$

Other Use Cases of Centered Ellipsis:

  1. Products:

    • When writing a product of several factors, ⋯ shows that the multiplication continues.

    Product of several factors, for example:

    a1⋅a2⋯an
    • This suggests a multiplication of the terms from a1 to an.
    markdown
     $$
     a_1 \cdot a_2 \cdots a_n
     $$
  2. Matrices:

    • In matrix notation, ⋯ can be used to indicate the continuation of entries in a row.

    Matrix Notation example:

    [12⋯nab⋯z]
    • This shows that the entries continue horizontally.
    markdown
     $$
     \begin{bmatrix}
     1 & 2 & \cdots & n \\
     a & b & \cdots & z
     \end{bmatrix}
     $$

1.12.2 Ellipsis Horizontal : "\ldots" ​

Horizontal Ellipsis : "\ldots" for "lowered dots"

Example

1,2,…,n
markdown
 $$
 1, 2, \ldots, n
 $$

1.12.3 Ellipsis Vertical : "\vdots" ​

Vertical Ellipsis: "\vdots"

Example:

(a⋮c)
markdown
 $$
 \begin{pmatrix} a \\ \vdots \\ c \end{pmatrix}
 $$

1.12.4 Ellipsis Diagonal : "\ddots" ​

Diagonal Ellipsis: "\ddots"

Example:

I=(10⋯001⋯0⋮⋮⋱⋮00⋯1)

This is the identity matrix I. \ddots shows that the 1s continue down the diagonal.

markdown
 $$
 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:

x1,x2,…,xnx1+x2+⋯+xn

\dots chooses the height for you: low dots after commas, and centred dots between operators like +. For full control, you can also use \dotsc (commas), \dotsb (operators like + and =), \dotsm (multiplication) and \dotsi (integrals).

Where it's used: everyday writing, when you are not sure whether to use \ldots or \cdots.

markdown
$$
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:

S={x1,x2,…,xn}f(x1,…,xn)i=1,2,…,n

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.

markdown
$$
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:

a11x1+a12x2+⋯+a1nxn=b1a21x1+a22x2+⋯+a2nxn=b2⋮am1x1+am2x2+⋯+amnxn=bm

\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 Ax=b in Linear Algebra.

markdown
$$
\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? ​

CommandLooks likeUse it forExample
\ldots…lists separated by commasx1,x2,…,xn
\cdots⋯between operators like +, ⋅ and =a1+a2+⋯+an
\vdots⋮skipping rows in vectors, matrices and systems of equationssee 1.12.3 and 1.12.7
\ddots⋱skipping along the diagonal of a matrixsee 1.12.4
\dots…when unsure, it chooses the height automaticallysee 1.12.5

1.13 Space Commands ​

1.13.1 Wide Space (Quad) ​

Space (Quad): "\quad"

Example:

x+yz

\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.

markdown
 $$
 x + y \quad z
 $$

1.13.2 Double Wide Space (Double Quad) ​

Double Space (Double Quad): "\qquad"

Example:

x+yz
markdown
 $$
 x + y \qquad z
 $$

1.13.3 Thin, Medium and Thick Spaces ​

Thin, Medium and Thick Spaces

Example:

∫01x2dx9.8m/s2θ∗=arg⁡minθJ(θ)

\, adds a thin space, \: a medium space and \; a thick space. They are all smaller than \quad.

Where it's used: \, before dx in integrals and between a number and its unit, and \; to give an operator like arg⁡min some room.

markdown
$$
\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:

if x>0x and y

LaTeX ignores normal spaces in maths, so typing a b shows ab. Use \ (backslash and a space) for a normal space, or put the spaces inside \text{ }.

Where it's used: conditions like "if x>0" in piecewise functions, and short words such as "and" or "for all" inside formulas.

markdown
$$
\text{if } x > 0 \qquad x \ \text{and} \ y
$$

1.13.5 Negative Space ​

Negative Space

Example:

∫∫Df(x,y)dxdyvs∫∫Df(x,y)dxdy

\! 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.

markdown
$$
\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:

abab

\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.

markdown
$$
a \hspace{1cm} b \qquad a \hspace{2em} b
$$

1.13.7 Line Breaks and Vertical Spacing ​

Line Breaks and Vertical Spacing

Example:

f(x)=(x+1)2=x2+2x+1f′(x)=2x+2

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.

markdown
$$
\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.

CommandNameSizeExample
\!negative thin space−3/18 emab
noneno space0ab
\,thin space3/18 emab
\:medium space4/18 emab
\;thick space5/18 emab
\ normal spaceabout 1/3 ema b
\quadquad1 emab
\qquaddouble quad2 emab

1.14 Matrix Environments ​

1.14.1 Bracket Styles ​

Matrix Bracket Styles

Example:

abcd(abcd)[abcd]|abcd|{abcd}

& 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.

markdown
$$
\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:

accuracy=95%S={a,b,c}Q&A

Where it's used: percentages, sets, and any formula that mentions these characters.

markdown
$$
\text{accuracy} = 95\% \qquad S = \{ a, b, c \} \qquad \text{Q\&A}
$$
CodePrintsWhy it needs a backslash
\%percent sign% starts a comment in LaTeX
\$dollar signa dollar sign starts and ends maths
\_underscore_ makes a subscript
\#hash sign# is used inside commands
\&ampersand& separates columns
\{ \}curly brackets{ } group things together
\backslashbackslash\ starts every command

1.16 Boxes, Colour and Equation Numbers ​

1.16.1 Box a Final Answer ​

Boxed Answer: "\boxed{}"

Example:

15⋅413⋅2615=8

Where it's used: highlighting the final answer of a worked solution.

markdown
$$
15 \cdot \frac{4}{13} \cdot \frac{26}{15} = \boxed{8}
$$

1.16.2 Colour ​

Colour: "\color{}"

Example:

f(x)=3x2+2x+1

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.

markdown
$$
f(x) = {\color{red} 3}x^2 + {\color{blue} 2}x + 1
$$

1.16.3 Equation Numbers ​

Equation Number: "\tag{}"

Example:

(1)y^=w⊤x+b

Where it's used: numbering important equations so you can refer back to them, such as "from Equation (1)".

markdown
$$
\hat{y} = \mathbf{w}^{\top} \mathbf{x} + b \tag{1}
$$

More LaTeX Quick Code ​

Ready for formulas? Each subject has its own page:

✅Study notes based on Kittycat's insights from the best online courses at top universities