%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Contents: Math typesetting with LaTeX
% $Id: math.tex,v 1.3 1998/03/26 15:36:54 oetiker Exp oetiker $
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
\chapter{Typesetting Mathematical Formulae}

\begin{intro}
  Now you are ready! In this chapter we will attack the main strength
  of \TeX{}: mathematical typesetting. But be warned, this chapter
  only scratches the surface. While the things explained here are
  sufficient for many people, don't despair if you can't find a
  solution to your mathematical typesetting needs. It is highly likely
  that your problem is addressed in AMS-\LaTeX{}
  \footnote{\texttt{CTAN:/tex-archive/macros/latex/packages/amslatex}}
  or some other package.
\end{intro}
  
\section{General}

\LaTeX{} has a special mode for typesetting \wi{mathematics}.
Mathematical text within a paragraph is entered between \ci{(}
and \ci{)}, \index{$@\texttt{\$}} %$
between \texttt{\$} and \texttt{\$} or between
\verb|\begin{|\ei{math}\verb|}| and \verb|\end{math}|.\index{formulae}

\begin{example}
Add $a$ squared and $b$ squared 
to get $c$ squared. Or using 
a more mathematical approach:
$c^{2}=a^{2}+b^{2}$
\end{example}

\begin{example}
\TeX{} is pronounced as 
$\tau\epsilon\chi$.\\[6pt]
100~m$^{3}$ of water\\[6pt]
This comes from my $\heartsuit$
\end{example}

It is preferable to display larger mathematical equations or formulae,
that is to typeset them on separate lines. Therefore you enclose them
between \ci{[} and \ci{]} or between
\verb|\begin{|\ei{displaymath}\verb|}| and
  \verb|\end{displaymath}|.  This produces formulae which are not
numbered. If you want \LaTeX{} to number them, you can use the
\ei{equation} environment.


\begin{example}
Add $a$ squared and $b$ squared 
to get $c$ squared. Or using 
a more mathematical approach:
\begin{displaymath}
c^{2}=a^{2}+b^{2}
\end{displaymath}
And just one more line.
\end{example}

With \ci{label} and \ci{ref} you can reference an equation within the
text.

\begin{example}
\begin{equation} \label{eq:eps}
\epsilon > 0
\end{equation}
From (\ref{eq:eps}) we gather 
\ldots
\end{example}

Note that expressions will be typeset in a different style if displayed:
\begin{example}
$\lim_{n \to \infty} 
\sum_{k=1}^n \frac{1}{k^2} 
= \frac{\pi^2}{6}$
\end{example}
\begin{example}
\begin{displaymath}
\lim_{n \to \infty} 
\sum_{k=1}^n \frac{1}{k^2} 
= \frac{\pi^2}{6}
\end{displaymath}
\end{example}



There are differences between \emph{math mode} and \emph{text mode}. For
example, in \emph{math mode}: 

\begin{enumerate}

\item Most spaces and linebreaks do not have any significance, as all spaces
are either derived logically from the mathematical expressions or
have to be specified using special commands such as \ci{,}, \ci{quad} or
\ci{qquad}.
 
\item Empty lines are not allowed. Only one paragraph per formula.

\item Each letter is considered to be the name of a variable and will be
typeset as such. If you want to typeset normal text within a formula
(normal upright font and normal spacing) then you have to enter the
text using the \verb|\textrm{...}| commands.
\end{enumerate}


\begin{example}
\begin{equation}
\forall x \in \mathbf{R}:
\qquad x^{2} \geq 0
\end{equation}
\end{example}

\begin{example}
\begin{equation}
x^{2} \geq 0\qquad
\textrm{for all }x\in\mathbf{R}
\end{equation}
\end{example}
 

%
% Add AMSSYB Package ... Blackboard bold .... R for realnumbers
%
Mathematicians can be very fussy about which symbols are used:
it would be conventional here to use `\wi{blackboard bold}'
\index{bold symbols} which is obtained by \ci{mathbb} from the
package \pai{amsfonts} or \pai{amssymb}.
\ifx\mathbb\undefined\else
The last example becomes
\begin{example}
\begin{displaymath}
x^{2} \geq 0\qquad
\textrm{for all }x\in\mathbb{R}
\end{displaymath}
\end{example}
\fi

\section{Grouping in Math Mode}

Most math mode commands act only on the next character. So if you
want several characters affected by a command you have to group them
together using curly braces: \verb|{...}|.

\begin{example}
\begin{equation}
a^x+y \neq a^{x+y}
\end{equation}
\end{example}
 
\section{Building Blocks of a Mathematical Formula}

In this section the most important commands used in mathematical
typesetting will be described. For a list of all symbols available
take a look at section~\ref{symbols} on page~\pageref{symbols}.

\textbf{Lowercase \wi{Greek letters}} are entered as \verb|\alpha|,
 \verb|\beta|, \verb|\gamma|, \ldots, uppercase letters\footnote{There is no
  uppercase Alpha defined in \LaTeXe{} because it looks the same as a
  normal roman A. Once the new math coding is done, things will
  change.} are entered as \verb|\Gamma|, \verb|\Delta|, \ldots

\begin{example}
$\lambda,\xi,\pi,\mu,\Phi,\Omega$
\end{example}
 
\textbf{Exponents and Subscripts} can be specified using
\index{exponent}\index{subscript}
the \verb|^|\index{^@\verb"|^"|} and the \verb|_|\index{_@\verb"|_"|} character.

\begin{example}
$a_{1}$ \qquad $x^{2}$ \qquad
$e^{-\alpha t}$ \qquad
$a^{3}_{ij}$\\
$e^{x^2} \neq {e^x}^2$
\end{example}

The \textbf{\wi{square root}} is entered as \ci{sqrt}, the
$n^\mathrm{th}$ root is generated with \verb|\sqrt[|$n$\verb|]|. The size of
the root sign is determined automatically by \LaTeX. If just the sign
is needed use \verb|\surd|.

\begin{example}
$\sqrt{x}$ \qquad 
$\sqrt{ x^{2}+\sqrt{y} }$ 
\qquad $\sqrt[3]{2}$\\[3pt]
$\surd[x^2 + y^2]$
\end{example}

The commands \ci{overline} and \ci{underline} create
\textbf{horizontal lines} directly over or under an expression.
\index{horizontal!line}
\begin{example}
$\overline{m+n}$
\end{example}

The commands \ci{overbrace} and \ci{underbrace} create
long \textbf{horizontal braces} over or under an expression.
\index{horizontal!brace}
\begin{example}
$\underbrace{ a+b+\cdots+z }_{26}$
\end{example}

\index{mathematical!accents} To add mathematical accents such as small
arrows or \wi{tilde} signs to variables you can use the commands given
in Table~\ref{mathacc}.  Wide hats and tildes, covering several
characters are generated with \ci{widetilde} and \ci{widehat}. 
The \verb|'|\index{'@\verb"|'"|} symbol gives a \wi{prime}.
% a dash is --

\begin{example}
\begin{displaymath}
y=x^{2}\qquad y'=2x\qquad y''=2
\end{displaymath}
\end{example}

Often \textbf{\wi{vectors}} are specified by adding small \wi{arrow
  symbols} on top of a variable. This is done with the \ci{vec}
command. To denote the vector from $A$ to $B$ the two commands
\ci{overrightarrow} and \ci{overleftarrow} are useful.

\begin{example}
\begin{displaymath}
\vec a\quad\overrightarrow{AB}
\end{displaymath}
\end{example}


Names of log-like functions are often typeset in an upright
font and not italic as variables. Therefore \LaTeX{} supplies the
following commands to typeset the most important function names:
\index{mathematical!functions}

\begin{verbatim}
\arccos   \cos    \csc   \exp   \ker     \limsup  \min   \sinh
\arcsin   \cosh   \deg   \gcd   \lg      \ln      \Pr    \sup
\arctan   \cot    \det   \hom   \lim     \log     \sec   \tan
\arg      \coth   \dim   \inf   \liminf  \max     \sin   \tanh
\end{verbatim}

\begin{example}
\[\lim_{x \rightarrow 0}
\frac{\sin x}{x}=1\]
\end{example}

For the \wi{modulo function} there are two commands: \ci{bmod} for the
binary operator ``$a \bmod b$'' and \ci{pmod}
for expressions
such as ``$x\equiv a \pmod{b}$.''

A built-up \textbf{\wi{fraction}} is typeset with the
\ci{frac}\verb|{...}{...}| command.
Often the slashed form $1/2$ is preferable, because it looks better
for small amounts of `fraction material.'
\begin{example}
$1\frac{1}{2}$~hours
\begin{displaymath}
\frac{ x^{2} }{ k+1 }\qquad
x^{ \frac{2}{k+1} }\qquad
x^{ 1/2 }
\end{displaymath}
\end{example}

To typeset binomial coefficients or similar structures you can use
either the command \verb|{... |\ci{choose}\verb| ...}| or 
\verb|{... |\ci{atop}\verb| ...}|. The second command produces the 
same output as the first one, but without braces.

\pagebreak[4]

\begin{example}
\begin{displaymath}
{n \choose k}\qquad {x \atop y+2}
\end{displaymath}
\end{example}
 
\medskip

The \textbf{\wi{integral operator}} is generated with \ci{int}, the
\textbf{\wi{sum operator}} with \ci{sum}. The upper and lower limits
are specified with~\verb|^| and~\verb|_| as with subscripts and superscripts.

\begin{example}
\begin{displaymath}
\sum_{i=1}^{n} \qquad
\int_{0}^{\frac{\pi}{2}} \qquad
\end{displaymath}
\end{example}

For \textbf{\wi{braces}} and other \wi{delimiters} there exist all
types of symbols in \TeX{} (e.g.~$[\;\langle\;\|\;\updownarrow$).
Round and square braces can be entered with the corresponding keys,
curly braces with \verb|\{|, all other delimiters are generated with
special commands (e.g.~\verb|\updownarrow|). For a list of all
delimiters available, check table~\ref{tab:delimiters} on page
\pageref{tab:delimiters}.

\begin{example}
\begin{displaymath}
{a,b,c}\neq\{a,b,c\}
\end{displaymath}
\end{example}

If you put the command \ci{left} in front of an opening delimiter or
\ci{right} in front of a closing delimiter, \TeX{} will automatically
determine the correct size of the delimiter. Note, that you must close
every \ci{left} with a corresponding \ci{right}. If you don't want 
anything on the right, use the invisible `\ci{right.}'!

\begin{example}
\begin{displaymath}
1 + \left( \frac{1}{ 1-x^{2} }
    \right) ^3
\end{displaymath}
\end{example}

In some cases it is necessary to specify the correct size of a
mathematical delimiter\index{mathematical!delimiter} by hand,
therefore you can use the commands \ci{big}, \ci{Big}, \ci{bigg} and
\ci{Bigg} as prefixes to most delimiter commands\footnote{These
  commands do not work as expected if a size changing command has been
  used, or the \texttt{11pt} or \texttt{12pt} option has been
  specified.  Use the \pai{exscale} or \pai{amsmath} packages to
  correct this behaviour}.

\begin{example}
$\Big( (x+1) (x-1) \Big) ^{2}$\\
$\big(\Big(\bigg(\Bigg($\quad
$\big\}\Big\}\bigg\}\Bigg\}$\quad
$\big\|\Big\|\bigg\|\Bigg\|$
\end{example}

To enter \textbf{\wi{three dots}} into a formula you can use several
commands. \ci{ldots} typesets the dots on the baseline, \ci{cdots}
sets them centred. Beside that there are the commands \ci{vdots} for
vertical and \ci{ddots} for \wi{diagonal dots}.  \index{vertical
  dots}\index{horizontal!dots} In section \ref{sec:vert} you can find 
another example.

\begin{example}
\begin{displaymath}
x_{1},\ldots,x_{n} \qquad
x_{1}+\cdots+x_{n}
\end{displaymath}
\end{example}
 
\section{Math Spacing}

\index{math spacing} If the spaces within formulae chosen by \TeX{}
are not satisfactory, they can be adjusted by inserting special
spacing commands. There are some commands for small spaces: \ci{,} for
$\frac{3}{18}\:\textrm{quad}$ (\demowidth{0.166em}), \ci{:} for $\frac{4}{18}\:
\textrm{quad}$ (\demowidth{0.222em}) and \ci{;} for $\frac{5}{18}\:
\textrm{quad}$ (\demowidth{0.277em}).  The escaped space character
\verb*.\ . generates a medium sized space and \ci{quad}
(\demowidth{1em}) and \ci{qquad} (\demowidth{2em}) produce large
spaces. The size of a \ci{quad} corresponds to the width of the
character `M' of the current font.  The \verb|\!|\cih{"!} command produces a
negative space of $-\frac{3}{18}\:\textrm{quad}$ (\demowidth{0.166em}).

\begin{example}
\newcommand{\ud}{\mathrm{d}}
\begin{displaymath}
\int\!\!\!\int_{D} g(x,y)
  \, \ud x\, \ud y 
\end{displaymath}
instead of 
\begin{displaymath}
\int\int_{D} g(x,y)\ud x \ud y
\end{displaymath}
\end{example}
Note that `d' in the differential is conventionally set in roman type.
 
\section{Vertically Aligned Material}
\label{sec:vert}

To typeset \textbf{arrays}, use the \ei{array} environment. It works
somewhat similar to the \texttt{tabular} environment. The \verb|\\| command is
used to break the lines.

\begin{example}
\begin{displaymath}
\mathbf{X} =
\left( \begin{array}{ccc}
x_{11} & x_{12} & \ldots \\
x_{21} & x_{22} & \ldots \\
\vdots & \vdots & \ddots
\end{array} \right)
\end{displaymath}
\end{example}

The \ei{array} environment can also be used to typeset expressions which have one
big delimiter by using a ``\verb|.|'' as a invisible \ci{right} 
delimiter:

\begin{example}
\begin{displaymath}
y = \left\{ \begin{array}{ll}
 a & \textrm{if $d>c$}\\
 b+x & \textrm{in the morning}\\
 l & \textrm{all day long}
  \end{array} \right.
\end{displaymath}
\end{example}


For formulae running over several lines or for \wi{equation system}s
you can use the environments \ei{eqnarray} and \verb|eqnarray*|
instead of \texttt{equation}. In \texttt{eqnarray} each line gets an
equation number. In the \verb|eqnarray*| no line numbers are produced.

The \texttt{eqnarray} and the \verb|eqnarray*| environments work like
a 3-column table of the form \verb|{rcl}|, where the middle column can
be used for the equal sign or the not-equal sign. Or any other sign
you see fit. The \verb|\\| command breaks the lines.

\begin{example}
\begin{eqnarray}
f(x) & = & \cos x     \\
f'(x) & = & -\sin x   \\
\int_{0}^{x} f(y)dy &
 = & \sin x
\end{eqnarray}
\end{example}

\noindent Notice that there is too much space each side of the 
middle column, the equal signs. This can be reduced by setting
\verb|\setlength\arraycolsep{2pt}| as in the next example.

\index{long equations} \textbf{Long equations} will not be
automatically divided into neat bits.  The author has to specify
where to break them and how much to indent. The following two methods
are the most common ones used to achieve this.

\begin{example}
{\setlength\arraycolsep{2pt}
\begin{eqnarray}
\sin x & = & x -\frac{x^{3}}{3!}
     +\frac{x^{5}}{5!}-{}
                    \nonumber\\
 & & {}-\frac{x^{7}}{7!}+{}\cdots
\end{eqnarray}}
\end{example}
\pagebreak[1]

\begin{example}
\begin{eqnarray}
\lefteqn{ \cos x = 1
     -\frac{x^{2}}{2!} +{} }
                    \nonumber\\
 & & {}+\frac{x^{4}}{4!}
     -\frac{x^{6}}{6!}+{}\cdots
\end{eqnarray}
\end{example}

\enlargethispage{\baselineskip}
\noindent The \ci{nonumber} command causes \LaTeX{} not to generate a number for
this equation.

It can be difficult to get vertically aligned equations to look right
with these methods; the package \pai{amsmath} provides a more
powerful set of alternatives.

\section{Math Font Size}

\index{math font size} In math mode \TeX{} selects the font
size according to context. Superscripts for example get typeset in a
smaller font. If you want to add roman text to an equation and use the
\verb|\textrm| command, the font size switching mechanism will not work, as
\verb|\textrm| temporarily escapes to text mode. Use \verb|\mathrm| instead to
keep the size switching mechanism active. But pay attention,
\ci{mathrm} will only work well on short items. Spaces are still not
active and accented characters do not work\footnote{The AMS-\LaTeX{}
  package makes the \ci{textrm} command work with size changing.}.

\begin{example}
\begin{equation}
2^\textrm{nd} \quad 
2^\mathrm{nd}
\end{equation}
\end{example}

Sometimes you need to tell \LaTeX{} about the correct font size
nevertheless. In math mode the fontsize is set with the four
commands: 
\begin{flushleft}
\ci{displaystyle}~($\displaystyle 123$),
 \ci{textstyle}~($\textstyle 123$), 
\ci{scriptstyle}~($\scriptstyle 123$) and
\ci{scriptscriptstyle}~($\scriptscriptstyle 123$).
\end{flushleft}

Changing styles also affects the way limits are displayed.

\begin{example}
\begin{displaymath}
\mathop{\mathrm{corr}}(X,Y)= 
 \frac{\displaystyle 
   \sum_{i=1}^n(x_i-\overline x)
   (y_i-\overline y)} 
  {\displaystyle\biggl[
 \sum_{i=1}^n(x_i-\overline x)^2
\sum_{i=1}^n(y_i-\overline y)^2
\biggr]^{1/2}}
\end{displaymath}    
\end{example}
% This is not a math accent, and no maths book would be set this way.
% mathop gets the spacing right.

\noindent This is one of those examples in which we need larger
brackets than the standard \verb|\left[  \right]| provides.


\section{Theorems, Laws, \ldots}

When writing mathematical documents, you probably need a way to
typeset ``Lemmas'', ``Definitions'', ``Axioms'' and similar
structures. \LaTeX{} supports this with the command
\begin{command}
\ci{newtheorem}\verb|{|\emph{name}\verb|}[|\emph{counter}\verb|]{|%
         \emph{text}\verb|}[|\emph{section}\verb|]|
\end{command}
The \emph{name} argument is a short keyword used to identify the
``theorem''. With the \emph{text} argument you define the actual name
of the ``theorem'' which will be printed in the final document.

The arguments in square brackets are optional. They are both used to
specify the numbering used on the ``theorem''. With the \emph{counter}
argument you can specify the \emph{name} of a previously declared
``theorem''. The new ``theorem'' will then be numbered in the same
sequence.  The \emph{section} argument allows you to specify the
sectional unit within which you want your ``theorem'' to be numbered.

After executing the \ci{newtheorem} command in the preamble of your
document, you can use the following command within the document.

\begin{code}
\verb|\begin{|\emph{name}\verb|}[|\emph{text}\verb|]|\\
This is my interesting theorem\\
\verb|\end{|\emph{name}\verb|}|     
\end{code}

This should be enough theory. The following examples will hopefully
remove the final remains of doubt, and make it clear that the
\verb|\newtheorem| environment is way too complex to understand.

\begin{example}
% definitions for the document
% preamble
\newtheorem{law}{Law}
\newtheorem{jury}[law]{Jury}
%in the document
\begin{law} \label{law:box}
Don't hide in the witness box
\end{law}
\begin{jury}[The Twelve]
It could be you! So beware and
see law \ref{law:box}\end{jury}
\begin{law}No, No, No\end{law}
\end{example}

The ``Jury'' theorem uses the same counter as the ``Law''
theorem. Therefore, it gets a number which is in sequence with
the other ``Laws''. The argument in square brackets is used to specify 
a title or something similar for the theorem.

\begin{example}
\flushleft
\newtheorem{mur}{Murphy}[section]
\begin{mur}
If there are two or more 
ways to do something, and 
one of those ways can result 
in a catastrophe, then 
someone will do it.\end{mur}
\end{example}

The ``Murphy'' theorem gets a number which is linked to the number of
the current section. You could also use another unit, like  chapter or
subsection for example.

\section{Bold symbols}
\index{bold symbols}

It is quite difficult to get bold symbols in \LaTeX{}; this is 
probably intentional as amateur typesetters tend to overuse them.
The font change command \verb|\mathbf| gives bold letters, but these are
roman (upright) whereas mathematical symbols are normally italic.
There is a \ci{boldmath} command, but \emph{this can only be
used outside mathematics mode}. It works for symbols too.

\begin{example}
\begin{displaymath}
\mu, M \qquad \mathbf{M} \qquad
\mbox{\boldmath $\mu, M$}
\end{displaymath}
\end{example}

\noindent
Notice that the comma is bold too, which may not be what is required.

The package \pai{amsbsy} (included by \pai{amsmath}) makes this much
easier.  It includes a \ci{boldsymbol} command, and ``poor man's
bold'' \ci{pmb} which works by over-striking for systems without the
fonts required for bold symbols.

\ifx\boldsymbol\undefined\else
\begin{example}
\begin{displaymath}
\mu, M \qquad
\boldsymbol{\mu}, \boldsymbol{M}
\qquad \pmb{\mu}, \pmb{M}
\end{displaymath}
\end{example}
\fi


%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "lshort2e"
%%% End: 


