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 . The second type is called block formatting and gives you large equations (block equations) that look like this:
To write inline equations, we put our LaTeX code within single dollar
signs $...$. For instance, the code for 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:
Note that you can also put long block equations on several lines to make it more readable. For instance, consider this equation:
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, 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, is typed with
the code $\pi = 3.14159...$, where \pi is the special code to
represent the Greek letter . Be aware that LaTeX codes are in
general case-sensitive! The following table shows a selection of the
symbols used for Greek letters:
| Letter | LaTeX code (placed between dollar signs) |
|---|---|
\alpha | |
\beta | |
\Gamma | |
\gamma | |
\Delta | |
\delta | |
\epsilon | |
\varepsilon | |
\zeta | |
\eta | |
\Theta | |
\theta | |
\kappa | |
\Lambda | |
\lambda |
| Letter | LaTeX code (placed between dollar signs) |
|---|---|
\mathcal{M} | |
\mu | |
\nu | |
\Xi | |
\xi | |
\Pi | |
\pi | |
\rho | |
\Sigma | |
\sigma | |
\varsigma | |
\tau | |
\Upsilon | |
\Phi | |
\phi | |
\varphi | |
\chi | |
\Psi | |
\psi | |
\Omega | |
\omega |
In addition, a few symbols commonly used in physics with unusual or stylized forms are shown with their LaTeX codes below:
| Letter | LaTeX code (placed between dollar signs) |
|---|---|
\hbar | |
\mathcal{L} | |
\mathscr{L} | |
\mathcal{H} | |
\mathscr{H} | |
\mathbb{R} | |
\mathbb{C} | |
\mathbb{E} | |
\mathbb{Q} | |
\mathbb{Z} |
Powers, subcripts, and superscripts¶
For superscripts, use the ^{...} (caret) symbol; for instance,
is typeset as $x^{2}$. For subscripts, use the _{...} (underscore)
symbol; for instance, 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 (), use the \dots
command. This, for instance, allows us to write equations like:
Which uses the code:
a_{0} x^{3} + a_{1} x^{2} + a_{2} x + a_{3} + \dots = 0Fractions 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 . 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:
The LaTeX code corresponding to it is:
Y = \frac{1 + 2 + 7/5}{7(9) + 53 - 25}
+ 3600Writing 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 . For n-th roots, we use
the \sqrt[n]{...} command, so \sqrt[n}{2} is rendered as
. If we set then the code \sqrt[3]{2} is then
shown as .
The other way is to use a fraction as an exponent. For instance,
can also be written as (in code,
(1 + x)^{1/2}) or as (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 centimeters. For this, we use specialized LaTeXsymbols, as shown in the below table:
| Symbol | LaTeX code (placed between dollar signs) |
|---|---|
| ± | \pm |
\mp | |
\approx | |
\sim | |
\propto | |
\geq | |
\leq | |
\neq |
Combining what we’ve seen so far, we can now typeset the quadratic formula in LaTeX code:
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:
| Symbol | LaTeX code (placed between dollar signs) |
|---|---|
e^x | |
\ln (x) | |
\log (x) | |
| $\log_{10}(x) | \log_{10} (x) |
\sin (x) | |
\cos (x) | |
\tan (x) | |
\csc (x) | |
\sec (x) | |
\cot (x) |
We can now type out the famous Euler’s formula in LaTeX:
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 (inverse sine) and
\cos^{-1}(x) for (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
(hyperbolic sine), \cosh(x) for (hyperbolic cosine),
\tanh(x) for (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:
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:
We can indeed omit the bounds so we just have:
\sum_n \frac{1}{n!}which displays as:
Derivatives are generally just written as fractions in LaTeX, for instance:
f'(x) = \frac{df}{dx}displays as:
Note that for time derivatives like one alternative is
to use Newton’s dot notation and , 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:
But if we do specify the bounds, for instance, with the code
\int_{a}^{b} f(x) dx, we get:
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:
To notate a partial derivative, we use the
symbol rather than , which is notated as
$\partial f/\partial x$. Thus we have:
A double integral is denoted \iint, and a triple integral denoted
\iiint. They display as follows:
In addition, you can write a closed line integral with \oint, which displays as follows:
The gradient of a function is coded as \nabla f, the
divergence is coded as \nabla \cdot, and the curl
is coded as \nabla \times. The Laplacian 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:
| Symbol | LaTeX code (placed between dollar signs) |
|---|---|
\vec a | |
\vec a \cdot \vec b | |
\mathbf{X} | |
\hat a | |
\tilde a | |
a^\dagger | |
\overline{ab} | |
\overrightarrow{ab} | |
\overleftarrow{ab} | |
\cancel{abc} | |
\Rightarrow | |
\rightarrow |
More information¶
This guide introduces a very small subset of LaTeX. For more information, I suggest visiting the following sites:
For a full list of LaTeX codes for symbols, see https://
oeis .org /wiki /List _of _LaTeX _mathematical _symbols For a website where you can draw symbols and it tells you the LaTeX code, see https://
detexify .kirelabs .org/ 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://