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.
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$.
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?
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$.
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:
\text{...}
.In the rest of this lesson we will assume that you are working in math mode.
Powers and indices can be inserted using the hat ^
and the underscore
_
. For example
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} a^n+1 a_{i/2}
Typeset the powers
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}
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
\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|
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.
Typeset the display.
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
Warning: only mess with the spacing when you need to! You should aim to use this feature as little as possible!
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:
\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}
Typeset the display.
Hint: text inside math mode must be inserted using \text{...}
. You can
use Wikibooks/LaTeX for
a list of symbols and LaTeX commands.
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.
\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}
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). \]
\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)}. \]
\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}
theorem
environment defined in the preamble.proof
environment.You don’t need to remember all LaTeX commands. There are lot of references on the internet:
$
, [
]
, _
, ^
, \alpha
, \sin
, \log
, \frac
,
\binom
, \sqrt[n]
, \sum
, \int
, ()
, []
, \{ \}
and | |
do?