\chapter {Math}\label{sec:math}

\section{Amsmath}

Although you can do a lot of math typesetting with \LaTeX{} alone,
we shall assume that \package{amsmath} and related packages are loaded,
\emph{e.g.} with a command
\begin{verbatim}
\usepackage{amsmath,amsfonts,amssymb}
\end{verbatim}
in the preamble, \emph{i.e.} between \cmd{documentclass\{\ldots\}}
and \benv{document}.

For documentation, click in the launcher \menupath{Documentation /
  AmsMath User Guide}, or search the documentation list, see
section~\ref{sec:doclist}.\nocite{amsmath}

\section{Math mode: Inline and display math}\label{sec:displaymath}

Math in running text is bracketed between \$ characters:\footnote{%
Alternative codings: \texttt{\bsl( \ldots \bsl)} or \benv{math} \ldots
\eenv{math}.}
\exa
\begin{verbatim}
Simple bits of math in running text,
enclosed in \$ characters: $x$ or
$\alpha$ or $\sum_i n_i$
\end{verbatim}
\exb
Simple bits of math in running text,
enclosed in \$ characters: $x$ or
$\alpha$ or $\sum_i n_i$
\exc
\nextblock
Notice that \emph{ordinary letters are italicized} in math mode.

\nextblock
More elaborate formulas are better typeset as \emph{display math},
on a line by itself.\footnote{Alternative codings: \benv{displaymath}
\ldots \eenv{displaymath} and, only with the \package{amsmath}
package: \benv{equation*} \ldots \eenv{equation*}.} Notice the more
spacious typesetting of indices in display math mode.
\exa
\begin{verbatim}
\[ x = \sum_{i=0}^\infty y_i \]
\end{verbatim}
\exb
\[ x = \sum_{i=0}^\infty y_i \]
\exc

\nextblock
Display math with automatic equation numbering:
\exa
\begin{verbatim}
\begin{equation}
x = \sum_{i=0}^\infty y_i \label{firstequation}
\end{equation}
See equation \ref{firstequation}
on page \pageref{firstequation}.
\end{verbatim}
\exb
\begin{equation}
x = \sum_{i=0}^\infty y_i \label{firstequation}
\end{equation}
See equation \ref{firstequation} on page \pageref{firstequation}.
\exc
\nextblock
This is yet another example of automatically generated numbers
which can be used for cross-referencing.

\section{Mathematical notation}

Many symbols listed below can be entered via the \TS{} interface;
either via the Math menu or via the panel at the left. But you can
also type the code directly.\newpage

\subsection{Greek letters}
~
\exa
\begin{verbatim}
lowercase: $\alpha, \beta, \epsilon,
\varepsilon, \gamma, \phi, \psi,
\xi, \pi, \sigma, \omega$ \\
uppercase: $\Gamma, \Phi, \Psi, \Xi,
 \Pi,\Sigma, \Omega$
\end{verbatim}
\exb
lowercase: $\alpha, \beta, \epsilon,
 \varepsilon, \gamma, \phi, \psi,
 \xi, \pi, \sigma, \omega$ \\
uppercase: $\Gamma, \Phi, \Psi, \Xi,
 \Pi, \Sigma, \Omega$
\exc

\subsection{Mathematical accents}

\exa
\begin{verbatim}
$x', \hat{a}, \acute{e}, \bar{\imath},
\vec{o}, \dot{u}, \ddot{v},
\vec{\dot{Y}}$
\end{verbatim}
\exb
$x', \hat{a}, \acute{e}, \bar{\imath},
\vec{o}, \dot{u}, \ddot{v},
\vec{\dot{Y}}$
\exc

\nextblock
Note \cmd{imath} for a dotless i, and the last example which stacks
two accents on top of each other.

\subsection{Various symbols}

\nextblock
Arithmetic and relational operators

\exa
\begin{verbatim}
$\alpha = \theta - \gamma \times \zeta$\\
$x < y$ and $a > b$ \\
$u \leq v$ and $i \geq j$ \\
$\sigma \pm \tau$ and $\beta \sim \rho$
\end{verbatim}
\exb
$\alpha = \theta - \gamma \times \zeta$\\
$x < y$ and $a > b$ \\
$u \leq v$ and $i \geq j$ \\
$\sigma \pm \tau$ and  $\beta \sim \rho$
\exc

\nextblock
Arrows

\exa
\begin{verbatim}
$\leftarrow, \Rightarrow,
\uparrow, \Downarrow,
\leftrightarrow,
\longleftrightarrow$
\end{verbatim}
\exb
$\leftarrow, \Rightarrow,
\uparrow, \Downarrow,
\leftrightarrow,
\longleftrightarrow$
\exc

\subsection{Finding symbols}

Many symbols are already available via the \TS{} interface. But for
a very comprehensive list, consult the document `Comprehensive
Symbol list', which is part of the \TL{} documentation. Search for
`comprehensive' in the documentation list (see \ref{sec:doclist}).
\nocite{symbols}

\subsection{Functions}

\exa
\begin{verbatim}
Do not write $log 100 = 2$ but \\
$\log 100 = 2$ \\
$\ln 100 = 4.605$ \\
$\sin(45) = 0.707$
\end{verbatim}
\exb
Do not write $log 100 = 2$ but \\
$\log 100 = 2$ \\
$\ln 100 = 4.605$ \\
$\sin(45) = 0.707$
\exc

\section{Various constructs}\label{sec:constructs}

For the samples below, we use display math, since many of them take
up too much height to fit within a standard line of text. Note the use
of braces \texttt{\{} and \texttt{\}} to collect several letters and
symbols into one argument.%\enlargethispage*{20bp}

\nextblock
Subscripts and superscripts

\exa
\begin{verbatim}
\[ x_i, x_{i+1}, a^2, b^{x+y} \]
\end{verbatim}
\exb
\[ x_i, x_{i+1}, a^2, b^{x+y} \]
\exc

\nextblock
Roots, without and with optional parameter

\exa
\begin{verbatim}
\[ \sqrt{x+y}, \sqrt[n]{2} \]
\end{verbatim}
\exb
\[ \sqrt{x+y}, \sqrt[n]{2} \]
\exc

\nextblock
Two styles of fractions and regular text within display math

\exa
\begin{verbatim}
\[ x/y \text{ and } \frac{\alpha}{\beta + \gamma} \]
\end{verbatim}
\exb
\[ x/y \text{ and } \frac{\alpha}{\beta + \gamma} \]
\exc

\nextblock
Sums, products and integrals

\exa
\begin{verbatim}
\[ \sum_i x_i = \prod_{i=2}^7 i+1 =
 \int_{z=0}^\infty z^2 \]
\end{verbatim}
\exb
\[ \sum_i x_i = \prod_{i=2}^7 i+1 =
 \int_{z=0}^\infty z^2 \]
\exc

\nextblock
Ellipsis (dots), on the baseline and higher up

\exa
\begin{verbatim}
\[ x_0 \ldots x_{100},
 x_0 + \cdots + x_{10} \]
\end{verbatim}
\exb
\[ x_0 \ldots x_{100},
 x_0 + \cdots + x_{10} \]
\exc

\section{Arrays/matrices}

\begin{figure}[bt]
\centering
\includegraphics[width=.5\linewidth]{../screenshots/array_wizard}
\caption{Quick Array wizard}
\label{fig:quick_array}
\end{figure}

\LaTeX{} arrays:

\exa
\begin{verbatim}
\[ \begin{array}{lcr}
 0.15   &   3a  &  0 \\
 0.0003 &  501d & 10 \\
 0.011  &   2c  &  1
\end{array} \]
\end{verbatim}
\exb
\[ \begin{array}{lcr}
 0.15   &   3a  &  0 \\
 0.0003 &  501d & 10 \\
 0.011  &   2c  &  1
\end{array} \]
\exc
\noindent
In the second parameter above, \texttt{lcr}, each of the three
letters `lcr' specify the alignment of one column: left, centered and right.

\TS{} has a `Quick Array' wizard to create a first approximation, see
Figure~\ref{fig:quick_array}. The wizard assumes that the text cursor is
between math mode delimiters such as \verb|\[...\]|.

\nextblock
Matrices, \package{amsmath}-style:

\exa
\begin{verbatim}
\[ \begin{matrix}
x & y & z \\
.0 & .01 & .001
\end{matrix} \]
\end{verbatim}
\exb
\[ \begin{matrix}
x & y & z \\
.0 & .01 & .001
\end{matrix} \]
\exc

\nextblock
Notice the absence of column specifications; all columns are centered.

\nextblock
You get built-in round brackets `( )' with \texttt{pmatrix} and square
brackets `[ ]' with \texttt{bmatrix}. See the amsmath documentation
for more variations.
\exa
\begin{verbatim}
\[ \begin{pmatrix}
x & y & z \\
.0 & .01 & .001
\end{pmatrix} \begin{bmatrix}
x & y & z \\
.0 & .01 & .001
\end{bmatrix} \]
\end{verbatim}
\exb
\[ \begin{pmatrix}
x & y & z \\
.0 & .01 & .001
\end{pmatrix} \begin{bmatrix}
x & y & z \\
.0 & .01 & .001
\end{bmatrix} \]
\exc

\nextblock
Matrix with various ellipses:

\exa
\begin{verbatim}
\[ \begin{bmatrix}
   a_{11} & \ldots & a_{1m} \\
   \vdots & \ddots & \vdots \\
   a_{n1} & \ldots & a_{nm}
 \end{bmatrix} \]
\end{verbatim}
\exb
\[ \begin{bmatrix}
   a_{11} & \ldots & a_{1m} \\
   \vdots & \ddots & \vdots \\
   a_{n1} & \ldots & a_{nm}
 \end{bmatrix} \]
\exc

\nextblock
or

\exa
\begin{verbatim}
\[ \begin{bmatrix}
   a_{11} & \ldots & a_{1m} \\
   \hdotsfor{3} \\
   a_{n1} & \ldots & a_{nm}
 \end{bmatrix} \]
\end{verbatim}
\exb
\[ \begin{bmatrix}
   a_{11} & \ldots & a_{1m} \\
   \hdotsfor{3} \\
   a_{n1} & \ldots & a_{nm}
 \end{bmatrix} \]
\exc

\nextblock
Bracketing with large delimiters:

\exa
\begin{verbatim}
\[ \left( \begin{array}{rr}
        10 & 100 \\
         a &   b
       \end{array}\right) \]
\end{verbatim}
\exb
\[ \left( \begin{array}{rr}
        10 & 100 \\
         a &   b
       \end{array}\right) \]
\exc

\nextblock
This also works with braces `\{ \}' and square brackets `[ ]'. If
you need only one of the two braces, use `.' for the other one:

\exa
\begin{verbatim}
\[ \left\{ \begin{array}{c} a \\
 b \end{array} \right. \]
\end{verbatim}
\exb
\[ \left\{ \begin{array}{c} a \\
 b \end{array} \right. \]
\exc

\section{Multiline equations}

There are various constructs for multiline equations. Basic \LaTeX{}
has the \texttt{eqnarray} and \texttt{eqnarray*} environments, the
first with, the second without automatic numbering.

But we shall just give an example of the \package{amstex}
\texttt{align} and \texttt{align*} environments:

\exa
\begin{verbatim}
\begin{align}
f(x) &= (a + b)^2 \nonumber \\
     &= a^2 + 2ab + b^2\label{AnEquation} \\
     &\ne (a+b)(a-b)\label{AnOther}
\end{align}
See equation \ref{AnEquation} and \ref{AnOther}.
\end{verbatim}
\exb
\begin{align}
f(x) &= (a + b)^2 \nonumber \\
     &= a^2 + 2ab + b^2\label{AnEquation} \\
     &\ne (a+b)(a-b)\label{AnOther}
\end{align}
See equations \ref{AnEquation} and \ref{AnOther}.
\exc

\nextblock
The \texttt{\&} character defines the alignment. You see that every line
get its own number, unless it is suppressed with a \cmd{nonumber}
command.

\nextblock
The starred version omits the numbering:
\exa
\begin{verbatim}
\begin{align*}
f(x) &= (a + b)^2 \\
     &= a^2 + 2ab + b^2
\end{align*}
\end{verbatim}
\exb
\begin{align*}
f(x) &= (a + b)^2 \\
     &= a^2 + 2ab + b^2
\end{align*}
\exc

\section{Fonts in math}

\subsection{Upright and italic}

First, note that alphabetic characters will be italicized in math
mode. Use \cmd{mathrm} to get an upright version:

\exa
\begin{verbatim}
$E, \mathrm{E}, p, \mathrm{p}$
\end{verbatim}
\exb
$E, \mathrm{E}, p, \mathrm{p}$
\exc

\subsection{Bold}

With bold, the situation is, unfortunately, a bit complicated. For
regular `latin' alphabetic characters, use \cmd{mathbf}, which makes
the character at the same time bold and upright:

\exa
\begin{verbatim}
$M, \mathbf{M}, v, \mathbf{v}$
\end{verbatim}
\exb
$M, \mathbf{M}, v, \mathbf{v}$
\exc
\nextblock
For Greek characters and other symbols, try \cmd{boldsymbol} instead
of \cmd{mathbf}:
\exa
\begin{verbatim}
$\Psi, \boldsymbol{\Psi},
\infty, \boldsymbol{\infty}$
\end{verbatim}
\exb
$\Psi, \boldsymbol{\Psi},
\infty, \boldsymbol{\infty}$
\exc

\nextblock
If neither \cmd{mathbf} nor \cmd{boldsymbol} does the trick, load
the \package{bm} package:
\begin{verbatim}
\usepackage{bm}
\end{verbatim}
and try again.

\subsection{Fancy math fonts}
~
\exa
\begin{verbatim}
Blackboard: $\mathbb{B}$\\
Calligraphic: $\cal{A}$\\
Fraktur: $\mathfrak{A}$
\end{verbatim}
\exb
Blackboard: $\mathbb{B}$\\
Calligraphic: $\cal{A}$\\
Fraktur: $\mathfrak{A}$
\exc

\section{Macros}

It can become cumbersome to write something like \texttt{\bsl
boldsymbol\{\bsl alpha\}} for $\boldsymbol{\alpha}$ over and over
again. You can define an abbreviation with the following code:
\begin{verbatim}
\newcommand{\balph}{\boldsymbol{\alpha}}
\end{verbatim}
and then you just need to type \cmd{balph}.

A macro can also have parameters. Below, \texttt{[1]} indicates the
number of parameters and \verb+#1+ indicates the first parameter.
\begin{verbatim}
\newcommand{\bvc}[1]{\vec{\mathbf{#1}}}
\end{verbatim}
or, if you also want to use it in text without bothering with \$ signs:
\begin{verbatim}
\newcommand{\bvc}[1]{\ensuremath{\vec{\mathbf{#1}}}}
\end{verbatim}
{\sloppy With this definition you can type \verb+\bvc{x}+
  rather than \verb+\vec{\mathbf{x}}+ or
  \verb+$\vec{\mathbf{x}}$+ for \ensuremath{\vec{\mathbf{x}}}.\par}

\section{Practice}\label{sec:math_practice}

When trying out the code samples from this chapter, do not forget to
load the AMS packages:
\begin{verbatim}
\documentclass{article}
\usepackage{amsmath,amsfonts,amssymb}
...
\begin{document}
...
\end{document}
\end{verbatim}
Remember not to use inline math for displayed equations, see
section~\ref{sec:displaymath}.

The practice zip, see section~\ref{sec:zipfile}, contains an example
\LaTeX{} file \path{math_sample.tex}.

{\sloppy
When looking for real mathematical texts to convert to \LaTeX{}, you
may turn to Wikipedia pages such as
\url{http://en.wikipedia.org/wiki/Linear_regression} or
\url{http://en.wikipedia.org/wiki/L2_norm}, or use something of your
own.\par}
