\title{Maths in \protect\LaTeX: Part~5, Getting started on arrays}
%%%
\author[R.~A.~Bailey]{R.~.A.~Bailey\\
Queen Mary and Westfield College\\University of London}
%%%
%%% here are extra environments that I have used in this article.
%%% I hope that they do not conflict with anything of the editor's.
%%%
%%%
\newcommand{\writer}[1]{{\sc #1}:}
\newcommand{\book}[1]{{\it #1},}
\newcommand{\publish}[2]{{\rm #1, #2,}}
\newcommand{\byear}[1]{{\rm (#1).}}
\newtheorem{preqn}{Exercise}
\newenvironment{qn}{\preqn\rm}{\endpreqn}
%%% Dear Mr Editor, I should like the content of exercises
%%% to come out in what all copy-editors call ROMAN, not in what
%%% Frank Mittelbach calls roman. And this should be irrespective
%%% of the surrounding text.
%%%
\newcommand{\latexword}[1]{{\tt #1}}
%%% and the above should be in typewriter-ordinary IRRESPECTIVE of the
%%% surrounding fonts
%%%
%%%
%%% editor: I have been as generic as I can, but of course you can't
%%% put \verb inside a \newcommand. I have consistently used + as the
%%% delimiter for \verb, except when I needed the + in Maths!
%%%
\newcommand{\lamport}{{\it \LaTeX: A Document Preparation System\/} by Leslie 
Lamport}
\newcommand{\shortlamp}{{\it The Manual}}
\let\torl\AllTeX
\newcommand{\phz}{\phantom{0}}
\newcommand{\dfrac}[2]{\displaystyle\frac{#1}{#2}}
\newcommand{\CS}{\mathop{{\rm CS}}\nolimits}
\newcommand{\normof}[1]{{\left\Vert#1\right\Vert}^2}
\newcommand{\littlehead}[1]{\textit{#1}}

\begin{Article}
\section*{Recall}
This is the fifth part of a sequence of tutorials on typesetting Mathematics in 
\LaTeX. The first four appeared in issues~4.4, 4.5, 5.1 and~5.2 of \BV. 
The series
includes some things which can be found in \cite{leslie}, but I am 
working in more things which, while straightforward and necessary for 
Mathematical work, are  not in \cite{leslie} or \cite{newleslie}. In this 
tutorial and the next
I cover arrays: matrices, tables of data, aligned equations, and other
items with a two-dimensional layout.
 
I do not cover the extra array goodies 
provided by packages such as Frank Mittelbach's \latexword{array.sty}, David 
Carlisle's plethora of array add-ons or the new packages from the American 
Mathematical Society. I hope that someone else will write a follow-on article 
describing some or all of those, particularly if they can give easier methods 
of doing some of the more cumbersome items in this tutorial.

In case you missed the previous
tutorials, I remind you that
I expect you, the reader, to do some work. Every so 
often comes a group of exercises, which you are supposed to do. Use \LaTeX\ to 
typeset everything in the exercise except sentences in italics, which are 
instructions. If you are not satisfied that you can do the exercise, then tell 
me. Either write to me at
\begin{verse}
School of Mathematical Sciences\\
Queen Mary and Westfield College\\
Mile End Road\\
London E1 4NS
\end{verse}
with hard copy of your input and output, 
or email me at \mbox{\tt r.a.bailey@qmw.ac.uk}
with a copy of the 
smallest possible piece of \LaTeX\ input file that contains your
attempt at the answer.
In either case
I will include a solution in the following issue of \BV: you will remain 
anonymous if you wish.

\addtocounter{section}{7}
\section{Mathematical arrays}
\subsection{Basics}
The \latexword{array} environment is used to set all manner of aligned 
Mathematics. A simple example is:
\[
\begin{array}{l}
\verb+\begin{array}{lrrr}+\\
\verb+x & 1 & 2 & 5\\+\\
\verb+x^2 & 1 & 4 & 25 \\+\\
\verb+\end{array}+
\end{array}
\quad \qquad
\begin{array}{lrrr}
x & 1 & 2 & 5\\
x^2 & 1 & 4 & 25 
\end{array}
\]
This array has four columns. The alignment of these columns is shown in the 
argument \latexword{lrrr} of the environment. The first column is left-aligned;
the other three are right-aligned. It is also possible to have a centred 
column, shown by a~\latexword{c}. Typically columns of Mathematical symbols are
centred, integers are right-aligned and powers of a single symbol are 
left-aligned.
>From now on I shall refer to the argument such as \latexword{lrrr} as the
{\em columns specifier}.

Entries in each row of the \latexword{array} are separated by ampersands. 
The end of a row 
(except the last one) is shown by \verb+\\+. If the last few entries in a row 
are blank there is (usually) no need to put in all the ampersands.

The \latexword{array} environment can be used only in Maths mode. All its 
entries are automatically in Maths mode, in \verb+\textstyle+. 
If you have fractions or summations in an array you may want to precede them by
\verb+\displaystyle+.

Each entry is in a separate `box', 
which is typeset before the array is built up. Thus switches such as 
\verb+\bf+, \verb+\displaystyle+ and \verb+\raggedright+ extend no further than
the end of the current entry. If a binary relation or operator, such as~$=$ 
or $+$, occurs at the beginning or end of an entry  \torl\ cannot give it the 
proper spacing relative to the previous or succeeding entry.

\subsection{Matrices and vectors}
The most obvious use for \latexword{array} is for a matrix. Use \verb+\left+ 
and \verb+\right+ to surround the matrix with fences of the right size.
For example, the matrix in Figure~\ref{mv} begins
\begin{verbatim}
     \left(
     \begin{array}{rrr}
\end{verbatim}

\begin{figure*}
\begin{minipage}{.5\textwidth}
\[
\left(
\begin{array}{rrr}
2 & -1 & -1\\
-1 & 3 & -1\\
-1 & -1 & 4
\end{array}
\right)
\left(
\begin{array}{r}
5\\-3\\8
\end{array}
\right)
 = 
\left(
\begin{array}{r}
5\\-22\\30
\end{array}
\right)
\]
\caption{A matrix equation}
\label{mv}
\end{minipage}
\begin{minipage}{.5\textwidth}
\[
\renewcommand{\arraystretch}{2}
\det \left [
\begin{array}{cccc}
\dfrac{\partial h_1}{\partial y_1} & \dfrac{\partial h_1}{\partial y_2} & 
    \cdots & \dfrac{\partial h_1}{\partial y_n}\\
\dfrac{\partial h_2}{\partial y_1} & \dfrac{\partial h_2}{\partial y_2} & 
   \cdots &  \dfrac{\partial h_2}{\partial y_n}\\
\vdots &  \vdots &\ddots & \vdots\\
\dfrac{\partial h_n}{\partial y_1} & \cdots & \cdots &
   \dfrac{\partial h_n}{\partial y_n}
\end{array}
 \right]
\]
\caption{The Jacobian of~$h$}
\label{jacob}
\end{minipage}
\end{figure*}

The commands \verb+\vdots+ and \verb+\ddots+ are useful in matrices. They are 
both used in Figure~\ref{jacob}.


An array can have a single column, as in the column vectors in Figure~\ref{mv},
or a single row, which is useful for a permutation or a row vector.

\subsection{Arrays of arrays}
Try to think logically, rather than visually, about the contents of an array.
For example, if you have six $5\times 5$ Latin squares set out in a $2 \times 
3$ rectangle then you might think that you have $10~$rows and $15$~columns. 
However, it is more logical to do \verb+\begin{array}{ccc}+ and then make each 
entry in this array a new array with five centred columns. One advantage of 
this approach is that it makes editing
the file much easier when you decide to 
change the order of the Latin squares, or to lay them out in a $3\times 2$ 
rectangle.

\subsection{Changing the row spacing}
The default spacing between the rows of an \latexword{array} is fine for simple
matrices with numbers in, but is often insufficient for large entries, 
particularly if you use \verb+\displaystyle+. To adjust the amount of space 
after the current row, put an explicit length in square brackets after the 
\verb+\\+: for example, \verb+\\[20pt]+. 

I find it useful to work in multiples 
of \verb+\jot+: this length is the usual extra space between lines of displayed
Mathematics. Curiously, \verb+\jot+ is equal to $3$~points in all of \LaTeX's 
three point sizes, so an adjustment in a $10$~pt document may not be correct if 
you change the document to $11$~pt. If you work in multiples of 
the length \verb+\baselineskip+
then no extra adjustment is needed if the point size of the 
document is changed.

Because of the complicated way in which the row-spacing in an array is adjusted
to fit the contents, the length given in \verb+[  ]+ is not exactly an {\em 
addition}. You have to experiment to find out what works best: doubling the 
length given does not usually double the space between the rows.

The existence of this optional length parameter to \verb+\\+ implies that no row
of an array may start with \verb+[+. If you need to do this, 
precede it with an emtpy pair of braces: \verb+{}[...+.

Changing the inter-row spacing for the whole array is much simpler than 
changing it for a single row. This spacing is governed by the value of the
number
\verb+\arraystretch+, whose default setting is~$1$. 
Between the rows of an array, the length \verb+\baselineskip+ is multiplied by
\verb+\arraystretch+.
In Figure~\ref{jacob}, 
the rows have been spread out by putting
\begin{verbatim}
     \renewcommand{\arraystretch}{2}
\end{verbatim}
before the \verb+\begin{array}+. However, this command was given {\em after\/} 
the start of the displayed Maths, so that it remained local to that particular 
display. If you experiment with other values of \verb+\arraystretch+ in this 
example you will find that there is virtually no separation between the first 
two rows  when \verb+\arraystretch+ is smaller than~$2$.
Figure~\ref{ima} also shows an array in which \verb+\arraystretch+
has been altered.

Arrays inside arrays can have different values of \verb+\arraystretch+.
For example, in
\begin{verbatim}
     \[
     \renewcommand{\arraystretch}{2}
     \begin{array}{ccc} 
     A & B & 
        \renewcommand{\arraystretch}{1.2}
        \begin{array}{lr}
        ...
\end{verbatim}
the outer array has \verb+\arraystretch+ equal to~$2$, while the inner 
array which is the third entry in the first row 
has \verb+\arraystretch+ equal to~$1.2$. 

\begin{figure*}
\[
\renewcommand{\arraystretch}{2}
\addtolength{\arraycolsep}{1.5\arraycolsep}
\begin{array}{cccp{1.5in}c}
W_{1_G} & V_0 & 1 & one stratum\\
W_\psi &\left(V_{\rm rows} + V_{\rm cols} \right) \cap V_0^\perp &
2(q^2 + q) & 
\raggedright
direct sum of two
unidentifiable strata
&
\\
W_\eta &\left(V_{\rm rows} + V_{\rm cols} \right) ^\perp & q^3 & one stratum
\end{array}
\]
\caption{A table of mathematical information}
\label{ima}
%This gives four candidates for automorphisms:
\[
\begin{array}{l@{\colon p + q\omega + r \omega^2 + 
s\omega^3 + t\omega^4 \mapsto p + 
{}}r@{\omega + {}}r@{\omega^2 + {}}r@{\omega^3 + {}}r@{\omega^4}}
\alpha_1 & q & r & s & t\\
\alpha_2 & s & q & t & r\\
\alpha_3 & r & t & q & s\\
\alpha_4 & t & s & r & q 
\end{array}
\]
\caption{Four automorphisms of a splitting field}
\label{galois}
\end{figure*}

\subsection{Changing the column spacing}
The default inter-column spacing in an array is one quad. To change the spacing
between a pair of adjacent columns, use \verb+@{...}+ between their alignment 
codes in the columns specifier, putting  inside the braces
the actual horizontal space 
required. For example,
\verb+\begin{array}{cc@{\qquad}c@{}c}+ 
produces an array with four centred columns in which the inter-column spaces 
are, in order, one quad, two quads and zero.

At the start and end of the array there is usually a space of width half the 
inter-column space. These can be suppressed by putting \verb+@{}+ at the start 
and end of the columns specifier.

The overall inter-column spacing is controlled by the value of the length
\verb+\arraycolsep+, which is half the default inter-column space. 
This can be changed by using \verb+\addtolength+ in the 
usual way. I like to alter \verb+\arraycolsep+ by multiples of itself.
%, so that 
%the changes are correct no matter what the point size of the document. 
For example, to treble the size of \verb+\arraycolsep+ do
\begin{verbatim}
  \addtolength{\arraycolsep}{2\arraycolsep}
\end{verbatim}
while to halve it make the last parameter \verb+-0.5\arraycolsep+. 
In Figure~\ref{ima}, \verb+\arraycolsep+ has been multiplied by~$2.5$.

Finally, the command \verb+\extracolsep+ inside an \verb+@{...}+
can be used to insert extra space to the left of all subsequent columns:
for example, \verb+@{\extracolsep{1em}}+. This is not suppressed by a 
subsequent \verb+@{...}+.

My remarks about quads above are not strictly true unless your current text font
is Computer Modern roman in $10$~pt. A quad length is defined to be $1$~em, 
whose size depends on the current font, whereas \verb+\arraycolsep+ is defined 
to be $5$~pt, irrespective of the point size of the document. I find it very 
odd that \LaTeX, which has been so carefully set up so that the user can 
apparently change painlessly
between $10$~pt, $11$~pt and $12$~pt, does not change the
size of Maths lengths like \verb+\jot+ and \verb+\arraycolsep+ in the different
point sizes.

\subsection{Words in arrays}
The odd word or two in an array can be put in by using \verb+\mbox+. But what 
should you do when you have a verbal phrase extending over more than one line, 
as in the final column of Figure~\ref{ima}?

Most people's first thought is to have a separate line of the array for each 
line of text. As usual, this visual approach is inferior to the logical one 
when it comes to revising your file. Hard experience has shown me that you have
to keep altering which words go on which lines as you alter other parts of the 
array. It is better to use the fourth type of column code~\verb+p+, which 
creates a paragraph box of a specified width. Thus the 
columns specifier in Figure~\ref{ima} is actually
\verb+cccp{1.5in}+. Just guess a length to put inside 
\verb+p{...}+ at first: changing it later is much simpler than shuffling words 
around.

Figure~\ref{ima} demonstrates another good reason to use the \verb+p+ code.
The rows are far apart, because \verb+\arraystretch+ has been magnified. But we
{\em don't\/} want the lines of text at the end of the second row to be that 
far apart. We could make an inner array with a smaller value of
\verb+\arraystretch+, but it's simpler to use~\verb+p+.

The only problem with the naked \verb+p+ columns is that they are set 
justified on both sides, which is absurd in narrow columns. I usually put 
\verb+\centering+ or \verb+\raggedright+ at the beginning of every entry in 
such a column. Unfortunately, you cannot do this in the final column of an
array, because \LaTeX\ gets confused about whether \verb+\\+ refers to the row 
of the array or the line in the paragraph. If necessary, I get round this by 
adding a dummy unused column at the end of the array. Thus I told a lie above:
the columns specifier in Figure~\ref{ima} is really
\verb+cccp{1.5in}c+. 

\subsection{Inter-column constants}
The code \verb+@{...}+ can be used for putting anything between two columns 
that must appear in every row, not just spaces. This is convenient for any 
repeated items. For example, the columns specifier in 
Figure~\ref{galois} is
\begin{verbatim}
  {l@{\colon p + q\omega + r \omega^2 + 
  s\omega^3 + t\omega^4 \mapsto p + 
  {}}r@{\omega + {}}r@{\omega^2 + 
  {}}r@{\omega^3 + {}}r@{\omega^4}}
\end{verbatim}
while the first row is just 
\begin{verbatim}
     \alpha_1 &q & r & s & t
\end{verbatim}
Notice the pairs \verb+{}+ that have been inserted to make $+$ behave 
as a binary operator.


\subsection{Positioning}
By default, each array is positioned so that it is centred vertically. In 
Figure~\ref{mv}, the equals sign is aligned with the centre of each of the 
three arrays. To align an array on its top row, start it with
\verb+\begin{array}[t]{...}+;
to align it on its bottom row, use \verb+[b]+ in place of \verb+[t]+.

These positionings work well for simple tables of numbers. However, the 
logical interaction between them and \verb+\left+ and \verb+\right+ fences 
produces bizarre results (try it!). If you want matrices aligned on their top 
rows, as in Figure~\ref{topmat}, you have to do something quite complicated, 
which I shall come back to later.

\begin{figure*}
\newcommand{\topthing}[1]{\begin{array}{@{}c@{}}#1\\\phz\end{array}}
\newcommand{\vecmu}{\mbox{\boldmath$\mu$}}
\newcommand{\vecx}{{\bf x}}
\[
\topthing{3(\vecx - \vecmu)^{\rm T} \Sigma^{-1} (\vecx - \vecmu) = (x-1,y+2)} 
\left[\begin{array}{rr}
4&1\\1&1
\end{array}
\right] 
\left(\begin{array}{c}
x-1\\y+2
\end{array}
\right)
\]
\caption{Matrices aligned on their top rows}
\label{topmat}
\end{figure*}

\subsection{Cases}
We often write equations where the value on the right-hand side depends on some
condition, as in
%The $(\omega,t)$-entry of~$X$ is defined by
\begin{equation}
X(\omega,t) = \cases{1 & if $\phi(\omega) = t$\cr
     0 & otherwise.}
\label{eqncase}
\end{equation}
You can do the right-hand side of this using an \latexword{array}, but it is
simpler to use the direct plain \TeX\ command \verb+\cases+. The right-hand 
side of Equation~(\ref{eqncase}) was produced with
\begin{verbatim}
    \cases{1 & if $\phi(\omega) = t$\cr
           0 & otherwise.}
\end{verbatim}
The argument of \verb+\cases+ is like a two-column array, with its rows 
separated by \verb+\cr+ instead of \verb+\\+. The first entry in each row is in
Maths mode; the second is in non-Maths mode. Both
columns are left-aligned.

\subsection{Binomial coefficients}
If you write your binomial coefficients as
$6 \choose 2$ rather than ${}^6{\rm C}_2$ 
you may be tempted to build them with an
\latexword{array}. Don't. Use the plain \TeX\ infix command \verb+\choose+, as 
follows.
\[
\verb+6 \choose 2+ \quad \qquad {6 \choose 2}
\]
It comes out rather differently in \verb+\textstyle+ and in 
\verb+\displaystyle+.

Because \verb+\choose+ does not have its two arguments in braces, it is
safest if you enclose the whole expression in grouping braces, as in
\verb+{6 \choose 2}+, to ensure that \verb+\choose+ does not search too far 
back or too far forward for its arguments.
If you need to use this a lot, make a \LaTeX-style macro such as
\begin{verbatim}
     \newcommand{\binom}[2]{{#1\choose#2}}
\end{verbatim}


The command \verb+\atop+ works similarly, but omits the parentheses.

\section*{Exercises}
\addtocounter{preqn}{54}
\begin{qn}
The matrix $\left[\begin{array}{cc}
1 & 3\\
3 & 5
\end{array}
\right]$
is symmetric.
\end{qn}

\begin{qn}
The determinant is given by
\[
\det A = \left |
\begin{array}{ccc}
a_{11} & \ldots & a_{1n}\\
\vdots & \ddots & \vdots\\
a_{n1} & \ldots & a_{nn}
\end{array}
\right|
\]
\end{qn}

\begin{qn}
For a rotation,
\[
\left(\begin{array}{rr}
\cos \theta & \sin \theta\\
-\sin \theta & \cos \theta
\end{array}
\right)
\left(\begin{array}{c}x \\ y\end{array}\right)
 = 
\left(\begin{array}{c}
x\cos \theta + y\sin \theta\\
y\cos\theta - x\sin\theta
\end{array}
\right).
\]
\end{qn}

\begin{qn}
Here is a pair of mutually orthogonal Latin squares:
\[
\begin{array}{cc}
\begin{array}{ccc}
A & B & C\\
B & C & A\\
C & A & B\\
\end{array}
&
\begin{array}{ccc}
\alpha & \beta & \gamma\\
\gamma & \alpha & \beta\\
\beta & \gamma & \alpha
\end{array}
\\ \\
\mbox{Square 1} & \mbox{Square 2}
\end{array}
\]
\end{qn}

\begin{qn}
A few real functions:
\[
\begin{array}{lp{2in}c}
x \mapsto x^2 & \raggedright differentiable everywhere & \\
x \mapsto \left| x\right | & \raggedright 
differentiable everywhere except at the origin &\\
x \mapsto \lfloor x \rfloor & \raggedright 
continuous everywhere except at the integers
\end{array}
\]
\end{qn}

\begin{qn}
\it Typeset the table in Figure~\ref{ima}.
\end{qn}

\begin{qn}
\addtolength{\arraycolsep}{-0.5\arraycolsep}
Pascal's Triangle begins
\[
\begin{array}{*{13}{c}}
 & & & & & & 1 & & & & & & \\
 & & & & & 1 & & 1 & & & & & \\
 & & & & 1 & & 2 & & 1 & & & &\\
 & & & 1 & & 3 & & 3 & & 1 & & &\\
 & & 1 & & 4 & & 6 & & 4 & & 1 & &\\
 & 1 & & 5 & & 10 & & 10 & & 5 & & 1 &\\
 1 & & 6 & & 15 & & 20 & & 15 & & 6 & & 1
\end{array}
\]
\end{qn}

\begin{qn}
The Stirling numbers of the second kind are defined by
\[
S(n,k) =  \frac{1}{k!} \sum_{j=1}^{k} (-1)^{k-j} {k \choose j} j^n.
\]
\end{qn}

\begin{qn}
If the random variable $X$ is symmetric about~$0$ and if $Y = X^2$ then
\[
f_Y(y) = \cases{ 0 & if $y \leq 0$ \cr
          \dfrac{f_X(\sqrt{y})}{\sqrt{y}} & if $y > 0$.}
\]
\end{qn}

\begin{qn}
\[
\left[
\begin{array}{cccc}
\sigma_{11} & \sigma_{12} &\cdots & \sigma_{1n}\\
\sigma_{21} & \sigma_{22} &\cdots & \sigma_{2n}\\
 \vdots & \cdots & \ddots& \vdots\\
\sigma_{1n} & \cdots & \cdots & \sigma_{nn}
\end{array}
\right]
\]
\end{qn}

\begin{qn}
\[
\begin{array}{ccccccccc}
M_0 & \subseteq & M_1 & \subseteq & M_2 & \subseteq & \cdots & \subseteq & 
M_m\\
\uparrow && \uparrow && \uparrow\\
\mbox{null} && \mbox{linear} && \mbox{quadratic}
\end{array}
\]
\end{qn}

\section{Tables of data}
\subsection{General}
The extra topics 
that I cover in this section are most pertinent to tables of data, but do 
sometimes arise in Mathematical arrays. Likewise,
many considerations about Mathematical arrays also apply to tables of data.
For example, the table in Figure~\ref{eel} does not really have eight columns:
it has two long columns but has been broken down into four parts to fit on the 
page nicely. So it is set as an array with one row and four 
columns, each of whose entries is an array with two columns. 

\LaTeX\ has another environment, called \latexword{tabular}, which is very 
similar to \latexword{array}. It may be used inside or outside Maths mode. By 
default, each of its entries is set in ordinary mode. However, any
extra space on either side of the ampersands has no effect.  
The length \verb+\arraycolsep+ is replaced by the length 
\verb+\tabcolsep+.
Tables of data often have
a mix of words and symbols, and you have to decide which is less trouble: using 
an \latexword{array} and putting the words in \verb+mbox+es or 
\verb+p+~columns, or using a \latexword{tabular} and putting the symbols in 
\verb+$...$+. If the latter, don't forget to put numbers in \verb+$...$+ if 
they involve a minus sign.

\begin{figure*}
\[
\begin{tabular}{c|c|c|c}
\begin{tabular}{cc}
Year 1 & Year 2\\
\hline
269 & 466\\
100 & 219\\
197 & 421\\
216 & 708\\
\end{tabular}
&
\begin{tabular}{cc}
Year 1 & Year 2\\
\hline
134 & 590\\
\phz 74 & 137\\
\phz 88 & 356\\
\phz 25 & 212
\end{tabular}
&
\begin{tabular}{cc}
Year 1 & Year 2\\
\hline
211 & 505 \\
102 & 363\\
191 & 563\\
\phz 67 & 338\\
\end{tabular}
&
\begin{tabular}{cc}
Year 1 & Year 2\\
\hline
209 & 352\\
\phz 29 & 254\\
\phz 19 & 106\\
\phz 44 & 268
\end{tabular}
\end{tabular}
\]
\caption{Number of eelworms in two successive years}
\label{eel}
\end{figure*}

\subsection{Horizontal rules}

\begin{figure*}[hbtp]
\[
\begin{tabular}{l@{\qquad}l@{\qquad}lrrrrrrrrr}
\hline
 & & & \multicolumn{3}{c}{1961} & \multicolumn{3}{c}{1971} & 
\multicolumn{3}{c}{1981}\\
\hline
\multicolumn{3}{l}{Billion passenger kilometres travelled by:}\\
 & \multicolumn{2}{l}{Air} && 1 &&& 2 &&& 3&\\
 & \multicolumn{2}{l}{Rail} && 39 &&& 36 &&& 34&\\
 & \multicolumn{2}{l}{Road}\\
 & & Buses and coaches && 67 &&& 51 &&& 42&\\
 & & Cars, taxis and two-wheeled motor vehicles && 142 &&& 284 &&& 380&\\
 & & Bicycles && 10 &&& 4 &&& 4&\\
\cline{4-12}
 & \multicolumn{2}{l}{Total} && 259 &&& 377 &&& 406&\\
\hline
\end{tabular}
\]
\caption{Passenger transport (from \protect\cite{keyd})}
\label{cars}
\end{figure*}

To obtain a horizontal rule stretching the full width of the array, use 
\verb+\hrule+. Don't forget to finish the previous row with \verb+\\+, even if
it is the last row of the array, as at the end of Figure~\ref{cars}. Two or 
more \verb+\hrule+s can follow each other with no intervening \verb+\\+. 
A little vertical space is put between two such horizontal rules.
However, any vertical rules in the array (see below) will be suppressed between
the successive horizontal rules.

For a horizontal rule spanning only columns \mbox{$2$--$4$}, say, use 
\verb+\cline{2-4}+. Again, finish the preceding row with \verb+\\+. 
Figure~\ref{cars} shows an example of this.
Successive 
\verb+\cline+s will be interpreted as being in the same row, as in 
\verb+\cline{2-4}\cline{7-7}+, for example, so that
\verb+\cline{2-4}\cline{2-4}+ does {\em not\/} produce two horizontal lines 
with a little space between them. Likewise, there is no space between an 
\verb+\hline+ and an immediately preceding or succeeding \verb+\cline+.

\subsection{Vertical rules}

A vertical rule is obtained by putting \verb+|+ at the appropriate place in the
columns specifier. For example, the columns specifier for the 
\latexword{tabular} in Figure~\ref{travel} is
\begin{verbatim}
     |r@{ Month}l@{\quad\pounds}r|
\end{verbatim}
Two or more \verb+|+s may come next to each other.

\begin{figure*}
\begin{minipage}{.5\textwidth}
\[
\begin{tabular}{|r@{ Month}l@{\quad\pounds}r|}
\hline
\multicolumn{3}{|c|}{Premiums}\\
\hline
1 & & 18\\
3 & s & 48\\
6 & s & 95 \\
9 & s & 136\\
\hline
\end{tabular}
\]
\caption{Travel insurance}
\label{travel}
\end{minipage}
\begin{minipage}{.5\textwidth}

\[
\begin{tabular}{lr@{}l}
Bootstrap & 0.301\\
Jackknife & 0.314\\
Delta method & 0.244& *\\
Half-samples & 0.364 & \dag\\
Random subsampling & 0.423 & \ddag\\
Normal theory & 0.302
\end{tabular}
\]
\caption{Sticking out to the right (from \protect\cite{brad})}
\label{efron}
\end{minipage}
\end{figure*}

For a vertical rule in a single item, stretching from the top to bottom of its 
row, use \verb+\vline+. This cannot be used in a \verb+p+~column, but may be 
put inside \verb+@{...}+.

If you omit the trailing ampersands in a row with few items the trailing 
vertical rules will also be omitted. This can be a nuisance in a sparse table.
If you want a vertical rule at each side of a sparse table, consider doing 
this:
\begin{verbatim}
     \begin{array}{|c|}
     \begin{array}{@{}...@{}}
     ...
     \end{array}
     \end{array}
\end{verbatim}

The length \verb+\arrayrulewidth+ controls the thickness of  all the horizontal
and vertical rules just described. The width of the space between the vertical 
rules created by \verb+||+ in a the columns specifier is controlled by the 
length \verb+\doublerulesep+, as is the vertical space between two successive 
\verb+\hline+s.

\subsection{Spanning several columns}
The \verb+\multicolumn+ command is used to create an entry spanning several 
columns. It takes three arguments. The first is the number of columns to be 
spanned; the second is the code for the type of column to be used;
the third is the text of the entry. 
\begin{verbatim}
     \begin{tabular}{crrrr}
     Type & \multicolumn{4}{c}{Grade}\\
     \cline{2-5}
      & 12 & 16 & 24 & 30\\
     \hline
     $A$ & 0 & 1 & 21 & 24\\
     $B$ & 1 & 6 & 24 & 13
     \end{tabular}
\end{verbatim}
\[
\begin{tabular}{crrrr}
Type & \multicolumn{4}{c}{Grade}\\
\cline{2-5}
 & 12 & 16 & 24 & 30\\
\hline
$A$ & 0 & 1 & 21 & 24\\
 $B$ & 1 & 6 & 24 & 13
\end{tabular}
\]

Any \verb+|+ in the columns specifier is 
regarded as belonging to its preceding column, and may be overwritten by
\verb+\multicolumn+ unless it is included in the second argument. The exception
is any \verb+|+ to the left of the first column, which is regarded as part of 
the first column.
For example, the first row in Figure~\ref{travel} is
\begin{verbatim}
     \multicolumn{3}{|c|}{Premiums}\\
\end{verbatim}
Expressions \verb+@{...}+ in the columns specifier are similarly assigned to 
columns and similarly overwritten.

Indented row labels for subheadings can also be conveniently obtained by using 
\verb+\multicolumn+. The \latexword{tabular} in Figure~\ref{cars} begins with 
three \verb+l+~columns. The first row after the second horizontal rule is a 
\verb+\multicolumn+ spanning three columns; the next begins with \verb+&+ 
followed by a 
\verb+\multicolumn+ spanning two columns.

Unfortunately, if the width of a multicolumn entry spanning three or more 
columns is more than the total width of the spanned columns, \LaTeX\ cannot 
adjust the spacing between those columns to make it equal: it puts all the 
extra space to the left of the last of the spanned columns. The only way that
I know
around this is to use \verb+@{...}+ to put the same amount of extra space 
between each adjacent pair of the spanned columns.

\subsection{Changing the type of an entry}
Although its name does not suggest it, the command
\verb+\multicolumn+ is also used to 
change the type of a single entry. This is most often used to give a centred 
heading to a column of right-aligned data.
\begin{verbatim}
     \begin{array}{rrr}
     \multicolumn{1}{c}{n} & 
     \multicolumn{1}{c}{n^2} &
     \multicolumn{1}{c}{\phi(n)}\\
     9 & 81 & 6\\
     10 & 100 & 4\\
     11 & 121 & 10
     \end{array}
\end{verbatim}
\[
\begin{array}{rrr}
\multicolumn{1}{c}{n} & \multicolumn{1}{c}{n^2} 
& \multicolumn{1}{c}{\phi(n)}\\
9 & 81 & 6\\
10 & 100 & 4\\
11 & 121 & 10
\end{array}
\]
As this example shows, this works fine when the centred heading is narrower 
than the widest other entry in the column. However, when it is wider then all 
the other entries in the column are aligned with its right-hand edge, when 
probably what you wanted was to have them right-aligned among themselves and 
then all centred under the heading.
How can this be fixed?

If most of the numbers are the same length, a reasonable solution is to make the
whole column centred and put in \verb+\phantom{0}+s to make the shorter numbers
as long as the longest. This works because all digits have the same width.
 That is what I did in Figure~\ref{eel}.

I adopted a different solution in Figure~\ref{cars}, where I decided that the 
year headings, not being numbers to be added to other numbers in their columns,
should be centred. This time I used the columns specifier \verb+rrr+ for each 
column of data. In the headings I used \verb+\multicolumn{3}{c}{...}+ and in 
the data rows I left one blank entry either side of each data entry. The result
is rather widely spaced: space could be saved by using \verb+@{...}+ between 
each data column and its blank neighbours.

The command
\verb+\multicolumn{1}+ can also be used to remove a vertical rule or the 
contents of an \verb+@{...}+ from one particular row. For example, if the 
columns specifier is \verb+l|r+ then the vertical rule is missing from any row 
whose first entry is made with \verb+\multicolumn{1}{l}+.

\subsection{Decimal tabulation}
Sometimes a column of numbers with decimal points should be aligned
on those decimal points. If they all have the same number of digits after the 
point, there is no problem: simply use an \verb+r+ column. If only a few numbers
are missing a few digits after the point, fill in the spaces with 
\verb+\phantom{0}+ and still use an \verb+r+ column. Only if you have a long 
column of numbers with very variable numbers of digits after the point is it 
worth going to the trouble of using the columns specifier \verb+r@{.}l+ and 
entering a number such as 3.142 as \verb+3 & 142+.

\subsection{Things that stick out}
Sometimes you have a column of numbers that should be right-aligned but some of
the entries have accretions that should stick out to the right, as in 
Figure~\ref{efron}, or a left-aligned column with pieces sticking out to the 
left. There are three ways of dealing with this, each appropriate in different 
circumstances.


If the pieces that stick out are all digits, use \verb+\phantom{0}+ as 
described above.

Sometimes the piece that sticks out is very small and occurs in only one row. 
If a displayed Mathematical array ends the sentence 
then sometimes the full stop needs to
come outside the array, vertically centred (with \verb+\end{array}.\]+); but 
sometimes the final row of the array
needs a full stop at the end of it without upsetting the column alignment.
Then the trick is to add the full stop in such a way that \torl\ thinks that it
doesn't take up any space. According to \cite[page~98]{leslie}, you do this 
with
\verb+\makebox[0pt][l]{.}+, but the plain \TeX\ \verb+\rlap{.}+ is shorter and 
achieves the same effect. For a small item sticking out on the left use
\verb+\makebox[0pt][r]{ }+ or \verb+\llap{ }+. Of course, the content of 
\verb+\makebox{ }+ or \verb+\rlap+ or \verb+\llap+ is not in Maths mode.

Statisticians sometimes put a question mark to indicate dubious data, and 
scientists often decorate numbers with stars to show statistical significance.
In such cases it is best to use the columns specifier \verb+r@{}l+ for the 
column: the \verb+r+ part is used for the numbers and the \verb+l+ part is used
for any decorations. 
In Figure~\ref{efron} the columns specifier is \verb+lr@{}l+ and
the fourth line is
\begin{verbatim}
     Half-samples & 0.364 & \dag\\
\end{verbatim}

To see the difference between these last two solutions, compare the following.
\begin{center}
$
\begin{array}{ll}
\begin{array}{l}
\verb+\begin{array}{|r@{}l|}+\\
\verb!20 & ++!\\
\verb+\end{array}+
\end{array}
&
\begin{array}{|r@{}l|}
20 & ++
\end{array}
\\ \\
\begin{array}{l}
\verb+\begin{array}{|r|}+\\
\verb!20 \rlap{$++$}!\\
\verb+\end{array}+
\end{array}
&
\begin{array}{|r|}
20 \rlap{$++$}
\end{array}
\end{array}
$
\end{center}

\subsection{Columns specifiers}
We have seen that a columns specifier is a string of the following objects:
\verb+r+, \verb+l+, \verb+c+, \verb+p{...}+, \verb+@{...}+ and \verb+|+. It is 
easy to lose track when setting up the column specifier for a complicated 
array. However, repeated elements may be simplified by using \verb+*+. Thus an
array with $15$~centred columns can be specified with \verb+*{15}{c}+. This 
shorthand is really advantageous for something complicated like
\verb+l|*{4}{r@{}lrr|}+. Moreover, \verb+*+~expressions can be nested within 
each other.

\section*{Exercises}
\begin{qn}
\renewcommand{\arraystretch}{1.6}
\[
\begin{array}{c|ccccc}
\multicolumn{6}{c}{\mbox{Values of $\nu_{\gamma\chi}$ for the design}}\\
\hline
\gamma & \chi_0 & \chi_1 & \chi_2 &\chi_3 & \chi_4\\
\hline
\mu & 1 & 0 & 0 & 0 & 0 \\
\sigma & 1 & 0 & 0 & 0 & 0\\
\rho & 1 & 0 & 0 & \frac{1}{4} & 0\\
\kappa & 1 & \displaystyle\frac{3 + e_2 - e_1}{36} & 
\displaystyle\frac{3 + e_4 - e_2}{36} & 0 &
\displaystyle\frac{3 + e_1 - e_4}{36}\\
\varepsilon & 1 & 1 & 1 & 1 & 1\\
\hline
\end{array}
\]
\end{qn}

\begin{qn}
Table of results:
\[
\begin{array}{l|cc|r}
 & B_1 & B_2\\
\hline 
A_1 & 37 & \phz53 & 90\\
A_2 & 41 & \phz48 & 89\\
\hline
 & 78 & 101 & 179
\end{array}
\]
\end{qn}

\begin{qn}
\it Typeset the table in Figure~\ref{eel}.
\end{qn}

\begin{qn}
\[
\begin{array}{lrrrr@{.}l}
\mbox{Source} & \mbox{df} & \multicolumn{1}{c}{\mbox{SS}} & 
\multicolumn{1}{c}{\mbox{MS}} & \multicolumn{2}{c}{\mbox{VR}}\\
\hline
x_1 & 1 & 104474 & 104474 & 1220 & 5\\
x_2 \mid x_1 & 1 & 2284 & 2284 & 26 & 68\\
\cline{1-3}
\mbox{regression} & 2 & 106758\\
\mbox{residual} & 15 & 1284 & 85.6\\
\cline{1-3}
\mbox{`total'} & 17 & 108042
\end{array}
\]
\end{qn}

\begin{qn}
\it Typeset the table in Figure~\ref{cars}.
\end{qn}

\begin{qn}
\[
\begin{tabular}%
{||r@{ }l|r@{}l|cr@{}lc|@{}c@{\hspace{\arraycolsep}\pounds}r||}
\multicolumn{2}{||c|}{Date} & \multicolumn{2}{c|}{Miles} & 
\multicolumn{4}{c|}{Gallons} & \multicolumn{2}{c||}{Cost}\\
\hline
27 & December & 46674 & && 4 & .18 &&& 5.56\\
3 & January & 46038 & & &3 & .93  && &5.00\\
6 & January & 47956 & ? && 7 & .1 && &10.50\\
10 & January & 47292 & && 7 & .89&& & 10.50\\
17 & January & 47464 &&& 4 & &&&5.00\\
\multicolumn{10}{||c||}{\dotfill}\\
3 & February & 48112 &&& 6 & .88 &&& 9.00
\end{tabular}
\]
\end{qn}

\begin{thebibliography}{9}
\bibitem{keyd}
\writer{Central Statistical Office}
\book{Key Data 1986}
\publish{Her Majesty's Stationery Office}{London}
\byear{1986}

\bibitem{brad}
\writer{Efron,~B.}
\book{The Jackknife, the Bootstrap and Other Resampling Plans}
\publish{Society for Industrial and Applied Mathematics}{Philadelphia}
\byear{1982}

\bibitem{leslie}
\writer{Lamport,~L.}
\book{\LaTeX: A Document Preparation System}
first edition,
\publish{Addison-Wesley}{Reading, Mass.}
\byear{1986}

\bibitem{newleslie}
\writer{Lamport,~L.}
\book{\LaTeX: A Document Preparation System}
second edition, 
\publish{Addison-Wesley}{Reading, Mass.}
\byear{1994}

\end{thebibliography}
\end{Article}


