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!
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:
%%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.
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.
%%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.
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.
%%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.
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:
%%pdflatex
Suppose we have a real sequence $x_n$ such that $x_n \rightarrow 0$ as $n \rightarrow \infty$.
And we can give both a super and subscript to symbols by giving both an _
and a ^
symbol after it:
%%pdflatex
The result of our main theorem follows trivially having computing the value of the integral
$$ \int_0^1 e^x dx = e-1. $$
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:
%%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}. $$
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.
%%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} $$
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:
%%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$.
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.
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.
%%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*}
Notice that when we are in a mathematical enviornment, $\LaTeX$ ignores line breaks unless we use a double backslash.
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:
%%pdflatex
Define the function $f$ by
\begin{equation*}
f(x) = (\frac{1}{2}x - 2)^2.
\end{equation*}
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:
%%pdflatex
Define the function $f$ by
\begin{equation*}
f(x) = \left( \frac{1}{2}x - 2 \right)^2.
\end{equation*}
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:
%%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]$.
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.
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:
%%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*}
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!
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.
%%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*}
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.
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.
%%pdflatex
\begin{align*}
2x - 5y &= 8 & \\
3x + 9y &= -12 &
\end{align*}
%%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*}