245

Motivation

I want to include a list of related equations, say, for a proof, in my LaTeX document. As far as I know, I have two good options, eqnarray and align.

Question

What is the difference between eqnarray and align, and how do I know which I should be using? Or does it matter at all?

strpeter
  • 5,215
Larry Wang
  • 3,485
  • 4
  • 23
  • 18

5 Answers5

222

Although eqnarray may seem to work "well enough", Avoid eqnarray! Avoid eqnarray! Avoid eqnarray!

Two main problems are mentioned in the doc above:

  • eqnarray sets horizontal space around the = operator that is not consistent with the space set in other environments, such as \[...\] or $$...$$ (it is wider).
  • eqnarray (also eqnarray* !) has an ill-defined equation numbering, which leads to numbering errors on referencing—mostly when using the command \nonumber

Use align and the rest of the ams environments. See texdoc amsldoc (PDF) or the Short Math Guide for LaTeX for documentation on how to use them.

Ari Brodsky
  • 2,596
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
  • 10
    "avoid eqnarray" has been updated and published in tugboat 33:1. this will be accessible only to tug members until spring 2013, but after that it will be open to anyone. – barbara beeton Dec 15 '12 at 17:50
  • 2
    Why do we have eqnarray then? – masu Oct 29 '13 at 22:43
  • 7
    @masu: eqnarray was written before the other (better) environments were written. Now that the better ones exist, there is no longer any reason to use eqnarray (unless for some reason you can't use the amsmath ones). – ShreevatsaR Oct 30 '13 at 04:03
  • @ShreevatsaR and I should have thought about backward compatibility. – masu Oct 30 '13 at 08:38
  • The TUGboat article mentioned by Barbara: http://www.tug.org/TUGboat/tb33-1/tb103madsen.pdf. – Speravir Mar 05 '14 at 01:37
  • 13
    Your answer lacks justification... please elaborate. – Paul Nov 27 '14 at 16:35
  • 7
    @Paul: I think the three linked justifications are more than enough. (Especially the first one: the entire article by Lars Madsen titled "Avoid eqnarray!".) Yes it would have been better to include some of them in the answer in case the links rot, but on the other hand it's hard to replicate those visual elements from a PDF to this HTML page (would have to take screenshots or something). – ShreevatsaR Nov 28 '14 at 01:23
  • http://tex.stackexchange.com/questions/285226/is-eqnarray-really-obsolete shows two cases where the use of align and friends is at least not obviously better than the use of eqnarray (unless you find a better way to use them - then add an answer there). – cryingshadow Jan 05 '16 at 13:34
  • 2
    @cryingshadow - As you've demonstrated in the linked posting, it is possible to come up cases for which the output of the basic, "out of the box" align environment isn't great. The solution in such cases, though, is not to resort to a deprecated and generally inferior method (eqnarray). Instead, as the answers to your posting demonstrate, the better solution is to use a method that's more flexible than the basic align environment and fully preserves interoperability with important packages such as hyperref and cleveref. – Mico Jun 08 '16 at 07:17
  • 2
    Sorry, I have to downvote because there's still no justification. I consider it bad form to post links without explanation (link-only answers are forbidden in some SE sites), and even if the linked pages provide more justification, as it is the answer gives me no clue at all as to why I should avoid eqnarray. Two of the links are to general documentation PDFs; one would have to search through the documents to find answers. At present, this isn't really an answer imo. Other people clearly disagree, because they upvoted it; that's their right. – Mars Mar 03 '18 at 17:10
  • 3
    One more point, now that I am reading "Avoid eqnarray!": You don't have to reproduce the entire document here. Even a simple summary like "eqnarray produces spacing that is inconsistent in these three ways" would be helpful. Some of the other answers make reference to spacing problems, but no answers here clearly summarize the spacing problems. – Mars Mar 03 '18 at 17:18
  • @Mars I agree with you now actually, especially because now I know how to take screenshots and don't consider it very unusual to have images in answers on this site. :-) (But about the links: the first two “avoid eqnarray” are about the specific topic even if the third only makes a mention. The last two general documentation links were only about how to use align and the rest of the AMS environments; was that not clear from the second paragraph?) – ShreevatsaR Mar 03 '18 at 17:20
  • You're right ShreevatsaR, "documentation on how to use them" does make that clear. My fault for not reading carefully. I don't think screenshots are essential, btw. – Mars Mar 03 '18 at 17:31
  • @ShreevatsaR "I think the three linked justifications are more than enough." The current StackExchange policy seems to be to at least incorporate the essence of linked content in order to fight link rot. This answer as it is is just a link only answer. The important part of the linked content should be edited in. – NoDataDumpNoContribution May 06 '19 at 09:12
70

align is from amsmath, while eqnarray is from base LaTeX, so I would expect the former to be better. Some differences:

  • eqnarray has two alignment points (it's basically just array with a default preamble); align has one. x + y &=& z versus x + y &= z
  • eqnarray changes the spacing at the alignment points depending on different factors; align keeps it fixed (which is generally what you want)
  • eqnarray allows page breaks between lines; align doesn't
  • \\ * is treated the same as \\* in eqnarray, but won't work in align (since * shows up commonly in equations)

(largely from The LaTeX Companion §8.2.1)

Michael Mrozek
  • 16,127
  • 1
  • 23
  • 26
  • 18
    i consider the first point to be an advantage of eqnarray. how can i simulate this behavior with align? i need it whenever i have operators of different width, e.g. = vs ==. i want = to be centered below ==, not left aligned to it. – peter Jul 24 '13 at 12:19
  • 7
    align allows page breaks between lines with the global declaration \allowdisplaybreaks. – skpblack Oct 03 '14 at 00:11
35

Besides the better spacing and the fewer ampersands to type, a big advantage of align vs eqnarray is that you can include a \qedhere at the end of the last line and have the nice CQFD square (also called a “Halmos”) placed at the same height as your last formula, and not underneath.

\begin{proof} The proof is a follows: 
\begin{align}
(x+y)^3&=(x+y)(x+y)^2\\
       &=(x+y)(x^2+2xy+y^2)\\
       &=x^3+3x^2y+3xy^3+x^3.\qedhere
\end{align}
\end{proof}

enter image description here

yannis
  • 2,013
  • 19
  • 18
20

The align environment only works if you use the AMS (American Mathematical Society) packages. If you need to use journal specific document classes or style files, the align environment may not be available. (For example, when I needed to use the iopart class for submission to an Institute of Physics journal, I had to change all my aligns to eqnarrays for the file to compile.

But unless you are forced to, I generally recommend the align environment. Here's a good write-up of what the differences are.

Willie Wong
  • 24,733
  • 8
  • 74
  • 106
13

If you must use the eqnarray environment, there's a package called eqnarray (available here) that at least removes the excessive space around the middle column. Compare:

\documentclass{article}

\usepackage{eqnarray,amsmath}

\begin{document}

\begin{eqnarray*}
A&=&B,\\
C&=&D,\\
E&=&F
\end{eqnarray*}

\begin{align*}
A&=B,\\
C&=D,\\
E&=F
\end{align*}

\end{document}

The package is for LaTeX 2.09, but it might still work.

I notice that, in my example, the eqnarray* and align* are centered very slightly differently (the eqnarray* is less than 1 point to the left of the align*). I'm not sure why. If you use {B,} and {D,} instead of B, and D, in the eqnarray*, the two displays come out centered exactly the same, so I assume that the ending punctuation symbols are causing the problem. (The default eqnarray* appears to have the same issue.)

David Carlisle
  • 757,742
MSC
  • 2,722
  • 1
    Does the package {eqnarray} works with Cleveref? That is one of the biggest drawbacks of {eqnarray} .... – Paulo Ney Apr 21 '18 at 16:28