TeXblog

 Typography with TeX and LaTeX

eqnarray vs. align

12 April 2008 by Stefan Kottwitz

There’s a lot of freely available documentation for LaTeX, but there’s a pitfall: some documents that are still online are outdated and therefore contain obsolete information. Documents like “Obsolete packages and commands” (”l2tabu”) address the need of up-to-date information.

For instance the obsolete eqnarray environment frequently appears in questions of new LaTeX users and many people including me usually answer: don’t use eqnarray and give advice how to use the align environment of amsmath instead.

Here’s a summary of the problems with eqnarray:

  • the spacing around relation symbols are inconsistent,
  • long equations might collide with the equation numbers,
  • there could be problems with labels and references.

Here is one small example document just to illustrate the space inconsistany problem:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{minipage}{0.5\textwidth}
equation:
\begin{equation*}
  z_0 = d = 0
\end{equation*}
\begin{equation*}
  z_{n+1} = z_n^2+c
\end{equation*}
align:
\begin{align*}
  z_0 &= d = 0 \\
  z_{n+1} &= z_n^2+c
\end{align*}
eqnarray:
\begin{eqnarray*}
  z_0 &=& d = 0 \\
  z_{n+1} &=& z_n^2+c
\end{eqnarray*}
\end{minipage}
\end{document}

Compile for yourself and examine it, if you want. For a quick look here’s a screenshot of the output:

eqnarray vs. align

Notice the difference of the spacing around the equal sign in the eqnarray environment compared to equation and even compared to the other equal sign inside the first eqnarray line.
If you try to repair the spacing by adjusting \arraycolsep you will notice that all other arrays including matrices will be affected too. So the best solution is to use amsmath, this package provides even more environments useful for multiline formulas and a lot more enhancement for mathematical typesetting.

See the amsmath user’s guide.

For further information regarding this topic you may have a look at the article “Avoid eqnarray!” by Lars Madsen published in the PracTeX Journal 4 2006.

This topic was discussed on MatheBoard.

This entry was posted on 12 April 2008 at 10:08 PM and is filed under Mathematics. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

36 responses about “eqnarray vs. align”

  1. Catherine Davey said:

    Very useful! I have been wondering what the difference is. Thanks!

  2. Brad said:

    Where is the documentation and specifications for the align environment ? For example, how do you control if it aligns a certain column left or right ?

  3. Stefan Kottwitz said:

    Hi Brad,

    the align environment has an implicit {rlrl…} alignment of its columns. The environment is documented in the amsmath user’s guide, further information can be found in the general Mathmode document.

    Stefan

  4. Bob said:

    One problem with the align environment is it appears that the \pause command in Beamer does not work in align as it does in eqnarray. This may seem minor, but it’s important in my lectures to be able to step through multiline equations. Align does seem to be much nicer otherwise, so I would appreciate if anyone has advice on the pauses. Thanks!

  5. David Schaich said:

    Bob,

    Beamer’s \pause doesn’t actually work inside any amsmath environment, “since these do really wicked things” according to the Beamer manual (v.3.06, page 63). You can find some workarounds through Google, though just using eqnarray in this case is the simplest.

  6. Stefan Kottwitz said:

    You could step through align environments using the \uncover macro. It works for unnumbered (starred) and numbered align environments, in the last case an empty untagged last line is necessary, see the beamer user guide 23.4 Uncovering Tagged Formulas Piecewise.

  7. Bob said:

    Thanks for the responses. The \uncover solution works well. I wish I would have checked back here sooner. I’ll be using that a lot from now on. Thanks.

  8. Christian said:

    Hi, i’ve got a Problem with \align.
    According to your article I started to change the \eqnarray s in my documents into \align s. But in a certain case, don’t know why, I get a strange space, here is an example:

    \documentclass{scrreprt}

    \usepackage{amsmath}

    \begin{document}

    \begin{eqnarray}
    a & = & b\\
    b & = & c
    \end{eqnarray}

    \begin{align}
    a & = & b\\
    b & = & c
    \end{align}

    \end{document}

    I’d be happy if you could explain to me, where these huge space in the align between = and b comes from and how I can avoid it. Thanks.

  9. Stefan Kottwitz said:

    Hi Christian!

    In align environments the & symbol doesn’t separate columns, it marks the relation symbol. The correct way would be:

    \begin{align}
    a & = & b \\
    b & = & c
    \end{align}

    The second & would cause an additional align-column, that’s why there’s that space. To see it try:

    \begin{align}
    a &= b & b &= a \\
    b &= c & c &= b
    \end{align}

    Stefan

  10. Allen K. said:

    I’ve got a different problem with align. I would like to label an equation with tag{H_T}, but the subscript gets it really confused and unhappy. tag{{H_T}} doesn’t fix it. Any ideas?

  11. Stefan Kottwitz said:

    Hi Allen,

    use math mode in the argument of tag, \tag{\(H_T\)} or \tag{$H_T$}.

    Stefan

  12. Allen K. said:

    Excellent, thank you!

  13. Christian said:

    Thanks for your answer, now I understand how align works, think I’m gonna use it every time now. ;-)

    Christian

  14. andrew said:

    Hi all,

    I have a couple more questions on align:

    Is it possible to adjust the multi-column behavior in align? I’m looking for something like array, but that could display math in equation-style (big vees etc.)

    Also, can I add/eat the space between the lines? I want to have dots leading to the last equation, but the line containing \vdots is unevenly spaced with the ones around it:

    \begin{align*}
    A_1 &= B_1\\
    &\ \ \vdots\\
    A_n &= B_n
    \end{align*}

    Thanks a lot,
    Andrew

  15. Stefan Kottwitz said:

    Hi Andrew,

    concerning multiple columns, amsmath provides several align-like environments, have a look at amsldoc.pdf. I’m often using the alignat environment when several columns are needed.
    Line specific spacing is possible, just state the space in square brackets after the linebreak, for instance:

    \begin{align*}
    A_1 &= B_1\\
    &\ \ \vdots\\[5pt]
    A_n &= B_n
    \end{align*}

    Negative space is possible as well:

    A_1 &= B_1\\[-5pt]

    Stefan

  16. andrew said:

    Thank you Stefan!

  17. Anna said:

    I have a problem with align. I’d like to label each row with \label{} so that I can refer to the equations individually. I get an error message that multiple labels are not possible.
    \begin{align}
    a &=b\label{eq1}\\
    c &=d\label{eq2}
    \end{align}

  18. Stefan Kottwitz said:

    Hi Anna,

    there’s no problem in this code. This complete example is compilable without error and refers to both equations:

    \documentclass[a4paper,10pt]{article}
    \usepackage{amsmath}
    \begin{document}
    \begin{align}
    a &=b\label{eq1}\\
    c &=d\label{eq2}
    \end{align}
    See \eqref{eq1} and \eqref{eq2}. 
    \end{document}

    Try this one. Perhaps post a complete example here, like I did, that shows the error. Something else causes this problem, not visible in the piece of code here.

    Stefan

  19. Alem said:

    Hi all,
    I just have few questions on fancy headings should some one extends a helping hand. I am writing a thesis and want to use this fancy thing. Here is what i have so far:
    \documentclass[reqno,12pt, twoside ]{article}
    \usepackage{fancyheadings}
    \renewcommand{\sectionmark}[1]{\markright{\thesection \ #1}{}}
    \renewcommand{\subsectionmark}[1]{\markright{\thesubsection \ #1}{}}
    \rhead{\leftmark\\\rightmark}
    \fancyhead[LE,RO]{\slshape \rightmark}
    \fancyhead[LO,RE]{\slshape \leftmark}
    \fancyfoot[LE,RO]{\thepage}
    My questions:1. I want to remove the heading for the main titles(sections) while keeping the page number as it used to be(left or right depending on…)
    2. I want to have the section title on the left hand side of the (even numbered pages) and the subsection title on the right hand side of (odd pages).
    Any help would be appreciated.

  20. Stefan Kottwitz said:

    Hi Alem,

    I recommend to use fancyhdr instead of fancyheadings, this is an early version. Since you are writing a thesis consider to use another class than article. The article class is appropriate for smaller documents. You could use report or book instead, there are even some dedicated thesis classes.
    book and report provide chapters. chapter starting pages would have another style, numbered but without heading. That may be what you would like to achieve.
    Have a look at the fancyhdr documentation for explanation and examples. If you still have questions just write them here.

    Stefan

  21. Alem said:

    Thank you Stefan. I have been advised not use this chapter thing. If i do, i have to make sure that the word chapter should not appear where the chapter is supposed to begin. I can suppress it using \chapter*{title}. The problem is then i need to have the chapter number before the title.

    Yes you right, with this book or report document classes, the header from the chapter or mains section will disappear which is what i want. The problem with this is again, it puts the page number for that page at the center. I want the page number to be on the right.

  22. Stefan Kottwitz said:

    Hi Alem,

    using a KOMA class could help. They are much more customizable. Further they don’t write out “Chapter” and they provide a very good package for headings and footers. The position of the page number looks also like you wished.
    Test this small example:

    \documentclass[a4paper,10pt]{scrbook}
    \usepackage[automark]{scrpage2}
    \pagestyle{scrheadings}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \begin{document}
    \Blinddocument
    \end{document}

    The documentation will explain more, there’s a whole chapter dedicated to headings.

    Stefan

  23. Alem said:

    Thanks Stefan. This was what i had been looking for.It solved my problem!!!

  24. Corinna said:

    I’ve been using align for years, however, the environment doesn’t seem to be a part of {iopart, iopams} (for publication in AMS journals), while eqnarray works - so there actually are situations where you can’t avoid using it. …and yes, the spacing is ugly!

  25. athens said:

    how to move the equation to left-hand side or right-hand side?

  26. Stefan Kottwitz said:

    To place the equations on the left side for the whole document, use the option fleqn for the document class or for the amsmath package.
    Alternatively, to align equation arrays to the left or to the right side, you could use the flalign environment. Just use an additional column separator & on the right respectively the left side.

  27. Jukka said:

    Thanks, you saved my day :)

  28. Yul said:

    Hello Mr. Stefan!

    I would like to thank for the excelent post! I was using eqnarray a lot… However, it seems that there is a comment with a problem wich i have for my own, and is not answered… It seems that the problem is with the package “ntheorem”

    The problem is with align’s labels.

    \documentclass[a4paper,11pt,twoside]{article}
    \usepackage[utf8]{inputenc}
    \usepackage[portuguese]{babel}
    \usepackage[pdftex]{graphicx}
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{amsmath}
    \usepackage[thmmarks]{ntheorem}
    \begin{document}

    \begin{equation}\label{e1}
    equation1
    \end{equation}

    \begin{align}
    equation2 \label{e2}\\
    equation3 \label{e3}
    \end{align}

    \begin{align}
    equation4 \label{e4}\\
    equation5 \label{e5}
    \end{align}

    \begin{equation}\label{e6}
    equation6
    \end{equation}

    Equations \ref{e1}, \ref{e2}, \ref{e3}, \ref{e4}, \ref{e5}, \ref{e6}
    Equations \eqref{e1}, \eqref{e2}, \eqref{e3}, \eqref{e4}, \eqref{e5}, \eqref{e6}.

    Then there are errors with labels
    ! Package amsmath Error: Multiple \label’s: label ‘e1′ will be lost.

    However, if i put

    \begin{equation}\label{e1}
    equation1
    \end{equation}

    \begin{align}
    equation2 \\
    equation3
    \end{align}

    Then ref{e1} gets reference to equation 2!
    It seems that if I remove ntheorem from the file, it compiles ok. Does anyone have the same problems? Is there a way to solve it? any link to proper documentation would help!

    Thank you very much.

  29. Stefan Kottwitz said:

    Hi Yul,

    the problem is indeed caused by ntheorem. But that package provides an option that takes care of it, just add amsmath as option:

    \usepackage[amsmath,thmmarks]{ntheorem}
  30. Alem said:

    Hi all,
    I want latex to generate footnote consecutive numbers automatically through out my document. I tried to use this command \footenote{text}.But it only generate consecutive numbers only with in a page. In a new page it always begins with 1 every time. I want all the footnotes through out the document to have different numbering say 1, 2…. I can do that manually using \footenote[#]{text}. But it seems cumbersome. Can any one help me how to do it?

  31. Stefan Kottwitz said:

    Hi Alem,

    which document class do you use? Do you load the footmisc package or any other footnotes-related package?

  32. Alem said:

    Hi Stefan,
    I use the document class scrbook. I loaded the footmisc package. But, it has not helped me.

  33. Stefan Kottwitz said:

    Hi Alem,

    that’s not the default behavior of scrbook and footmisc. In this example the second footnote will fall onto the second page and will get number 2:

    \documentclass{scrbook}
    \usepackage{footmisc}
    \begin{document}
    text\footnote{Test1}
    \newpage
    text\footnote{Test1}
    \end{document}

    So, there’s information missing. Perhaps post your preamble, I guess it’s caused by an option or package you didn’t mention. For instance, footmisc provides the option perpage, this causes such numbering per page. Or send me the preamble or a document by email to check.

    Stefan

  34. alem said:

    Hi Stefan,
    Here is my preamble
    \documentclass[12pt,a4paper,twoside,openright,BCOR=1cm]{scrbook}
    \usepackage{footmisc}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \usepackage{lmodern}
    \usepackage{fixltx2e}
    \usepackage{titlesec}
    \usepackage{amscd}
    \usepackage{amssymb}
    %\usepackage[margin=4cm]{geometry}
    %\usepackage[a4paper]{geometry}
    \usepackage{amstext}
    \usepackage{mathrsfs}
    \usepackage{relsize}
    \usepackage{mathptmx}
    \usepackage{makeidx}
    \usepackage{fancyhdr}
    \usepackage{textcomp}
    \usepackage[T1]{fontenc}
    \usepackage{verbatim, amsmath, amsfonts, amssymb, amsthm}
    \usepackage{microtype}
    \usepackage{setspace}
    \usepackage{pfnote}
    \usepackage{fnpos}
    \usepackage[final]{hyperref}
    \hypersetup{colorlinks=true, linkcolor=blue, anchorcolor=blue,
    citecolor=red, filecolor=blue, menucolor=blue, pagecolor=blue,
    urlcolor=blue}
    Alem

  35. Stefan Kottwitz said:

    Hi Alem,

    just remove that line:

    \usepackage{pfnote}

    The package pfnote resets the footnote counter for each page. As I believe that this is its only purpose, you would not need it.

    Stefan

  36. alem said:

    Thank you very much Stefan. I removed that, and got what i exactly want. you have been really of a great help!
    Alem

Leave a Reply