An important reason for using LaTeX is that it is fairly easy to typeset mathematical expressions. In this lesson we will discuss some of the most used commands for typesetting mathematics.

Firstly in the previous lesson you tried using the parskip package. The nice thing about LaTeX is that there are many other packages for a large variety of things. For example there are packages for drawing diagrams, for using emoticons, for changing the margins of the document, and crucially for this lesson, packages for typesetting all kinds of mathematical symbols.

For basic mathematics, no packages are needed. For example in the previous lesson we could write $a^2 + b^2 = c^2$ without a problem. However if you want to typeset more complicated mathematics, for example a binomial coefficient $\binom{n}{r}$ (which is usually noted nCr on a calculator), you will need the package mathtools (which is an extension of the older amsmath).

Packages are always called in the preamble, which is the bit between defining the documenttype and beginning the document. Here is an example of how we might typeset the binomial coeffient.

\documentclass{article}

% This is where the preamble starts
% here you can call any packages you want
% or define any new definitions that might help you.
\usepackage{mathtools}

\begin{document}
The mathematical notation for nCr is $\binom{n}{r}$.
\end{document}

Please consent the main reference Wikibooks/LaTeX/Mathematics, whenever you are in doubt. For more details you can look at this Short Math Guide for LaTeX, or the documentation of amsmath or mathtools.

Mathematical environments

Mathematics within text like $1+1=2$ is called inline mathematics. A line that consists of a single mathematical expression like \[ 1+1=2 \] is called a display. Inline mathematics can be typeset between two $ characters.

We know that $1+1=2$.
We know that $1+1=2$.

Mathematics can be typeset in a display by putting it between \[ and \].

Examine the curve given by \[ y = x^2 + 2. \] Does it attain a minimum?
Examine the curve given by
\[
y = x^2 + 2.
\]
Does it attain a minimum?

Recall that an empty line creates a new paragraph. Therefore there are no empty lines before or after the display in this example.

To number displays automatically one uses the equation environment. We will see later how to reference displays.

Since both $a$ and $b$ are odd we have \begin{equation} a + b = 2x \end{equation} and \begin{equation} a-b = 2y \end{equation} for some $x$ and $y$.
Since both $a$ and $b$ are odd we have
\begin{equation}
a + b = 2x
\end{equation}
and
\begin{equation}
a-b = 2y
\end{equation}
for some $x$ and $y$.

Note: if you do not want the equation (any other numbered environment) to have a number, you can use \begin{equation*}...\end{equation*}. The * tells the typesetting software that you don’t want this equation to be numbered.

LaTeX switches to math mode in every mathematical environment. The most important differences compared to text mode are:

In the rest of this lesson we will assume that you are working in math mode.

Powers and indices

Powers and indices can be inserted using the hat ^ and the underscore _. For example

\[ a^2\quad a_i\quad a_i^2 \]
a^2 a_i a_i^2

The _ and ^ bind themselves to the next group. Recall that groups can be made with { and }. Hence

\[ a^{n+1}\quad a^n+1\quad a_{i/2} \]
a^{n+1} a^n+1 a_{i/2}

Exercise 1

Typeset the powers

\[ (a^b)^c,\quad a^{b^c} \]

Common expressions

A fraction can be created using \frac{2}{3}. It is possible to create fractions inside fractions.

\[ \frac{\frac{1}{x}+\frac{1}{y}}{y-z} \]
\frac{\frac{1}{x}+\frac{1}{y}}{y-z}

We can use \tfrac for small fractions, it is up to the user to decide when to use this.

Roots can be inserted as \sqrt{2} or \sqrt[n]{2} for $\sqrt[n]{2}$.

A sum or integral is given by \sum or \int, use ^ and _ to specify the boundaries. For example

\[ \sum_{i=1}^{10} t_i\quad \int_0^\infty e^{-x}\, dx \]
\sum_{i=1}^{10} t_i
\int_0^\infty e^{-x}\, dx

There are more operators that use ^ and _ for specifying boundaries, for example \prod $\prod$ and \bigcup $\bigcup$. A more detailed list is shown at the end of this page.

You can scale brackets and delimiters, e.g. | or (, automatically using \left and \right.

\[ \left| \sum_{n=1}^\infty a_n \right| \leq \sum_{n=1}^\infty \left| a_n \right| \]
\left| \sum_{n=1}^\infty a_n \right| \leq \sum_{n=1}^\infty
\left| a_n \right|

With \ldots $\ldots$ and \cdots $\cdots$ one makes dots. The context determines which dots to use. For example, we write 1,2,\ldots,n $1,2,\ldots,n$ and 1\cdot 2 \cdots n $1\cdot 2\cdots n$. In general we try to choose the dots in such a way that they are at the same height as the surrounding operators. The command \dots tries to accomplish this.

Exercise 2

Typeset the display.

\[ \left | \sum_{i=1}^n a_ib_i \right| \leq \left( \sum_{i=1}^n a_i^2 \right)^{\frac{1}{2}} \cdot \sqrt{\sum_{i=1}^n b^2_i} \]

Horizontal whitespace

In math mode all spaces and line breaks are ignored; whitespace is determined by the operators or commands used. However, sometimes it is necessary to create some horizontal whitespace. The command \quad inserts whitespace the size of the font type, so 10pt for a 10pt font. A \qquad inserts twice as much whitespace. These commands also work in text mode. In math mode we have the following specific commands:

command length
\, 3/18 quad                     
\: 4/18 quad
\; 5/18 quad
\! $-$3/18 quad
\[ \int y\, dx \]
\int y\, dx

Warning: only mess with the spacing when you need to! You should aim to use this feature as little as possible!

Fonts

With the following commands we can choose different fonts. For the last two the package amssymb is required.

command example   usage
\mathrm $\mathrm{ABCDEF}$ $\mathrm{abcdef}$  
\mathit $\mathit{ABCDEF}$ $\mathit{abcdef}$  
\mathbf $\mathbf{ABCDEF}$ $\mathbf{abcdef}$ sets, vectors
\mathcal $\mathcal{ABCDEF}$ $\mathcal{abcde}$ categories, big-oh notation
\mathbb $\mathbb{ABCDEF}$   sets

We prefer not to use these commands directly in the code, but in a new command to seperate the content and layout. For example:

The complex numbers $\mathbb{C}$.
\documentclass[a4paper]{article}
\usepackage[dutch]{babel}
\usepackage{mathtools}
\usepackage{amssymb}

\newcommand{\field}[1]{\mathbb{#1}}
\newcommand{\CC}{\field{C}}

\begin{document}
The complex numbers $\CC$.
\end{document}

Exercise 3

Typeset the display.

\[ \forall \epsilon > 0 \quad \exists \delta \quad \text{such that} \quad \forall x,y \in \mathbb{R}: |x-y|< \delta \implies | f(x+\delta) - f(x)| < \epsilon. \]

Hint: text inside math mode must be inserted using \text{...}. You can use Wikibooks/LaTeX for a list of symbols and LaTeX commands.

Operators

An operator is a function written as a word, like $\sin$ and $\log$. LaTeX has many predefined operators, but sometimes it is necessary to define one yourself. We create operators using \DeclareMathOperator in the preamble.


The next two exercises are about typesetting theorems.

Exercise 4

  1. Create a new LaTeX file containing the following code:
    \documentclass[a4paper,12pt]{article}
    \usepackage[english]{babel}
    \usepackage{mathtools}
    \usepackage{amssymb}
    \usepackage{amsthm}
    
    \theoremstyle{plain}
    \newtheorem{theorem}{Theorem}
    \theoremstyle{definition}
    \newtheorem{definition}[theorem]{Definition}
    
    \begin{document}
    
    \begin{definition}
    \end{definition}
    
    \begin{theorem}[Cauchy-Schwarz inequality]
    \end{theorem}
    
    \end{document}
    
  2. Typeset the following definition:

    Definition 1. If $P(X\in S)=1$ for a finite $S$ then the expectation \[ \mathbb{E}X=\sum_{x\in S} xP(X=x). \]

  3. Typeset the following theorem. You may or may not use \DeclareMathOperator, whatever you find convenient.

    Theorem 2 (Cauchy-Schwarz inequality) . If $X$ and $Y$ are random variables with $\mathbb{E}X^2 \lt \infty$ and $\mathbb{E}Y^2 \lt \infty$, then \[ \DeclareMathOperator{\Var}{Var} \DeclareMathOperator{\Cov}{Cov} |\Cov(X,Y)|\le\sqrt{\Var(X)}\sqrt{\Var (Y)}. \]

Exercise 5

  1. Choose one of the following statements:
    • There are infinitely many prime numbers.
    • The number $\sqrt2$ is not rational.
  2. Create a new LaTeX file containing:
    \documentclass[a4paper,12pt]{article}
    \usepackage[english]{babel}
    
    \usepackage{mathtools}
    \usepackage{amssymb}
    \usepackage{amsthm}
    
    \theoremstyle{plain}
    \newtheorem{theorem}{Theorem}
    
    \begin{document}
    
    \begin{theorem}
    \end{theorem}
    
    \begin{proof}
    \end{proof}
    
    \end{document}
    
  3. Typeset your chosen statement using the theorem environment defined in the preamble.
  4. Prove the theorem and write your proof down in a proof environment.

Resources

You don’t need to remember all LaTeX commands. There are lot of references on the internet:

Remember

  • What is a math environment? What rules apply in math mode?
  • How to insert special characters?
  • What do $, [ ], _, ^, \alpha, \sin, \log, \frac, \binom, \sqrt[n], \sum, \int, (), [], \{ \} and | | do?
  • How to create your own operator?
  • How to insert horizontal whitespace?