[Tex/LaTex] Is eqnarray really obsolete

amsmathequationsobsolete

I just learned in eqnarray vs align that eqnarray should be avoided in general. Ok, so I started looking at my documents to get rid of it and (as suggested) use some environment of the amsmath package instead. However, I encountered two situations where this change yields a worse output (IMHO) than with eqnarray. This may be due to my lack of knowledge how to do it right, but first I will show the two situations:

The first one is a sequence of (in-)equalities used to prove a bound (as common in, e.g., runtime complexity analysis). This one here is used to show that 2^n does not grow faster than n!.

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{eqnarray*}
2^n & =                      & \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n \text{ times}}\\
    & \stackrel{n > 3}{=}    & 2 \cdot 2 \cdot 2 \cdot 2 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      & 16 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & <                      & 24 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      & 1 \cdot 2 \cdot 3 \cdot 4 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & \stackrel{n > 3}{\leq} & 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 \cdot \ldots \cdot n\\
    & =                      & n!
\end{eqnarray*}

\end{document}

It yields the following output:

enter image description here

These are my attempts to use some environment of the amsmath package instead:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
2^n & =                      \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n \text{ times}}\\
    & \stackrel{n > 3}{=}    2 \cdot 2 \cdot 2 \cdot 2 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      16 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & <                      24 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      1 \cdot 2 \cdot 3 \cdot 4 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & \stackrel{n > 3}{\leq} 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 \cdot \ldots \cdot n\\
    & =                      n!
\end{align*}

\[
\begin{aligned}
2^n & =                      && \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n \text{ times}}\\
    & \stackrel{n > 3}{=}    && 2 \cdot 2 \cdot 2 \cdot 2 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      && 16 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & <                      && 24 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      && 1 \cdot 2 \cdot 3 \cdot 4 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & \stackrel{n > 3}{\leq} && 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 \cdot \ldots \cdot n\\
    & =                      && n!
\end{aligned}
\]

\begin{alignat*}{2}
2^n & =                      && \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n \text{ times}}\\
    & \stackrel{n > 3}{=}    && 2 \cdot 2 \cdot 2 \cdot 2 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      && 16 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & <                      && 24 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & =                      && 1 \cdot 2 \cdot 3 \cdot 4 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
    & \stackrel{n > 3}{\leq} && 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 \cdot \ldots \cdot n\\
    & =                      && n!
\end{alignat*}

\end{document}

The corresponding outputs have the following flaws:

  • align:

enter image description here

The reduced space between the relation symbols and the terms they connect makes it harder to recognize the steps in this proof. While one can argue whether more space is a good thing here (I read "consistency over all" in one of the documents advocating the avoidance of eqnarray and I do not entirely agree on this – I think readability is more important than consistency while consistency supports readability in most cases but possibly not always), I think it is definitely not the best thing that the relations built with \stackrel are not aligned in a centered way below the other relation symbols. The latter point is also a flaw of all the other attempts to use an environment of the amsmath package (which is to be expected as these environments only offer columns being left- or right-aligned but not centered – at least I did not find a similar environment offering a centered column).

  • aligned:

enter image description here

While we do have more space on the right of the relation symbols, we have less space on the left of them.

  • alignat:

enter image description here

This is even worse than aligned since now the spacing is not only different between left and right of the relation symbols but we also have almost no space between the relation symbols built with \stackrel and the next term.

The second situation is the definition of substitutions as comma-separated lists of replacements in square brackets (a notation quite common in, e.g., term rewriting or logic programming). If these definitions are too long such that we need to break them, the environments of the amsmath package do not succeed in aligning the second line correctly while keeping the same amount of space left and right of the = symbol:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{eqnarray*}
\sigma & = & [x_1/t_1,x_2/t_2,\\
       &   & x_3/t_3,x_4/t_4]
\end{eqnarray*}

\begin{align*}
\sigma & = [x_1/t_1,x_2/t_2,\\
       &   x_3/t_3,x_4/t_4]
\end{align*}

\begin{align*}
\begin{split}
\sigma & = [x_1/t_1,x_2/t_2,\\
       &   x_3/t_3,x_4/t_4]
\end{split}
\end{align*}

\[
\begin{aligned}
\sigma & = && [x_1/t_1,x_2/t_2,\\
       &   && x_3/t_3,x_4/t_4]
\end{aligned}
\]

\begin{alignat*}{2}
\sigma & = && [x_1/t_1,x_2/t_2,\\
       &   && x_3/t_3,x_4/t_4]
\end{alignat*}

\end{document}
  • eqnarray:

enter image description here

  • align:

enter image description here

Here, the two versions with and without the use of split yield the same output. The spacing around = is consistent but the alignment of the second line is wrong.

  • aligned:

enter image description here

Here, the alignment is ok, but the spacing is inconsistent.

  • alignat:

enter image description here

Again, the alignment is ok but the spacing is inconsistent.

So my question is: How can these two situations be handled without using eqnarray yielding an output which is at least as readable as the versions using it and with a similar (or less) amount of code?

Of course, one can use array and customize it accordingly, but what would be the point of doing so in preference of using the existing eqnarray? Also, in Aligning a split up equation not at equal sign (which is similar to my second situation) the only answer suggests to use \phantom in combination with align to fix the alignment. While this is of course possible, it is definitely more tedious than using eqnarray in such a situation as described above.

Best Answer

The output of eqnarray is wrong anyway and there's little to do about it. Consider also that it doesn't work with cross-references if hyperref or cleveref are involved.

No, there's no reason for using it.

The first example can be dealt with using array:

\documentclass{article}

\usepackage{amsmath,array}

\begin{document}

\begin{equation*}
\renewcommand{\arraystretch}{1.5}
\begin{array}{@{} r @{} >{{}} c <{{}} @{} l @{} }
2^n
 & =                   & \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n \text{ times}}\\
 & \overset{n>3}{=}    & 2 \cdot 2 \cdot 2 \cdot 2 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
 & =                   & 16 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
 & <                   & 24 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
 & =                   & 1 \cdot 2 \cdot 3 \cdot 4 \cdot \underbrace{2 \cdot 2 \cdot \ldots \cdot 2}_{n - 4 \text{ times}}\\
 & \overset{n>3}{\leq} & 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 \cdot \ldots \cdot n\\
 & =                   & n!
\end{array}
\end{equation*}

A better proof, by induction, is
\begin{alignat*}{2}
2^4     &= 16 < 4! = 24  \\
2^{n+1} &= 2\cdot 2^n    &\quad&\text{for $n>3$} \\
        &< 2\cdot n!     &\quad&\text{induction hypothesis}\\
        &< (n+1)\cdot n! \\
        &= (n+1)!
\end{alignat*}
\end{document}

enter image description here

The second example is even easier: use a good alignment point:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
\sigma =[ &x_1/t_1,x_2/t_2,\\
          & x_3/t_3,x_4/t_4]
\end{align*}

\end{document}

enter image description here

More lines can be accommodated:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
\sigma = [ &x_1/t_1,x_2/t_2, \\
           & x_3/t_3,x_4/t_4] \\
\tau   = [ &y_1/t_1] \\
\psi   = [ &x_1/t_1,x_2/t_2,\\
           & x_3/t_3,x_4/t_4]
\end{align*}

\end{document}

enter image description here