Writing mathematics

Overview:

  • Teaching: 30 min
  • Exercises: 10 min

Objectives

  • To introduce syntax that will enable you to write mathematical expressions
  • Suggest some commonly used environments to help control the display of equations

Getting started

As you have seen in the last lesson, we use the amsmath package for writing mathematics. Make sure you have loaded that package into your preamble, that is you have the command

\usepackage{amsmath}

between your \documentclass and \begin{document} commands!

Mathematical Mode

Before we start writing mathematical commands in $\LaTeX$, we have to tell it that's what we are doing!

$\LaTeX$ allows us to writing mathematical expressions in two different modes: inline and display. Inline mode will insert the expression you write into the line of the text, whilst display mode will give the equation it's own line. As such; display mode is useful for drawing attention to a given formula like in a definition, displaying long formulas, or displaying equations with fractions or superscripts that might get cluttered into the rest of the text. Inline mode is much more useful for referring to variables by name in the text: if you have a variable $x$, you don't want a new line every time you need to refer to $x$!

To tell $\LaTeX$ we want to type some maths, we use dollar signs $ to enclose the expression. Let's see an example of the inline mode:

In [1]:
%%pdflatex
In physics, the mass-energy equivalence of a particle at rest is given by the equation $E=mc^2$, 
first postulated in 1905 by Albert Einstein. 
Here $m$ is the particle mass, $E$ it's equivalent energy, and $c$ is the speed of light.
Out[1]:

Notice how the equation is placed inside the sentence - it will also automatically break onto a new line if the sentence is too long, and for all intents and purposes the letters in the equation will act like letters in the sentence. We can also put equations in inline mode using \begin{math} \end{math} too, if you prefer to make your source code more explicit. Enclosing the expression in dollar signs $ is also sufficient, faster, and common practice, but ultimately it's just a matter of taste.

However you might decide that such an important equation is deserving of more attention, or might not like how close the power of two gets to the line above the equation in the paragraph. In this case, we can use display mode: we type exactly the same thing into the math environment, but this time use a double-dollar sign $$ to enclose it.

In [2]:
%%pdflatex
In physics, the mass-energy equivalence of a particle at rest is given by the equation $$E=mc^2,$$ 
first postulated in 1905 by Albert Einstein. 
Here $m$ is the particle mass, $E$ it's equivalent energy, and $c$ is the speed of light.
Out[2]:

We could also use an equation environment to create a display environment, again this comes down to personal choice. Note that using an environment will automatically give the displayed formula a number, but we can suppress this if we want, as we will see later in this lesson.

In [3]:
%%pdflatex
In physics, the mass-energy equivalence of a particle at rest is given by the equation 
\begin{equation}
E=mc^2,
\end{equation}
first postulated in 1905 by Albert Einstein. 
Here $m$ is the particle mass, $E$ it's equivalent energy, and $c$ is the speed of light.
Out[3]:

Useful Convention

Whilst there is no set convention, a good compromise in terms of styling your source code and ensuring readability is to use dollar signs for inline equations, and use environments for displayed formulae. This means that when scanning your source code, you can quickly find where you are writing your important formulae, and conversely you can read the prose in your source code as if it were a paragraph.

In [4]:
%%pdflatex
In the examples above, I display
\begin{equation}
    E = mc^2,
\end{equation}
as a formula, because it's important and doesn't really fit well inline: 
$E=mc^2$ is hard to spot and I can't give it an equation number.
Conversely, I don't need to use a display environment to define each variable $E$, $m$, and $c$ that appears.
I definitely don't want to have a new line appear every time I refer back to 
\begin{equation}
    E,
\end{equation}
\begin{equation}
    m,
\end{equation}
and
\begin{equation}
    c
\end{equation}
either!
Out[4]:

Equation Content

Subscripts and superscripts

In the examples above we used the carrot ^ symbol to inform $\LaTeX$ that we wanted to have a superscript on the letter $c$ in $mc^2$. We can also tell $\LaTeX$ to write subscripts by using an underscore _ after the symbol we want to attribute the subscript to, like below:

In [5]:
%%pdflatex
Suppose we have a real sequence $x_n$ such that $x_n \rightarrow 0$ as $n \rightarrow \infty$.
Out[5]:

And we can give both a super and subscript to symbols by giving both an _ and a ^ symbol after it:

In [6]:
%%pdflatex
The result of our main theorem follows trivially having computing the value of the integral
$$ \int_0^1 e^x dx = e-1. $$
Out[6]:

Notice how I said symbol - the integral symbol (\int) is a symbol, so we can tell $\LaTeX$ to give it a superscript and subscript! However we need to be more careful when our superscripts or subscripts are more than one character long themselves - look at what happens if we render the source code below:

In [7]:
%%pdflatex
Our secondary theorem, alas, requires us to evaluate the following integral during the proof:
$$ \int_0^1 e^2x dx = \frac{e^2}{2} - \frac{1}{2}. $$
Out[7]:

That hasn't had the desired effect! We wanted $2x$ in the superscript, but $\LaTeX$ has only put the $2$ in it. What we should do is place the superscript into curly braces { } so that $\LaTeX$ knows we want the whole expression in the superscript.

In [8]:
%%pdflatex
Our secondary theorem, alas, requires us to evaluate the following integral during the proof:
$$ \int_0^1 e^{2x} dx = \frac{e^2}{2} - \frac{1}{2} $$
Out[8]:

The same applies to subscripts as well. Furthermore, the use of { } is necessary if you want to nest superscripts or subscripts - that is you want superscripts which themselves have superscripts:

In [9]:
%%pdflatex
Following these results, we can estimate the value of the following integral as:
$$ \int_0^1 e^{x^{2}} dx \approx 1.46265. $$
From whence it follows that if $ x_{n_{k}} \rightarrow \infty $ as $k \rightarrow \infty$,
then $ x^{-1}_{n_{k}} \rightarrow 0$ as $k \rightarrow \infty$. 
Out[9]:

Greek letters

A common sight in mathematics lectures, textbooks, and papers are greek letters (anyone remember all those epsilons from Analysis?). Greek letters are produced in mathematics mode by preceding the name of the letter by a backslash. If you want the captial of a greek letter, you simply type it's name with a captial after the backslash!

Note that, in math environments, you don't need to include the text part of these commands! For further reference see the Overleaf page.

Common symbols and keywords

Some other common symbols and layout keywords that can be accessed by a (reserved) name in $\LaTeX$ are given below, along with some examples.

Useful symbols that are often found in equations:

  • \sum : for the summation symbol. Similarly \prod produces the product symbol.
  • \int : for the integral. This can be "stacked" to produce double integrals.
  • \approx : approximately equals symbol.
  • ( ),[ ], and \{ \} : will produce round, square, and curly braces.
  • \vert: if amsmath is installed will produce a vertical line, like the outline of the absolute value.
  • \forall and \exists : produce the for-all and there-exists symbols in logic expressions.
  • \geq, \leq : produce greater (respectively lesser) than or equal to symbols. You can use < and > for the usual less/greater than.
  • quad: this forces a "tab" in your equation, to space things out.
In [10]:
%%pdflatex
$$ \sum_{n=1}^{\infty} n^{-1} = \infty $$
$$ \int_0^1 \int_0^1 e^{x^{2} + y^{2}} dx dy \approx 2.13935 $$
$$ (1-x)^n \geq 1 - nx, \quad \forall \vert x \vert \leq 1 $$
Out[10]:

As for writing fractions and derivatives, we can use:

  • \frac{x}{y} : produces the fraction $\frac{x}{y}$. You can replace x and y with any expressions you like of course. We used these in our integral examples, above.
  • \dfrac{du}{dt} : produces the derivative $\dfrac{du}{dt}$. You could also use \frac{du}{dt} for this, but \dfrac is setup to produce a nicer-looking derivative.
In [11]:
%%pdflatex
If $y=x^3$ then $\dfrac{dy}{dx} = \frac{1}{3}x^2$.
Out[11]:

Matrices and Vectors

We can use the pmatrix environment to create matrices. Note that you can only create a pmatrix environment inside a mathematical environment (meaning any environment that allows you to render equations; so equation, align, and multiline which you will see shortly are all valid).

We use ampersands (&) to tell $\LaTeX$ we are starting a column of the matrix, and the double backslash (\\) to denote a new row. If we only use \\, we will be creating a column vector, and if we only include &, we will be making a row vector.

In [12]:
%%pdflatex
\begin{equation*}
\begin{pmatrix}
a_{11} & a_{12} & a_{13}\\
a_{21} & a_{22} & a_{23}\\
a_{31} & a_{32} & a_{33}\\
\end{pmatrix}
\begin{pmatrix}
x_1 \\
x_2 \\
x_3 \\
\end{pmatrix}
=
\begin{pmatrix}
\sum_{j=1}^3 a_{1j}x_j \\
\sum_{j=1}^3 a_{2j}x_j \\
\sum_{j=1}^3 a_{3j}x_j
\end{pmatrix}
\end{equation*}
Out[12]:

Notice that when we are in a mathematical enviornment, $\LaTeX$ ignores line breaks unless we use a double backslash.

Matching Brackets

You may notice that pmatrix automatically surrounds the elements of your matrix in parentheses. In general equations this isn't guaranteed to happen, particularly if you have long or tall expressions. Consider the example below where we have a "tall" fraction:

In [13]:
%%pdflatex
Define the function $f$ by
\begin{equation*}
    f(x) = (\frac{1}{2}x - 2)^2.
\end{equation*}
Out[13]:

The brackets that are meant to contain $\frac{1}{2}x - 2$ do not fully wrap around that expression. In order to do so, we need to use the \left and \right commands. We place these in pairs, and immediately in front of each part of the parentheses we want to scale; so the source code
\left( some maths \right)
will produce a pair of round brackets () that are scaled to be the correct height to encase everything in some maths between them:

In [14]:
%%pdflatex
Define the function $f$ by
\begin{equation*}
    f(x) = \left( \frac{1}{2}x - 2 \right)^2.
\end{equation*}
Out[14]:

You can also do this with curly {} and square [] bracers too, and even symbols like \vert! However, you need to remember that to produce a { symbol, we need to use \{ (and similarly for }), so this case looks a little different:

In [15]:
%%pdflatex
Define the function $f$ by
\begin{equation*}
    f(x) = \left( \frac{1}{2}x - 2 \right)^2.
\end{equation*}
We can enclose in square bracers in much the same way as before:
\begin{equation*}
    \int_0^1 f(x) dx = \left[ \frac{1}{12}x^3 - x^2 + 4x \right]_0^1.
\end{equation*}
To enclose in curly bracers, we have the same layout but need to ``escape" the bracer first (put a \textbackslash\ in front of it):
\begin{equation*}
    L^2(0,1) = \left\{ f \ \text{such that} \ \int_0^1 f^2 dx < \infty \right\}
\end{equation*}
We can also scale a handful of other symbols which are commonly used to enclose expressions.
You can also nest multiple left and right commands, so long as you close them off correctly:
\begin{equation*}
    \left\lvert \left\lvert f-f_n \right\rvert \right\rvert 
    = \mathrm{sup}_{(0,1)}\left\lvert f(x)-f_n(x) \right\rvert.
\end{equation*}
You don't even have to use the same symbol on each pair of the left and right, either.
This can be handy for intervals, like $\left( 0, \frac{1}{2} \right]$.
Out[15]:

If you proceed onto the lesson on Macros, you'll realise that you can setup your own custom commands to generate matching pairs of brackets, so you don't need to continually put \left and \right everywhere.

\text{} and other math fonts

Sometimes, you want to insert a short amount of text into your equations, or need to use a letter-like symbol in an equation. If you have the amsmath and amssymb packages installed, you can use the \text command in a math environment to type prose (as you would outside a math environment) - there is an example of us doing this in our expression for $L^2(0,1)$, above.

You can also use a single backslash \ in a math environment to force $\LaTeX$ to place a non-breaking space, just like when typing prose. This is useful for de-cluttering expressions.

To obtain symbols like those that denote the natural numbers or reals, you can use the \mathbb command. Finally, if you want to insert single letters which are not meant to be taken as variables, you can use the \mathrm command to have them displayed in the same font as your prose - this is typically used to distinguish a variable from a similar-looking symbol, like the below:

In [16]:
%%pdflatex
Let's list some common sets: like the naturals $\mathbb{N}$, the rationals $\mathbb{Q}$, the reals $\mathbb{R}$. \\
Did you know that $\frac{1}{n}\rightarrow 0 \text{ as } n\rightarrow\infty$? \\
Sometimes it's useful to distinguish between the variable $d$ and the $\mathrm{d}$ that's used for differentials;
\begin{equation*}
    \int_0^1 d(x) \ \mathrm{d}x.
\end{equation*}
Also, notice how inserting a backslash (\textbackslash)\ between our integrand ``$d$" and differential ``$\mathrm{d}$" 
has forced a small space to appear between the two.
Out[16]:

Give me a symbol!

Want to write a certain symbol in $\LaTeX$ but don't know the name of the command to use? Worry not - there is an incredibily useful website called DeTeXify at which you can draw the symbol you want and the webpage will make suggestions as to how to obtain it in $\LaTeX$.

Equation layout and formatting

Numering equations

One advantage of using \begin{equation} \end{equation} to display mathematics expression is that it automatically numbers your equations. This is very handy when you have many equations in a single files: if new equations are added or any existed equations are deleted, the numbering of equations will be automatically adjusted.

We can also prevent $\LaTeX$ from numbering an equation by placing an asterix (*) after the name of the environment - by way of illustration, try copying the following code into your source file and rendering it:

In [17]:
%%pdflatex
This equation is super important and I'll be refering back to it a lot:
\begin{equation}
    P(B|A) = \frac{P(A|B)P(B)}{P(A)}.
\end{equation}
As for this equation, well we won't be needing to refer back to it, but the reader should still notice it when they glance at the page:
\begin{equation*}
    P(A\cup B) = 1
\end{equation*}
Out[17]:

Notice how the use of \begin{equation*} \end{equation*} instead of \begin{equation} \end{equation} has prevented an equation number being produced. Now try adding another (labelled) equation that appears after the unlabelled one, and rendering your document. Then add another labelled equation between the original two equations - you should notice $\LaTeX$ automatically handles the renumbering!

Displaying long equations

Sometimes equations are just too long to fit on one line - in this case we use a special math environment called the multiline environment. Insert a double backslash (\\) to set a point for the equation to be broken onto the next line. Anything before the backslash will be aligned to the left and anything afterwards will be displayed in the next line and aligned to the right.

In [18]:
%%pdflatex
\begin{multline*}
p(x) = 3x^6 + 14x^5y + 590x^4y^2 + 19x^3y^3+14x^5y + 590x^4y^6 + 19x^3y^8 -x^2 - x^9 + \\ %line break here
\sin(4\pi) - 12x^2y^4 - 12xy^5 + 2y^6 - a^3b^3
\end{multline*}
Out[18]:

Note that in general the the double backslash inserts a line break. Of course, you could also use the align environment for this task, which we cover next.

Aligning several equations

If there are several equations that you need to align vertically, the align environment will do the job. Use an ampersand (&) to tell $\LaTeX$ at which point each line should line up, and use the double backslash (\\) to create new lines.

In [19]:
%%pdflatex
\begin{align*} 
2x - 5y &=  8 & \\ 
3x + 9y &=  -12 &
\end{align*}
Out[19]:
In [20]:
%%pdflatex
We can also force \LaTeX\ to align at multiple points:
\begin{align*}
-u''(t) &= \omega^2 u(t), &t \in (0,1) \\
-u''(t) &= \omega^2 u(t) + \nu u'(t), &t \in [1,2]
\end{align*}
Out[20]:

Writing Equations

Try to display the follow equations in $\LaTeX$:

$$ \frac{\prod_{ij}P(A_{ij} = a_{ij}|b)}{\prod_{ij}A_{ij}!} $$

Now try to copy this sentence followed by the function definition - the function definition should all be aligned as you see it displayed here!

We define the function $f$ with domain $(-\infty,\infty)$ as follows: \begin{align*} f:(-\infty, \infty) &\rightarrow [0,1], \\ f(x) &= 0, & x < 0, \\ f(x) &= \frac{1}{2}, & x = 0 \\ f(x) &= 1, & x > 1. \end{align*}

Don't forget about DeTeXify if you need to figure out the command for a certain symbol.

Solution

Key Points:

  • To write mathematics, we must be inside a mathematical environment like math, equation, or align.
  • We can use inline or display environments, each with their own advantages.
  • We can insert symbols, scripts, and formatting into our equations using in-built $\LaTeX$ commands.