\documentclass{pracjourn}
\TPJrevision{2012}{02}{16}
\TPJissue{2012}{1}
\TPJcopyright{}
%%%
\def\ctanloc#1{\url{http://mirror.ctan.org/#1}}
\def\ctanfile#1{\url{http://mirror.ctan.org/#1}}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,amsthm,mathtools}
\usepackage{fancyvrb,framed,multicol}
\providecommand\cs[1]{\texttt{\symbol{'134}\hyphenchar\font=`\-#1}}
\fvset{formatcom=\color[rgb]{0,0.4,0.2}}
\usepackage{microtype}
\hfuzz=3.5pt
\makeatletter
% a few macro copied from ltugboat.cls
\providecommand\d@sh[2]{\unskip#1\thinskip#2\thinskip\ignorespaces}
\providecommand\thinskip{\hskip 0.16667em\relax}
\def\endash{--}
\providecommand\emdash{\endash-}
\providecommand\Dash{\d@sh\nobreak\emdash}
\DeclareRobustCommand\SMC{%
\ifx\@currsize\normalsize\small\else
\ifx\@currsize\small\footnotesize\else
\ifx\@currsize\footnotesize\scriptsize\else
\ifx\@currsize\large\normalsize\else
\ifx\@currsize\Large\large\else
\ifx\@currsize\LARGE\Large\else
\ifx\@currsize\scriptsize\tiny\else
\ifx\@currsize\tiny\tiny\else
\ifx\@currsize\huge\LARGE\else
\ifx\@currsize\Huge\huge\else
\small\SMC@unknown@warning
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
}
\providecommand\SMC@unknown@warning{\TBWarning{\string\SMC: nonstandard
text font size command -- using \string\small}}
\providecommand\textSMC[1]{{\SMC #1}}
\providecommand\acro[1]{\textSMC{#1}\@}
% end copy
\newcommand\@dbx[1]{{\fboxsep=0pt\fbox{\phantom{\rule[-0.6mm]{#1}{3mm}}}}}
\newcommand\dbx[1][15mm]{\ifmmode\mathord{\@dbx{#1}}\else\@dbx{#1}\fi}
\newcommand\verbfile{\jobname.verb}
\newenvironment{VerbCode}{%
\VerbatimOut{\verbfile}}{%
\endVerbatimOut%
%\vskip-0.5\baselineskip%
\VerbatimInput[gobble=2,
%frame=single,
%framesep=0pt
]{\verbfile}
%\vskip-0.5\baselineskip
}
\newcommand\dlf@pre{\par\medskip\par\noindent\kern-\fboxsep\kern-\fboxrule}
\newcommand\dlf@post{\par\medskip}
\newcommand\ShowVerbFileN{%
\dlf@pre%
\fbox{\begin{minipage}{\linewidth}\input{\verbfile}\end{minipage}}%
\par\medskip
}
\newcommand\ShowVerbFileS{%
\dlf@pre%\par\medskip\par\noindent%
\fbox{%
\begin{minipage}{\linewidth}
\vskip-\abovedisplayskip%
\input{\verbfile}
% \vspace*{-\belowdisplayskip}%
\vskip-\belowdisplayskip%
\end{minipage}%
}%
\dlf@post%
}
\newcommand\ShowVerbFileSS{%
\dlf@pre%\par\medskip\noindent%
\fbox{%
\begin{minipage}{\linewidth}
\input{\verbfile}
\vskip-\belowdisplayskip%
\end{minipage}%
}%
\dlf@post%
}
\newcommand\ShowVerbFileSSS{%
\dlf@pre%\par\medskip\noindent%
\fbox{%
\begin{minipage}{\linewidth}
\vskip-1\abovedisplayskip%
\input{\verbfile}
\vskip-\belowdisplayshortskip%
\end{minipage}%
}%
\dlf@post%
}
\newcommand\ShowVerbFileM[1][]{%
\ifx\\#1\\\ShowVerbFileS%
\else%
\def\@tempa{x}%
\def\@tempb{#1}%
\ifx\@tempa\@tempb%
\ShowVerbFileSS%
\else%
\ShowVerbFileSSS%
\fi%
\fi%
}
\newcommand\ShowVerbFile{\@ifstar{\ShowVerbFileM}{\ShowVerbFileN}}
%\renewcommand\ShowVerbFile{}
\makeatother
\hyphenation{eqnarray mathenv}
\begin{document}
\author{Lars Madsen}
\title{Avoid eqnarray!}
\address{Department of Mathematics\\
Aarhus University\\
Denmark}
\email{daleif@imf.au.dk}
\abstract{Whenever the \texttt{eqnarray} environment appears in a
question or example of a problem on \url{comp.text.tex},
\url{tex.stackexchange.com} or other fora there is a high probability
that someone will tell the poster not to use \texttt{eqnarray}. This
article will provide some examples of why many of us consider
\texttt{eqnarray} to be harmful and why it should not be used.}
\hyperlinkemail
\license{Copyright \textcopyright\ 2006, 2012 Lars Madsen\\
Permission is granted to distribute verbatim or modified\\
copies of this document provided this notice remains intact\\
Originally published in \textsl{The Prac\TeX\ Journal} 2006-4.}
\maketitle
\section*{Introduction}
\label{sec:introduction}
When someone asks a question on \url{comp.text.tex},
\url{tex.stackexchange.com}
or other fora about the \texttt{eqnarray}
environment or shows an example using it, there will always be someone
that instructs the poster to stop using \texttt{eqnarray} and use
something better instead. This article provides an example-based
overview of some of the reasons why many people consider
\texttt{eqnarray} to be obsolete. Thus, this article can be used as a
reference when a poster asks for an explanation.
The prerequisites for this article are a basic knowledge of \LaTeX\
and knowledge of the syntax used by \texttt{eqnarray}. Experience
with the environments from the \texttt{amsmath} package is a plus but
not mandatory.
\section{The basics}
\label{sec:basics}
In plain \LaTeX, the \texttt{eqnarray} environment is basically the
only construction available for numbered multi-line equations. The
\texttt{eqnarray} environment is similar to
\begin{VerbCode}
\begin{array}{rcl}
...
\end{array}
\end{VerbCode}
with the difference being that the first and last cell in each row are
automatically typeset as display style mathematics, and not as text
style math as it would be in the \texttt{array} environment; also,
\texttt{eqnarray} supports automatic equation numbers.
The principal \texttt{eqnarray} usage is similar to this example:
\begin{VerbCode}
\begin{eqnarray}
y &=& (x+1)^2 \\ &=& x^2+2x+1
\end{eqnarray}
\end{VerbCode}
which results in (without the box):
\ShowVerbFile*[x]
In the examples that follow, we use the command \cs{dbx} instead
of writing some meaningless arbitrary mathematical formula.
\cs{dbx} is a simple macro, defined by the author, that writes a
box to simulate some random mathematical material. Using an optional
argument we can adjust the width of the box created by \cs{dbx}.
The reason for using simulated math instead of actually writing
something is that removing the actual text makes the reader
more aware of the actual problem, which is not the text
but rather the construction\slash surroundings themselves. The example
above will be shown like this instead:
\begin{VerbCode}
\begin{eqnarray*}
\dbx &=& \dbx[5cm] \\ &=& \dbx
\end{eqnarray*}
\end{VerbCode}
which results in:
\ShowVerbFile*
\section{Behold the problems}
\label{sec:behold-problems}
\subsection{The primary problem: Spacing inconsistency}
\label{sec:inconsistence}
Most commonly, \texttt{eqnarray}-users write their displayed
equations by mixing \texttt{eqnarray} and \texttt{eqnarray*} with
\texttt{equation}, \verb+\[...\]+, or \verb+$$...$$+ constructions.
Some even mix it with environments from the \texttt{amsmath} package
(though this is mostly seen when a document has been written by more
than one author).
This mixing results in the primary problem with \texttt{eqnarray}\Dash
\emph{spacing
inconsistency}. In the following example we consider a single line
equation versus a multi-line \texttt{eqnarray} equation.
\begin{VerbCode}
\[ \dbx = \dbx \]
whereas
\begin{eqnarray*}
\dbx &=& \dbx[3cm] \\ &=& \dbx
\end{eqnarray*}
\end{VerbCode}
which results in
\ShowVerbFile*[x]
\noindent Can you spot the problem?
It is even more obvious when we place the same code using
\texttt{eqnarray} and \texttt{equation} next to each other:
\begin{VerbCode}
\begin{eqnarray} \dbx &=& \dbx[3cm]
\end{eqnarray}
versus
\begin{equation} \dbx = \dbx[3cm]
\end{equation}
\end{VerbCode}
which results in
\ShowVerbFile*[y]
\noindent
Can you see the difference?
\bigskip
\noindent
We notice how the spacings around the \texttt{=}'s are inconsistent,
i.e., not equal. Consistency being one of the key values in any good
document design, the spacing around the \texttt{=} signs
should be equal on both sides (not counting stretch), no matter which
construction is used.
Since \texttt{eqnarray} is (naively) built on top of the
\texttt{array} environment we still have the \cs{arraycolsep} space between
columns, which then affects the spacing around the \texttt{=}'s in our
case. We could change the value of \cs{arraycolsep}:
\begingroup
\begin{VerbCode}
\setlength\arraycolsep{1.4pt}% some length
\[ \dbx = \dbx \]
\begin{eqnarray*}
\dbx & = & \dbx \\ &= & \dbx
\end{eqnarray*}
\end{VerbCode}
Resulting in:
\ShowVerbFile*[x]
\endgroup
\noindent
Changing the value of \cs{arraycolsep}, however, will also change the
appearance of any other construction that might be using \texttt{array}, so
this does not suffice; see the following example.
\begingroup
\begin{VerbCode}
Before the change:
\begin{eqnarray*}
A &=& \left(\begin{array}{cc}\dbx&\dbx\\
\dbx&\dbx\end{array}\right)
\end{eqnarray*}
after the change:
\setlength\arraycolsep{1.4pt}% some length
\begin{eqnarray*}
A &=& \left(\begin{array}{cc}\dbx&\dbx\\
\dbx&\dbx\end{array}\right)
\end{eqnarray*}
\end{VerbCode}
Resulting in:
\ShowVerbFile*[x]
\endgroup
\noindent
Some people argue that this larger spacing is a good thing, that it
helps understanding the equations in question. For that to be true the
author should do this with every single equation, whether the equation
was written using \texttt{eqnarray} or not. Consistency above all. We
can plainly see that \texttt{eqnarray} does not follow the spacing
conventions Knuth set out in \TeX, whereas both
\texttt{equation} and \cs{[}\dots\cs{]} do.
Here is another example from a set of notes I have been editing
(actual code from the original unedited notes).
\begin{VerbCode}
\begin{eqnarray*}
{\cal C}_{0} &\subseteq& {\cal C}\subseteq
\sigma ({\cal C}_{0},{\cal N}) ,
\end{eqnarray*}
\end{VerbCode}
\ShowVerbFile*
\noindent
Which makes one wonder if \LaTeX\ authors even notice the
difference in spacing, or do they just accept it as a fact of life?
Even though \texttt{eqnarray} might not be recommended for one-liners,
they do still appear quite a lot in the `wild'.
% As does the use of
% \texttt{align} for one-liners, which in some cases might also not be a
% good idea compared to e.g.\ \texttt{equation}.
As \texttt{eqnarray} is the only multi-line construction for plain
\LaTeX, what should be used instead? Short answer: Use the
environments from the \texttt{amsmath} package, in particular the
\texttt{align} environment.
Longer answer: There are a few packages that can help including
\texttt{nath}, \texttt{mathenv} and \texttt{amsmath}. Using
\texttt{amsmath} is highly recommended since it is already
included as part of every \LaTeX\ installation.
For those not familiar with the \texttt{amsmath} package we present a
few useful constructions in Appendix~\ref{amsmath}.
\subsection{Problem \#2: eqnarray can overwrite equation numbers}
\label{sec:may-overwr-equat}
Given a long formula which happens to fit within one line were it
not for the equation number, \texttt{eqnarray} will happily just
ignore the equation number, without any warnings.
\begin{VerbCode}
\begin{eqnarray}
\dbx &=& \dbx[12cm]
\end{eqnarray}
\end{VerbCode}
\ShowVerbFile*
It can get even worse. Assume we are using the
\texttt{leqno} class option, i.e.\ equation numbers on the left. Then
assume we have a math line that is slightly longer than the text
width:\footnote{Example provided by Barbara Beeton.}
\begingroup
\makeatletter
% this temporary move the eqn number to the left side
\renewcommand\@eqnnum{\hb@xt@.01\p@{}%
\rlap{\normalfont\normalcolor
\hskip -\displaywidth(\theequation)}}
\makeatother
\begin{VerbCode}
Left text edge \hfill right text edge%
\begin{eqnarray}
\dbx &=& \dbx[13.5cm]
\end{eqnarray}
\end{VerbCode}
\ShowVerbFile
\endgroup
\noindent
No offence, but why on earth is \texttt{eqnarray} moving the equation
number? Let us see what happens if we take the same example and
switch back to equation numbers on the right:
\begin{VerbCode}
Left text edge \hfill right text edge%
\begin{eqnarray}
\dbx &=& \dbx[13.5cm]
\end{eqnarray}
\end{VerbCode}
\ShowVerbFile
\noindent
\emph{Sigh\dots}
Well, at least that will teach authors to remember to break their
equations properly.
\bigskip\noindent
At least the environments from the \texttt{amsmath} bundle take the
equation number into consideration. Here is an example using
\texttt{align}:%
\begin{VerbCode}
\begin{align}
\dbx &= \dbx[12.5cm]
\end{align}
\end{VerbCode}
\ShowVerbFile*
\subsection{Problem \#3: Silence of the labels}
\label{sec:silence-labels}
Part of my job is to process a preprint series published by my
department. This brings me into contact with many different styles of
\LaTeX{} writing and usage. One thing that I frequently do (as part
of my visual improvement procedures) is convert \texttt{eqnarray}
environments into \texttt{align} environments (or similar). This is
where one starts to find the hidden label errors. Most often these occur
when two or more people have been writing/editing the same file.
Here is the first example:
\begin{VerbCode}
\begin{eqnarray}
\dbx & = & \dbx \\
\dbx & = & \dbx \label{eq:2} \nonumber
\end{eqnarray}
From equation (\ref{eq:2}) we conclude
\begin{equation}
\dbx=42.
\end{equation}
\end{VerbCode}
So the author had an equation which he or she no longer wanted to have
numbered (\cs{nonumber}). Which is perfectly reasonable, but the
author neglected to check whether the now-dead label (\texttt{eq:2}) was
referred to. The result is as follows:
\ShowVerbFile*
\noindent
Huh? This might end up as an interesting form of argumentation. It
seems as if \texttt{eqnarray} actually steps up the equation counter
at the start of every line (hence \cs{label} catches something) and
when it encounters \cs{nonumber} it does not write any equation number
and steps the equation counter one down again.
On a side note, \texttt{equation} has the same problem if one mixes it
with \cs{nonumber} (something which is \emph{not} fixed by using
\texttt{amsmath}).
The worst thing here is that \texttt{eqnarray} does this silently,
without warnings, so if you do not know that this might happen you
will never notice it unless someone carefully reads the article.
As it happens, I recently received an article which showed exactly
the same problem in \texttt{eqnarray*}. Here one only has to place a
label inside a non-numbering \texttt{eqnarray*} (we use
\cs{theequation} to show the current value of the equation number):
\begin{VerbCode}
Equation number before the equation: \theequation
\begin{eqnarray*}
\label{eq:4}
\text{Inside the equation \theequation} & = & \dbx
\end{eqnarray*}
The reference is \eqref{eq:4}.
Equation number after the equation: \theequation
\end{VerbCode}
Resulting in:
\ShowVerbFile
\noindent
Who smells a rat? So, even in \texttt{eqnarray*} the equation counter
is stepped up, and later stepped down at the end of each line. As we
have seen, this is a problematic approach.
% On a different note, I have been told that the \texttt{hyperref}
% package internally has to do a lot of nasty stuff to actually work
% with \texttt{eqnarray}.
\subsection{Problem \#4: The amsthm package vs.\ the eqnarray environment}
\label{sec:amsth-vs.-eqnarray}
If one uses the \texttt{amsthm} package, and its \texttt{proof}
environment, then you will get automatic placement of an ``\emph{end of
proof}'' marker. Sometimes one ends a proof with a displayed formula
and may want to place the end marker near the equation number. This
may be achieved by simply issuing \cs{qedhere} on the last line of the
formula.
\begin{VerbCode}
\begin{proof}
\dots
\begin{equation*}
a=0. \qedhere
\end{equation*}
\end{proof}
\end{VerbCode}
\ShowVerbFile
\noindent
This handy little feature, as one might guess by now, does \emph{not} work with
\texttt{eqnarray}!
\section{Solution}
\label{sec:solution}
The best solution is to \emph{not} use the \texttt{eqnarray}
environment at all. Use the environments from \texttt{amsmath}
instead. If in some case that will not do, the \texttt{mathenv} package
reimplements \texttt{eqnarray} to work more rationally. It also removes
the restraint on the number of columns in an
\texttt{eqnarray}. (Unfortunately, \texttt{mathenv} is not
compatible with certain useful modern packages, notably
\texttt{siunitx}.)
% But sadly we still see a lot of journals or publishing houses who in
% their guides for authors still recommend the use of \texttt{eqnarray},
% or at least mention it in their guides. It is quite rare to see
% \texttt{amsmath} mentioned in these guides.
Sadly we see many journals and publishers who still
recommend (or at least mention) the use of \texttt{eqnarray} in
their guides for authors.
\appendix
\section{The \texorpdfstring{\texttt{amsmath}}{amsmath} package}
\label{amsmath}
For more information about \texttt{amsmath} see \cite{shortmathguide},
\cite{amsldoc} and \cite{mathmode} (in order of recommended reading).
This appendix gives a few interesting constructions, mainly
showing replacements for common \texttt{eqnarray} usage.
All of the following examples require \texttt{amsmath}, hence the
document preamble must include:
\begin{verbatim}
\usepackage{amsmath}
\end{verbatim}
One thing to note about \texttt{amsmath} is that \emph{every}
environment from \texttt{amsmath} that provides equation numbers also
has a \texttt{*}-version which does not. The package also includes an
\texttt{equation*} environment which is missing from plain \LaTeX.
Now the first thing we need is a replacement for \texttt{eqnarray}.
We choose \texttt{align}, which has a slightly different
syntax than \texttt{eqnarray}:
\noindent
\makeatletter
\renewcommand\dbx[1][.9cm]{\ifmmode\mathord{\@dbx{#1}}\else\@dbx{#1}\fi}
\makeatother
\begin{minipage}[t]{0.46\linewidth}\hfuzz=3.5pt\small
\begin{VerbCode}
\begin{eqnarray*}
\dbx &=& \dbx[1.5cm]\\
&=& \dbx
\end{eqnarray*}
\end{VerbCode}
\ShowVerbFile*
\end{minipage}
\hfill
\begin{minipage}[t]{0.46\linewidth}\hfuzz=3.5pt\small
\begin{VerbCode}
\begin{align*}
\dbx &= \dbx[1.5cm]\\
&= \dbx
\end{align*}
\end{VerbCode}
\ShowVerbFile*
\end{minipage}
\noindent
Note the reduced number of \texttt{\&}'s.
Here is another common \texttt{eqnarray} construction and its
\texttt{align} counterpart:
\noindent
\begin{minipage}[t]{0.46\linewidth}\hfuzz=3.5pt\small
\begin{VerbCode}
\begin{eqnarray*}
\dbx &=& \dbx[1cm]\\
& & + \dbx \\
&=& \dbx
\end{eqnarray*}
\end{VerbCode}
\ShowVerbFile*
\end{minipage}
\hfill
\begin{minipage}[t]{0.46\linewidth}\hfuzz=3.5pt\small
\begin{VerbCode}
\begin{align*}
\dbx = {} & \dbx[1cm]\\
& + \dbx \\
= {} & \dbx
\end{align*}
\end{VerbCode}
\ShowVerbFile*
\end{minipage}
\noindent Notice the use of \texttt{\{\}} when the \texttt{\&} is
placed to the \emph{right} of a relational symbol. Also note that the spacing
around the \texttt{+} is correct in the \texttt{align} case
but not when using \texttt{eqnarray}.
One construction not easily achieved with base \LaTeX\ is a formula
spread over several lines but with only one equation number for the entire
formula. Again, this is easy using constructions from the
\texttt{amsmath} package:
\begin{VerbCode}
\begin{equation}
\begin{split}
\dbx & =\dbx[3cm] \\
& =\dbx
\end{split}
\end{equation}
\end{VerbCode}
\ShowVerbFile*
\noindent Notice how the equation number is vertically
centred. The syntax for \texttt{split} is otherwise more or less the
same as for \texttt{align*}.
The \texttt{amsmath} package also provides the \texttt{aligned} (and
\texttt{alignedat}) environment, which is basically the full
\texttt{align} environment, but for \emph{inner} use. (Like
\texttt{eqnarray}, \texttt{split} can only have one so-called
alignment column, while \texttt{align} and \texttt{aligned} can have
several.)
\begin{VerbCode}
\begin{equation}
\begin{aligned}
%
\dbx & =\dbx &\qquad \dbx & =\dbx \\
& =\dbx & & =\dbx
\end{aligned}
\end{equation}
\end{VerbCode}
\ShowVerbFile
\subsection{What about \texorpdfstring{\cs{lefteqn}}{lefteqn}?}
\label{sec:what-about-cslefteqn}
\texttt{amsmath} has no direct equivalent to \cs{lefteqn}, but the idea
is still
useful. To recap, using the \cs{lefteqn} macro inside
\texttt{eqnarray}, one can force that particular line to be moved to
the left:
\begin{VerbCode}
\begin{eqnarray*}
\lefteqn{\dbx[2cm] = \dbx[2cm]} \\
&& = \dbx[2cm] \\
&& = \dbx[2cm]
\end{eqnarray*}
\end{VerbCode}
\ShowVerbFile*
\noindent
One usually uses this to mark the first line, and then give the
impression of the rest of the lines being indented.
The \texttt{mathtools} package does
provide an alternative, namely \cs{MoveEqLeft}:
\begin{VerbCode}
\begin{align*}
\MoveEqLeft \dbx[3cm] = \dbx[2cm] \\
& = \dbx[3cm] \\
& = \dbx[3cm]
\end{align*}
\end{VerbCode}
\ShowVerbFile*
\noindent
The idea is that the \cs{MoveEqLeft} marks an alignment
point (which is what the ampersands follow), and then pulls the line
backwards in a suitable fashion. It does \emph{not} take any
required arguments, unlike \cs{lefteqn}.
\newpage
\section*{Acknowledgements}
\label{sec:acknowledgements}
Special thanks to Barbara Beeton from the \acro{AMS} for comments and
suggestions for this revised version. Also many thanks to the various
people who provided examples for the original version of the article.
%\SetBibJustification{\raggedright \advance\itemsep by 2pt plus1pt minus1pt }
%\smallskip
\begin{thebibliography}{9}
\bibitem{amsldoc} American Mathematical Society,
\emph{User's Guide for the \textnormal{\texttt{amsmath}} Package},
2002. Normally included in every \LaTeX\ installation as
\texttt{amsldoc}; also available via
\ctanloc{macros/latex/required/amslatex/math}.
\bibitem{shortmathguide} Michael Downes, \emph{Short Math Guide},
2002. Short introduction to the \texttt{amsmath}
and
\texttt{amssymb} packages.
\url{ftp://ftp.ams.org/pub/tex/doc/amsmath/short-math-guide.pdf}
\bibitem{mathtools} Morten H\o gholm, Lars Madsen, Will Robertson and
Joseph Wright (maintainers), \emph{The mathtools package},
2011. Various extensions to \texttt{amsmath} and others.
\ctanfile{macros/latex/contrib/mh}.
\bibitem{mathmode} Herbert Vo\ss, \emph{Math mode}, 2006. Extensive
summary describing various mathematical constructions, both with and
without the \texttt{amsmath} package.
\ctanfile{info/math/voss/mathmode/Mathmode.pdf}.
\end{thebibliography}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: