Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Writing in Markdown and LaTeX

Introduction

LaTeXis a special system of codes used to write mathematical formulas and equations, commonly used in writing papers and also available in Jupyter notebooks. Learning to use LaTeXto be able to typeset equations is a crucial skill for many scientific and engineering fields. However, it is also famously unintuitive. This is a short guide to try to make things easier.

The basics

There are two different basic types of math formatting you can achieve in LaTeX. The first type is called inline formatting and gives you smaller-sized equations (inline equations) that flow inline with text, such as E=m0c2E = m_0 c^2. The second type is called block formatting and gives you large equations (block equations) that look like this:

f(z0)=12πif(z)zz0dzf(z_0) = \frac{1}{2\pi i} \oint \frac{f(z)}{z - z_0} dz

To write inline equations, we put our LaTeX code within single dollar signs $...$. For instance, the code for x=1x = 1 is $x = 1$. If you type this into a Jupyter notebook cell (with the dollar signs) and then press Shift + Enter, you’ll be able to see the rendered result! Double-clicking on the cell again shows you the code again, so you can edit later.

To write block equations, we put our LaTeX code within double dollar signs $$...$$. For instance, the code $$x = 1$$ gives you an equation that gets its own line on the page:

x=1x = 1

Note that you can also put long block equations on several lines to make it more readable. For instance, consider this equation:

x=1+2+3+4+5+6+7+8+9+10x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

Notice that this is a rather long equation and if we write everything in a line it might be quite verbose and hard to read. A better way is to spread out the code (between the double dollar signs) over multiple lines, since newlines don’t matter for LaTeX code by default. You can, for instance, write the LaTeX code like this:

$$
x = 1 + 2 + 3
    + 4 + 5 
    + 6 + 7 
    + 8 + 9 
    + 10
$$

Basic algebra & symbols

In LaTeX, most basic symbols (like letters of the alphabet, as well as the arithmetic operations +,,/,>,<,=+, -, /, >, <, = , and numbers) are typed the same way they look. For instance, just as we have seen, x=y=z=t=42x = y = z = t = 42 is just typed $x = y = z = t = 42$ in LaTeX code. However, it is also common to use Greek letters in mathematical equations. LaTeX offers special codes to represent these letters: for instance, π=3.14159...\pi = 3.14159... is typed with the code $\pi = 3.14159...$, where \pi is the special code to represent the Greek letter π\pi. Be aware that LaTeX codes are in general case-sensitive! The following table shows a selection of the symbols used for Greek letters:

LetterLaTeX code (placed between dollar signs)
α\alpha\alpha
β\beta\beta
Γ\Gamma\Gamma
γ\gamma\gamma
Δ\Delta\Delta
δ\delta\delta
ϵ\epsilon\epsilon
ε\varepsilon\varepsilon
ζ\zeta\zeta
η\eta\eta
Θ\Theta\Theta
θ\theta\theta
κ\kappa\kappa
Λ\Lambda\Lambda
λ\lambda\lambda
LetterLaTeX code (placed between dollar signs)
M\mathcal{M}\mathcal{M}
μ\mu\mu
ν\nu\nu
Ξ\Xi\Xi
ξ\xi\xi
Π\Pi\Pi
π\pi\pi
ρ\rho\rho
Σ\Sigma\Sigma
σ\sigma\sigma
ς\varsigma\varsigma
τ\tau\tau
Υ\Upsilon\Upsilon
Φ\Phi\Phi
ϕ\phi\phi
φ\varphi\varphi
χ\chi\chi
Ψ\Psi\Psi
ψ\psi\psi
Ω\Omega\Omega
ω\omega\omega

In addition, a few symbols commonly used in physics with unusual or stylized forms are shown with their LaTeX codes below:

LetterLaTeX code (placed between dollar signs)
\hbar\hbar
L\mathcal{L}\mathcal{L}
L\mathscr{L}\mathscr{L}
H\mathcal{H}\mathcal{H}
H\mathscr{H}\mathscr{H}
R\mathbb{R}\mathbb{R}
C\mathbb{C}\mathbb{C}
E\mathbb{E}\mathbb{E}
Q\mathbb{Q}\mathbb{Q}
Z\mathbb{Z}\mathbb{Z}

Powers, subcripts, and superscripts

For superscripts, use the ^{...} (caret) symbol; for instance, x2x^2 is typeset as $x^{2}$. For subscripts, use the _{...} (underscore) symbol; for instance, x2x_{2} is typeset as $x_{2}$. (Note that including the {,} curly braces is not always necessary but it’s usually a good idea to use them). For ellipses (\dots), use the \dots command. This, for instance, allows us to write equations like:

a0x3+a1x2+a2x+a3+=0a_0 x^3 + a_1 x^2 + a_2 x + a_3 + \dots = 0

Which uses the code:

a_{0} x^{3} + a_{1} x^{2} + a_{2} x + a_{3} + \dots = 0

Fractions and radicals

Fractions in LaTeXuse the \frac{...}{...} command, where the numerator is placed between the first set of braces and the denominator is placed between the second set of braces. As an example, the code \frac{1}{2} renders as 12\frac{1}{2}. Since the fraction has 1 in the numerator and 2 in the denominator, we have {1}{2} after \frac (in LaTeX the numerator is always written before the denominator, so \frac{2}{1} would be wrong!). We can write more complicated fractions too, like this:

Y=1+2+7/57(9)+5325+3600Y = \frac{1 + 2 + 7/5}{7(9) + 53 - 25} + 3600

The LaTeX code corresponding to it is:

Y = \frac{1 + 2 + 7/5}{7(9) + 53 - 25}
      + 3600

Writing a radical (for instance, a square root) is accomplished using one of two ways. The first way is to use the \sqrt{...} command. Thus, \sqrt{2} is rendered as 2\sqrt{2}. For n-th roots, we use the \sqrt[n]{...} command, so \sqrt[n}{2} is rendered as 2n\sqrt[n]{2}. If we set n=3n = 3 then the code \sqrt[3]{2} is then shown as 23\sqrt[3]{2}.

The other way is to use a fraction as an exponent. For instance, 1+x\sqrt{1 + x} can also be written as (1+x)1/2(1 + x)^{1/2} (in code, (1 + x)^{1/2}) or as (1+x)12(1 + x)^{\frac{1}{2}} (in code, (1 + x)^{\frac{1}{2}}).

Uncertainties and inequalities

In many mathematical equations, we aim to describe quantities that are not exactly equal to each other or lie in a particular range of uncertainty. For instance, we might find that a measurement is 1±0.0051 \pm 0.005 centimeters. For this, we use specialized LaTeXsymbols, as shown in the below table:

SymbolLaTeX code (placed between dollar signs)
±\pm
\mp\mp
\approx\approx
\sim\sim
\propto\propto
\geq\geq
\leq\leq
\neq\neq

Combining what we’ve seen so far, we can now typeset the quadratic formula in LaTeX code:

x=b±b24ac2ax = \frac{b \pm \sqrt{b^2 - 4ac}}{2a}

The LaTeX code is:

x = \frac{b \pm \sqrt{b^2 - 4ac}}{2a}

Trigonometric and logarithmic functions

To go beyond basic algebra with purely polynomials, LaTeXoffers us extensive abilities to typeset transcendental functions, like trigonometric and logarithmic functions, as shown by the table below:

SymbolLaTeX code (placed between dollar signs)
exe^xe^x
ln(x)\ln (x)\ln (x)
log(x)\log (x)\log (x)
$\log_{10}(x)\log_{10} (x)
sin(x)\sin (x)\sin (x)
cos(x)\cos (x)\cos (x)
tan(x)\tan (x)\tan (x)
csc(x)\csc (x)\csc (x)
sec(x)\sec (x)\sec (x)
cot(x)\cot (x)\cot (x)

We can now type out the famous Euler’s formula in LaTeX:

eiϕ=cos(ϕ)+isin(ϕ)e^{i\phi} = \cos (\phi) + i \sin (\phi)

With the LaTeX code being:

e^{i\phi} = \cos (\phi) + i \sin (\phi)

It is also possible to write out the inverse trigonometric functions with, for instance, \sin^{-1}(x) for sin1(x)\sin^{-1}(x) (inverse sine) and \cos^{-1}(x) for cos1(x)\cos^{-1}(x) (inverse cosine). The other inverse trigonometric functions are also very similar. The hyperbolic trigonometric functions, used in advanced engineering and physics, can also be typed out in a similar way: \sinh(x) for sinh(x)\sinh(x) (hyperbolic sine), \cosh(x) for cosh(x)\cosh(x) (hyperbolic cosine), \tanh(x) for tanh(x)\tanh(x) (hyperbolic tangent), and so on.

Calculus

LaTeXoffers rich support for calculus-based symbols. For instance, the \lim_{...} command allows us to write a limit of a function. For instance, the following code:

\lim_{x \to a} f(x) = f(a)

displays as:

limxaf(x)=f(a)\lim_{x \to a} f(x) = f(a)

We can also write an infinite sum using the \sum_{...}^{...} command, where the lower bound of the sum is between the first 2 braces and the upper bound is between the second 2 braces. For instance:

\sum_{n = 1}^{30} \frac{1}{n!}

displays as:

n=1301n!\sum_{n = 1}^{30} \frac{1}{n!}

We can indeed omit the bounds so we just have:

\sum_n \frac{1}{n!}

which displays as:

n1n!\sum_n \frac{1}{n!}

Derivatives are generally just written as fractions in LaTeX, for instance:

f'(x) = \frac{df}{dx}

displays as:

f(x)=dfdxf'(x) = \frac{df}{dx}

Note that for time derivatives like dfdt\dfrac{df}{dt} one alternative is to use Newton’s dot notation f˙\dot f and f¨\ddot f, which correspond to the codes \dot f and \ddot f.

Integrals, by contrast, use a more specialized notation. The \int_{...}^{...} command creates an integral. If we do not specify the bounds, for instance, with the code \int f(x) dx, we just get:

f(x)dx\int f(x) dx

But if we do specify the bounds, for instance, with the code \int_{a}^{b} f(x) dx, we get:

abf(x)dx\int_{a}^{b} f(x) dx

To write the evaluation bounds of an antiderivative, we use the \bigg command. For instance, the code:

F(x)\bigg|_{a}^{b}

displays as shown:

F(x)abF(x)\bigg|_{a}^{b}

To notate a partial derivative, we use the f/x\partial f/\partial x symbol rather than df/dxdf/dx, which is notated as $\partial f/\partial x$. Thus we have:

fx=fx\dfrac{\partial f}{\partial x} = f_x

A double integral is denoted \iint, and a triple integral denoted \iiint. They display as follows:

f(x,y)dA,g(x,y,z)dV\iint f(x, y) dA, \quad \iiint g(x, y, z) dV

In addition, you can write a closed line integral with \oint, which displays as follows:

f(r)dr\oint f(r) dr

The gradient of a function f\nabla f is coded as \nabla f, the divergence \nabla \cdot is coded as \nabla \cdot, and the curl ×\nabla \times is coded as \nabla \times. The Laplacian 2\nabla^2 is written as \nabla^2.

Vectors, arrows, lines, and decorative symbols

Much of the rest of LaTeXis figuring out other symbols useful (although not always essential) for typesetting a particular symbol, of which some are shown in the table below:

SymbolLaTeX code (placed between dollar signs)
a\vec a\vec a
ab\vec a \cdot \vec b\vec a \cdot \vec b
X\mathbf{X}\mathbf{X}
a^\hat a\hat a
a~\tilde a\tilde a
aa^\daggera^\dagger
ab\overline{ab}\overline{ab}
ab\overrightarrow{ab}\overrightarrow{ab}
ab\overleftarrow{ab}\overleftarrow{ab}
abc\cancel{abc}\cancel{abc}
\Rightarrow\Rightarrow
\rightarrow\rightarrow

More information

This guide introduces a very small subset of LaTeX. For more information, I suggest visiting the following sites:

  1. For a full list of LaTeX codes for symbols, see https://oeis.org/wiki/List_of_LaTeX_mathematical_symbols

  2. For a website where you can draw symbols and it tells you the LaTeX code, see https://detexify.kirelabs.org/

  3. For a complete, in-depth guide to LaTeX, see the tutorial by Overleaf at https://www.overleaf.com/learn

Note also that copying the contents of any math cell from the free graphing/scientific calculator Desmos (https://desmos.com/calculator) will copy the results in LaTeX code that you can later paste in a Jupyter notebook, so it can be used (in a pinch!) as a visual equation editor that "exports" to LaTeX.