%latex2e file
%%%
%%% An article for Baskerville, intended to be the last of 6 parts
%%%
\title{Maths in \LaTeX: Part~6, Harder arrays}
\author[R.~A.~Bailey]{R.~A.~Bailey\\
Queen Mary and Westfield College, \\University of London}
%%%
%% Added by CAR for SPQR, and other small necessary changes made:
%% search for ``amsmath''.
%%% 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}
\newcommand{\latexword}[1]{{\tt #1}}
%%%
%%%
%%% 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{\phz}{\phantom{0}}
\newcommand{\RBdfrac}[2]{\displaystyle\frac{#1}{#2}}
\newcommand{\CS}{\mathop{{\rm CS}}\nolimits}
\newcommand{\normof}[1]{{\left\Vert#1\right\Vert}^2}
\newcommand{\R}{\mathbb{R}}
\begin{Article}
\section{Recall}
This is the sixth and final part of a sequence of tutorials on
typesetting Mathematics in \LaTeX. The first five appeared in
issues~4.4, 4.5, 5.1, 5.2 and~5.3 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 final
tutorial I cover the harder parts of arrays, including aligned
equations.
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.
This tutorial covers things that \LaTeX\ is not really very good at.
You may ask why I have not simply referred you to the packages
\latexword{amsmath}, \latexword{array} and \latexword{delarray}. One
reason is that every package has its price: it may give you the
functionality that you want at the expense of changing something that
you are quite happy with. The other is that you often are not allowed
to include style packages when you submit an article to a journal or
conference proceedings. It is your choice whether to use the inelegant
solutions presented here or to cut out the relevant pieces of code
from various style packages.
\section{Answers}
\subsection{Boxed subarrays}
In the panel session at the end of the \ukt\ meeting on `The New Maths for
the New \LaTeX' on 7~June, one of the participants asked how to create an array
in which there is a box around a subarray, as in
\[
\begin{array}{c|cccc|}
\multicolumn{1}{c}{1} & 2 & 3 & 4 & \multicolumn{1}{c}{5}\\
\cline{2-5}
2 & A & B & C & D\\
3 & B & A & D & C\\
4 & C & D & A & B\\
5 & D & C & B & A\\
\cline{2-5}
\end{array}
\]
The answer is to use \verb+\cline+ for the horizontal sides of the box and to
put \verb+|+ in the columns specifier to obtain the vertical sides of the box,
overriding this with \verb+\multicolumn{1}+ where necessary. Thus the input
for the preceding array begins
\begin{verbatim}
\begin{array}{c|cccc|}
\multicolumn{1}{c}{1} & 2 & 3 & 4 &
\multicolumn{1}{c}{5}\\
\cline{2-5}
2 & A & B & C & D\\
...
\end{verbatim}
\subsection{Angle brackets}
Several people have asked me why I insist that \verb+\langle+ and
\verb+\rangle+ should be used for angle brackets when they prefer the shape of
\verb+<+ and \verb+>+. At a group theory conference in July I saw a good, if
unconscious, demonstration of why \verb+<+ and \verb+>+ should not be used. A
line of displayed Maths on an overhead projector transparency was
\[
G = \times ^x
\]
Look at the spacing. \TeX\ knows that $=$ and $<$ are both relations,
so it puts no space between them, but it does put some space between the
relation~$>$ and the binary operator~$\times$. If you put this
equation in the running text, you will find that the line may break between
the~$<$ and the~$a$. If you really prefer the shapes of $<$ and~$>$ to
$\langle$ and~$\rangle$ then you should make yourself macros such as
\begin{verbatim}
\newcommand{\llangle}{\mathopen{<}}
\newcommand{\rrangle}{\mathclose{>}}
\end{verbatim}
\newcommand{\llangle}{\mathopen{<}}
\newcommand{\rrangle}{\mathclose{>}}
Then the display becomes
\[
G = \llangle a,b,c\rrangle \times \llangle a,c,e\rrangle^x
\]
However, you cannot make these new angle brackets expand by preceding them with
\verb+\left+ and \verb+\right+.
\addtocounter{section}{9}
\section{Arrays of equations}
\subsection{Don't do it}
Many of us write our lecture notes on the board as a series of equations, more
or less aligned, and are tempted to write in print in the same fashion. Don't.
For one thing, printed material needs the connecting words that you normally
say at the board, such as `and' or `it follows that' or `substituting for
\ldots'. For another, alignment suggests to the reader that the equations are
somehow related, so it should not be used merely because two displayed
equations come one after another with no intervening text: use two separate
lines of displayed Maths instead, using \verb+\[+ and~\verb+\]+.
\subsection{Parallel definitions}
For two or more parallel or analogous definitions or results, use the
\latexword{eqnarray*} environment. If a typical line is $A=B$ then type that
line as \verb+A & = & B+ and put \verb+\\+ at the end of each line except the
last. Extra space can be added after any \verb+\\+ just as with ordinary
arrays. For example, the parallel definitions of $\cap$ and~$+$
\begin{eqnarray*}
W \cap X& =& \left\{v\in V:v \in W \mbox{ and } v\in X\right\}\\
W + X & = & \left\{w+x:w\in W \mbox{ and } x\in X\right\}
\end{eqnarray*}
have input
\begin{verbatim}
\begin{eqnarray*}
W \cap X & =& \left\{v\in V:v \in W
\mbox{ and } v\in X\right\}\\
W + X & = & \left\{w+x:w\in W
\mbox{ and } x\in X\right\}
\end{eqnarray*}
\end{verbatim}
\subsection{Chains of equalities}
The \latexword{eqnarray*} environment is also useful for a chain of equalities
or inequalities, such as
\begin{eqnarray*}
\sum_{i=1}^q x_{ij}(M-x_{ij}) & = & M^2 - \sum_{i=1}^q x_{ij}^2\\
& \leq & M^2 - \frac{M^2}{q}\\
& = & \theta M^2.
\end{eqnarray*}
Here each line after the first begins with \verb+&+ followed by \verb+=+ or
some other relation, followed by another~\verb+&+.
Any line may conclude with \verb+\qquad\mbox{...}+ to give
a short explanation, just as in a single line of displayed Maths.
\begin{figure*}
\begin{eqnarray*}
n(Q_3 - Q_1)^4 \mathop{\rm Var} W
& = &
\left[(M-Q_1) \left(\frac{1}{f_M} - \frac{1}{2f_{Q_3}}\right)
+ (Q_3 - M) \left(\frac{1}{f_M} - \frac{1}{2f_{Q_1}}\right)\right]^2\\
& & \mbox{} + \frac{1}{2}\left[\left(\frac{M-Q_1}{f_{Q_3}}\right)^2
+ \left(\frac{Q_3 - M}{f_{Q_1}}\right)^2\right]
\end{eqnarray*}
\caption{An overlong equation}
\label{loong}
\end{figure*}
\subsection{Overlong displays}
Sometimes what is conceptually a single line of displayed Maths, whether it is
an equation or not, is simply too long to fit on one line. Then you can use
\latexword{eqnarray*}, choosing where to split the line. If you split it at a
binary operator, it is usual to put the binary operator after the split. In
this case you must precede it with \verb+\mbox{}+ so that \TeX\ knows that it
is a binary operator.
The two lines in Figure~\ref{loong} are given by
\begin{verbatim}
...\right]^2\\ & & \mbox{} + \frac{1}{2} ...
\end{verbatim}
To split an even longer line, you may want the second and succeeding lines to
come partly underneath the first line. You can do this by enclosing the whole
of the first line in \verb+\lefteqn{ }+, thus fooling \TeX\ into thinking that
it has no width. Starting subsequent lines with \verb+& &+ gives that necessary
bit of indentation.
In this example
\begin{eqnarray*}
\lefteqn{\sum\left\{\sum\left\{f(B) : B \cap A = \emptyset\right\} : A
\supseteq J\right\} = }\\
& & \sum\left\{\sum\left\{f(B) : A \supseteq J,\ A \cap B = \emptyset\right\}
: B \cap J = \emptyset \right\}
\end{eqnarray*}
the lines begin
\begin{verbatim}
\lefteqn{\sum ...
& & \sum ...
\end{verbatim}
\subsection{Numbered aligned equations}
The environment \latexword{eqnarray} works just like \latexword{eqnarray*}
except that each line is numbered, in the same sequence as
\latexword{equation}s. If you want any line to be not numbered, just put
\verb+\nonumber+ before the end of the line. If you want to refer somewhere
else to the number, put a \verb+\label+ on the line in the usual way.
Thus
\begin{eqnarray}
\bar{F}(x_1,x_2) & = & \int_0^\infty \exp(-\theta a_1x_1^c - \theta a_2x_2^c)
\frac{\theta^{b-1}\lambda^b {\rm e}^{-\theta\lambda}}{\Gamma(b)} {\rm
d}\theta \nonumber \\
& = & \frac{\lambda^b}{(\lambda + a_1x_1^c +a_2x_2^c)^b}
\end{eqnarray}
is created with
\begin{verbatim}
\begin{eqnarray}
\bar{F}(x_1,x_2) & = & \int_0^\infty ...
{\rm d}\theta \nonumber \\
& = & \frac{ ...
\end{eqnarray}
\end{verbatim}
\subsection{What is \latexword{eqnarray}?}
The two environments \latexword{eqnarray} and \latexword{eqnarray*} differ only
in the numbering of lines. Each creates a piece of displayed Maths containing a
special sort of array. The array has only three columns. The first column is in
\verb+\displaystyle+ and is right-aligned. The second is in \verb+\textstyle+
and is centred. The third is in \verb+\displaystyle+ and is left-aligned. The
space between columns is controlled by \verb+\arraycolsep+ just as for ordinary
arrays. The space between rows is (unless you put something after the
\verb+\\+) what you would get in an ordinary array by putting \verb+\\[\jot]+.
% Corrected by CAR: // to \\ twice here, once below.
\subsection{Simultaneous equations}
Simultaneous equations are often written with a vertical alignment for each
variable and for the binary operators in between them, as well as for the
equals sign, as the following example shows.
\[
\setlength{\arraycolsep}{0pt}
\begin{array}{*{4}{rc}r@{\mbox{}={}}r}
x_1 &{} -{} & x_2 & {}+{} & x_3 & {}-{} &x_4 &{} +{}& x_5 & 1\\[\jot]
2x_1 & - & x_2 & +& 3x_3 && & + &4x_5 & 2\\[\jot]
3x_1 & - &2 x_2 & + & 2x_3 & + &x_4 & +& x_5 & 1\\[\jot]
x_1 & & & + & x_3 & + &2x_4 & +& x_5 & 0\\[\jot]
\end{array}
\]
This is too many alignments for an \latexword{eqnarray*}, so an
\latexword{array} has been used in displayed Maths, with every line ending with
\verb+\\[\jot]+. With a column for each variable and one for each binary
operator, almost all pairs of adjacent columns should have the separation that
\TeX\ normally gives between an ordinary Maths symbol and a binary operator,
which is \verb+\medmuskip+. Unfortunately, you cannot set \verb+\arraycolsep+
to be equal to \verb+0.5\medmuskip+: \TeX\ will complain. So I have set
\verb+\arraycolsep+ to zero: the command
\begin{verbatim}
\setlength{\arraycolsep}{0pt}
\end{verbatim}
has been placed before the array but within the displayed Maths, to limit its
scope. Then the columns specifier
\verb+{*{4}{rc}r@{{}={}}r}+ does the trick for the equals sign, which comes in
every column: for the binary operators I have put \verb!{}+{}! or \verb+{}-{}+
as least once in each column.
There are two other possibilities that could be used here. The
\latexword{array} package allows you to put items in the columns specifier that
will be incorporated in array entries before the boxes are made. So you could
put the \verb+{}+ either side of each binary operator by putting it in the
columns specifier once and for all. See \cite[Section~5.3]{companion}.
That would be useful if the binary
operators in the array had differing widths. The second is to effectively set
\verb+\arraycolsep+ equal to \verb+0.5\medmuskip+. Now, \verb+\medmuskip+ is
4\,mu plus some stretchability, and 1\,mu is $1/18$ of an em in the current
font.
So you can do
\begin{verbatim}
\setlength{\arraycolsep}{0.11em}
\end{verbatim}
and omit all the \verb+{}+,
so long as the current font does not change (by too much) between the issuing
of that command and the setting of the entries in the array.
\subsection{Which to use: \latexword{eqnarray} or \latexword{array}?}
Regular readers will know that I am a big fan of \LaTeX. All the same, I think
that the design of \latexword{eqnarray} is fundamentally flawed. It is not
simply a method of aligning lines of displayed Maths, chiefly because it uses
\verb+\arraycolsep+ to insert larger spaces than normal, but also because it
changes between \verb+\displaystyle+ and \verb+\textstyle+ and because it is
limited to three columns. For these last two reasons, it is also not a method
of achieving a displayed array all of whose entries are in
\verb+\displaystyle+ and whose rows are spread out,
which would have been a useful environment.
So which should you use, \latexword{eqnarray} or \latexword{eqnarray*} or
\latexword{array}? Each of them needs some work to give good results.
If you need a set of aligned
equations carrying a single number then I recommend using
\latexword{array} inside an \latexword{equation}. You will have to put in
\verb+\displaystyle+ and \verb+\jot+ where necessary. If one or more lines
must be individually numbered then there is nothing for it but to use
\latexword{eqnarray}.
If an unnumbered set of aligned equations has only two alignment points you may
be able to use \latexword{eqnarray*} if you are careful about the inter-column
spacing. Thus if you put an \verb+&+ on only one side of an equals sign you
must put a quad space on the other side. In the following display each line has
the form
\begin{verbatim}
... \quad = & ... & \qquad ...
\end{verbatim}
\begin{eqnarray*}
g(x) \quad = & {\rm e}^{x} &\qquad\mbox{for $x\in\R$,}\\
h(y) \quad= & \ln y &\qquad\mbox{for $y > 0$,}\\
h'(y) \quad = & \frac{1}{y} &\qquad\mbox{for $y > 0$.}
\end{eqnarray*}
For a set with more alignment points, such as
\[
\begin{array}{r@{(1)={}}rr@{(2)={}}rr@{(3)={}}rr@{(4)={}}r}
f & 1 & f & 0 & f & -2 & f & 3\\[\jot]
g & 5 & g & 7.5 & g & 6 & g & -4\\
\end{array}
\]
or simultaneous equations, use \latexword{array} and be cunning with the
columns specifier.
For parallel results, or for chains of (in)equalities, it would be good to
have a form of \latexword{eqnarray} and \latexword{eqnarray*} in which the
space on either side of the equals sign is what \TeX\ normally puts between a
relation and an ordinary Maths symbol, which is \verb+\thickmuskip+.
Now, \verb+\thickmuskip+ is 5\,mu plus some stretchability, so we can use the
same fudge that we used for simultaneous equations.
It is no
good changing \verb+\arraycolsep+ globally, because that would affect other
\latexword{array}s. So you could make an environment to use in place of
\latexword{eqnarray} such as the following.
\begin{verbatim}
\newenvironment{bettereqnarray}%
{\setlength{\arraycolsep}{0.14em}%
\eqnarray}%
{\endeqnarray}
\end{verbatim}
\newenvironment{bettereqnarray}%
{\setlength{\arraycolsep}{0.14em}\eqnarray}%
{\endeqnarray}
Compare the following display, made with \latexword{bettereqnarray} and
\verb+\nonumber+, with the previous form made with \latexword{eqnarray*}.
Now that the spaces around the aligned~\verb+=+ are correct, a second~\verb+=+
can be placed on the same line.
\begin{bettereqnarray}
\sum_{i=1}^q x_{ij}(M-x_{ij}) & = & M^2 - \sum_{i=1}^q x_{ij}^2\nonumber\\
& \leq & M^2 - \frac{M^2}{q}
= \theta M^2 \nonumber
\end{bettereqnarray}
If you are uneasy about that fudge, set \verb+\arraycolsep+ to zero.
Then put \verb+& {}={} &+ instead of \verb+& = &+ in the centre of the array.
There is a disadvantage common to both of these \latexword{bettereqnarray}
environments: if you have any ordinary \latexword{array} within them then
the value
of \verb+\arraycolsep+ will almost certainly be wrong and you will have to
reset it locally.
There are several better environments for aligned equations in the
\latexword{amsmath} package, which is described in \cite{ams}. However, it does
not seem to be possible to obtain these environments without the rest of the
package, which you may not want: for example, it disables \verb+\over+.
\section{Exercises}
\addtocounter{preqn}{71}
\begin{qn}
M\"obius inversion gives:
\begin{eqnarray}
B_\gamma & = & \sum_{\alpha\in\Gamma} z(\gamma,\alpha) S_\alpha,\\
S_\alpha & = & \sum_{\gamma\in\Gamma} m(\alpha,\gamma) B_\gamma.
\label{arrayref}
\end{eqnarray}
\end{qn}
\begin{qn}
{\it Get the number cited here from the question above, by cross-reference.}
Now\begin{eqnarray*}
L_\alpha & = & X'S_\alpha X\\
& = & \sum_{\gamma\in\Gamma} m(\alpha,\gamma)X'B_\gamma X \qquad \mbox{by
Equation~(\ref{arrayref})}\\
& = & \sum_{\gamma\in\Gamma} \frac{m(\alpha,\gamma)}{k_\gamma} C_\gamma.
\end{eqnarray*}
\end{qn}
\begin{qn}
\begin{eqnarray}
\sum a_j b_k & = & a_1b_1 + a_1b_2 + a_1b_3 \nonumber\\
& & \mbox{} + a_2b_1 + a_2b_2 + a_2b_3 \nonumber\\
& & \mbox{} + a_3b_1 + a_2b_2 + a_3b_3.
\end{eqnarray}
\end{qn}
\begin{qn}
\begin{equation}
\begin{array}{rcl}
\displaystyle
\sum_{1\leq j,k\leq 3} a_j b_k
&=&
\begin{array}[t]{l}
a_1b_1 + a_1b_2 + a_1b_3 \\[\jot]
\mbox{} + a_2b_1 + a_2b_2 + a_2b_3 \\[\jot]
\mbox{} + a_3b_1 + a_2b_2 + a_3b_3.
\end{array}
\end{array}
\end{equation}
\end{qn}
\begin{qn}
Using the hint, we get
\begin{eqnarray*}
\lefteqn{3(1-z)^2\sum_k
%% Changed for amsmath:
% {1/2\choose k}
\binom{1/2}{k}
\left({\frac{8}{9}z}\right)^k
(1-z)^{2-k} = }\\
& & 3(1-z)^2 \sum_k
%% Changed for amsmath:
% {1/2\choose k}
\binom{1/2}{k}
\left({\frac{8}{9}}\right)^k
\sum_j
%% Changed for amsmath:
% {k+j-3\choose j}
\binom{k+j-3}{j}
z^{j+k}
\end{eqnarray*}
and now look at the coefficient of $z^{3+l}$.
\end{qn}
\begin{qn}
Solve the system of equations
\[
\setlength{\arraycolsep}{0pt}
\begin{array}{rcrcr@{{}={}}l}
2x &{} +{} &y &{}+{}& 5z&4\\[\jot]
3x & - & 2y & + & 2z & 2\\[\jot]
5x & - & 8y & - & 4z & 1.
\end{array}
\]
\end{qn}
\begin{qn}
The dyad appears as
\begin{equation}
\begin{array}{rcc@{}@{\mbox{}+\mbox{}}c@{\mbox{}+\mbox{}}c@{\mbox{}+\mbox{}}c}
{\bf AB} & = & \multicolumn{1}{c}{}& A_xB_x {\bf ii} & A_xB_y{\bf ij} &
A_xB_z{\bf ik} \\[\jot]
& && A_yB_x{\bf ji} & A_yB_y{\bf jj} & A_yB_z{\bf jk}\\[\jot]
& && A_zB_x{\bf ki} & A_zB_y{\bf kj} &A_zB_z{\bf kk}
\end{array}
\end{equation}
\end{qn}
\begin{qn}
\[
\begin{array}{r@{(x)={}}l@{\qquad}r@{{}=(}r@{,}r@{,}r@{,}r@{,}r@{)}l}
f & 1 & f & 1&\phz1&\phz1&1&1\\[\jot]
g & x & g & 1&2&3&4&5\\[\jot]
h & x^2 & h & 1&4&9&\phz16&\phz25&.
\end{array}
\]
\end{qn}
\section{Hand-crafting alignments}
In this section I show a few tricks for difficult alignments. I show them
because I know that I am not the only Mathematician who needs
to produce these effects. I am not particularly proud of the methods I have
used, because in each case I have had to either measure a length explicitly or
use phantoms: I couldn't find a way of getting the right sizes automatically.
So if any reader can write in with a better way of doing these things, I shall
be delighted.
\subsection{Horizontal braces in arrays}
When I use an \latexword{array} to show a chain of equalities, I often use an
under- or overbrace to indicate how terms are grouped. Here is an example.
\newlength{\gnat}
\settowidth{\gnat}{$\bar{y} (1,\ldots, 1)$}
\newlength{\gnu}
\settowidth{\gnu}{$\RBdfrac{\CS(x,y)}{\CS(x,x)} x'$}
\[
\begin{array}{rcccccc}
y & = & \multicolumn{3}{c}{f} & +& r\\
& & \multicolumn{3}{c}{
\overbrace{\hphantom{\bar{y} (1,\ldots, 1)\quad+\quad
\RBdfrac{\CS(x,y)}{\CS(x,x)}
x'}}}\\
&= & \bar{y} (1,\ldots, 1)&{}+{}&\RBdfrac{\CS(x,y)}{\CS(x,x)} x'
&{}+{} & r\\
& & \uparrow & & \uparrow & & \uparrow\\
& & \multicolumn{1}{p{\gnat}}{\centering fit for null model} &&
\multicolumn{1}{p{\gnu}}{\centering
extra fit \mbox{for straight} line model} &&
\mbox{residual}
\end{array}
\]
The problem is that the brace needs to span several columns. So the brace needs
to be put in with a \verb+\multicolumn+ command, so it cannot automatically be
set to the correct width. I solve this by using \verb+\hphantom+ to obtain the
width of the spanned columns. Here there is an entry
\begin{verbatim}
\multicolumn{3}{c}{f}
\end{verbatim}
in the first row; and the corresponding entry in the second row is
\begin{verbatim}
\multicolumn{3}{c}{
\overbrace{\hphantom{\bar{y} (1,\ldots, 1)
\quad+\quad ... x'}}}
\end{verbatim}
I have used the fact that the intercolumn space is one quad.
An alternative solution is to use the command \verb+\downbracefill+ as the
final argument of the \verb+\multicolumn+ in the second row. However, you have
to be outside Maths mode to use \verb+\downbracefill+, and putting it inside an
\latexword{mbox} is no good because that destroys the expandability. So you
have to set the whole thing in a \latexword{tabular} environment rather than
\latexword{array}, and then enclose every other entry in \verb+$ $+, which is a
pain. There is also an analogous command \verb+\upbracefill+.
\subsection{Paragraph boxes in arrays}
In the preceding display I have also set some explanatory text in paragraph
boxes whose width is determined by the width of a Mathematical expression in
the same column. For each such column I make a new length and use
\verb+\settowidth+ to make it as wide as the desired Mathematical expression:
see \cite[page~95]{leslie} or \cite[page~101]{newleslie}. Then I make a
\verb+p+ column element of that width.
For example, in the first column I get the correct width with
\begin{verbatim}
\newlength{\gnat}
\settowidth{\gnat}{$\bar{y} (1,\ldots, 1)$}
\end{verbatim}
and then use it as follows.
\begin{verbatim}
\multicolumn{1}{p{\gnat}}{\centering
fit for null model}
\end{verbatim}
\subsection{Top-aligned matrices}
\newcommand{\topthing}[1]{\begin{array}{@{}c@{}}#1\\\phz\end{array}}
\newcommand{\vecmu}{\mbox{\boldmath$\mu$}}
\newcommand{\vecx}{{\bf x}}
\newcommand{\cov}{\mathop{{\rm Cov}}\nolimits}
\begin{figure*}
\[
\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*}
In the last article I showed Figure~\ref{topmat} as an example of
something that is not easy to do in \LaTeX. To achieve this I have
made a macro \verb+\topthing+ which takes as its single argument any
item that must be aligned with the top rows of the arrays. The biggest
arrays here have two rows, so \verb+\topthing+ produces a two-rowed
array whose second row is empty. If there were bigger arrays here I
would have to have a macro for each smaller size of array. The empty
second row of the array contains a phantom zero: this works because
zero is the standard height, as are all the entries that occur in the
second rows of arrays. I don't know how to fudge this for arbitrary
heights of entries. Finally, I remove the extra space that is usually
put on each side of an array.
The \verb+\topthing+ macro is defined by
\begin{verbatim}
\newcommand{\topthing}[1]%
{\begin{array}{@{}c@{}}
#1\\\phantom{0}
\end{array}}
\end{verbatim}
Then the equation in Figure~\ref{topmat} is done with
\begin{verbatim}
\topthing{3(... (x-1, y+2)}
\left[\begin{array}{rr}
4&1\\1&1
\end{array}
...
\end{verbatim}
Top-aligned matrices can be done very simply if you have access to the package
\latexword{delarray}. Get it. See \cite[Section 5.3.6]{companion}. However, be
warned that \latexword{delarray} inputs the package \latexword{array}, which
makes a small difference to the way that \verb+|+ works in all
\latexword{array}s.
\subsection{Bordered matrices}
Using \verb+\left[+ and so on to get the right size of fences around a matrix
works fine for unbordered matrices. But how do you get a bordered matrix such
as the following?
\newlength{\perch}
\newcommand{\fish}[2]{\settowidth{\perch}{$#1$}\makebox[\perch]{$#2$}}
\newcommand{\minone}{\llap{$-$}1}
\begin{equation}
\begin{array}{c@{}c}
& \begin{array}{ccccc}
\emptyset & \{1\} & \{2\} & \{1,2\} & \{1,2,3\}\\
\end{array}\\
\begin{array}{c}
\emptyset \\ \{1\} \\ \{2\} \\ \{1,2\} \\ \{1,2,3\}
\end{array}
&
\left[
\begin{array}{ccccc}
\fish{\emptyset}{1} & \fish{\{1\}}{\minone} & \fish{\{2\}}{\minone}
& \fish{\{1,2\}}{1} & \fish{\{1,2,3\}}{0}\\
0 & 1 & 0 & \minone & 0\\
0 & 0 & 1 & \minone & 0\\
0 & 0 & 0 & 1 & \minone\\
0 & 0 & 0 & 0 & 1
\end{array}
\right]
\end{array}
\label{zeta}
\end{equation}
The command \verb+\bordermatrix+ given by plain \TeX\ will not do, because it
uses parentheses for the fences and does not let you choose how the columns are
aligned.
The problem is to get the fences of the right size while keeping the alignment
of the two borders with the body of the matrix. My solution is to set the whole
thing as a $2 \times 2$ array with no space between the columns. The top left
entry is empty. The top right entry is a one-rowed array containing the column
labels. The bottom left entry is a one-columned array containing the row
labels. The bottom right entry is the body of the matrix, including the fences.
If all entries have the same width and height, that's enough. However, if any
column in the body of the matrix has a different width from the corresponding
column in the top border, then the narrower of the two has to be expanded.
Set a new length equal to the wider item and then put the smaller item in a
\latexword{makebox} of that width.
In the matrix~(\ref{zeta}) it is clear that, in every column, it is the label
that is the widest element. So I made a macro to set the first row of the body
of the matrix to the width of the labels, as follows.
\begin{verbatim}
\newlength{\perch}
\newcommand{\fish}[2]%
{\settowidth{\perch}{$#1$}
\makebox[\perch]{$#2$}}
\end{verbatim}
For example, the first entry in the main body of the matrix is
\begin{verbatim}
\fish{\emptyset}{1}
\end{verbatim}
Note that it suffices to make a single entry in each column of the body as wide
as the column label.
(By the way, I can never decide whether the minus signs should be taken into
account or not when centering the columns of such a matrix. Here I decided not
to, and so I defined
\begin{verbatim}
\newcommand{\minone}{\llap{$-$}1}
\end{verbatim}
and then used \verb+\minone+ for each $-1$.)
If the row heights in the body of the matrix do not match those in the array of
row labels, as in the following matrix, they can be adjusted with
\verb+\vphantom+. If \verb+X+ is the tallest item in the row, simply put
\verb+\vphantom{X}+ in a single entry of the corresponding row of the other
array: it will not affect the horizontal spacing.
\begin{equation}
\begin{array}{c@{}c}
& \begin{array}{c|c}
\multicolumn{1}{c}{1\quad \ldots\quad r} & r+1 \quad\ldots\quad n
\end{array}\\
\begin{array}{c}
\begin{array}{c}
1\\\vdots\\ r
\end{array}\\
\begin{array}{c}
r+1\\\vdots\\ n
\end{array}
\end{array}
&
\left[
\begin{array}{c|c}
\fish{1\quad \ldots\quad r}{\Sigma_1}
\vphantom{\begin{array}{c}
1\\\vdots\\r
\end{array}}
& \fish{r+1\quad \ldots\quad n}{0}\\
\hline
\vphantom{\begin{array}{c}
r+1\\\vdots\\ n
\end{array}}
0
&
\Sigma_2
\end{array}
\right]
\end{array}
\label{showoff}
\end{equation}
\section{Exercises}
\begin{qn}
\[
\begin{array}{ccccccc}
\normof{f} & = & e_1^2 u_1\cdot u_1 & + & e_2^2 u_2 \cdot u_2 &+& e_3^2 u_3
\cdot
u_3\\[\jot]
& & 18 \bar{y}^2 & + & {\tt ss1} & + & {\tt ss2}\\[\jot]
& & & & 104474 & + & 2284\\
& & & &
\multicolumn{3}{c}{\underbrace{\hphantom{e_2^2 u_2 \cdot u_2\quad+\quad
e_3^2 u_3 \cdot u_3}}_{\textstyle 106758}}
\end{array}
\]
\end{qn}
\begin{qn}
\begin{bettereqnarray}
\topthing{\cov(U,V)} &
\topthing{=} &
\topthing{(2,1)}
{
\renewcommand{\arraycolsep}{5pt}
\left[\begin{array}{@{}rr@{}}1 & -1 \\ -1 & 4
\end{array}\right]
\left(\begin{array}{@{}r@{}}-2 \\ 1\end{array}\right)
}
\nonumber\\
&
\topthing{=} &
\topthing{(2,1)}
\left(\begin{array}{@{}r@{}}-3 \\6\end{array}\right)
\topthing{{}=0}
\nonumber
\end{bettereqnarray}
\end{qn}
\begin{qn}
{\it Typeset the matrix~(\ref{showoff}).}
\end{qn}
\section{Acknowledgements}
While writing these tutorials I have had to expand my own knowledge of how to
typeset Mathematics in \LaTeX\ to cover topics that I had never really bothered
with properly. I should like to thank David Carlisle, Frank Mittelbach and
Chris Rowley for patiently answering my questions, even when they did not
wholly approve of what I was writing. Of course, any remaining mistakes are my
own, as are the personal opinions expressed.
Thanks also to many of my colleagues, both at Goldsmiths' College and at Queen
Mary and Westfield College, for badgering me to explain how to do these things.
And thanks to those readers who have sent kind messages of appreciation while
the tutorials have been appearing.
\begin{thebibliography}{9}
\bibitem{ams}
\writer{American Mathematical Society}
\book{\AMS-\LaTeX\ Version~1.2 User's Guide}
\publish{American Mathematical Society}{Providence, Rhode Island}
\byear{1995}
\bibitem{companion}
\writer{Goossens,~M., Mittelbach,~F. \& Samarin,~A.}
\book{The \LaTeX\ Companion}
\publish{Addison-Wesley}{Reading, Mass.}
\byear{1994}
\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}