33

Today I found out the existence of the aligned environment of the amsmath package. However, I can't see the advantage of using it instead of the array environment from the example provided in amsldoc.pdf, Sec.3.7. In particular, as I can just get almost the same result by using the array environment:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
    \left.\begin{aligned}
        B'&=-\partial\times E,\\
        E'&=\partial\times B - 4\pi j, 
    \end{aligned}
    \right\}
    \qquad \text{Maxwell’s equations}
\end{equation*}

\begin{equation*}
    \left.\begin{array}{l}
        B'=-\partial\times E,\\
        E'=\partial\times B - 4\pi j, 
    \end{array}
    \right\}
    \qquad \text{Maxwell’s equations}
\end{equation*}

\end{document}

giving the output

enter image description here

What are the differences, and why should one be preferred over the other in this context?

Moriambar
  • 11,466
jjdb
  • 2,238

3 Answers3

27

aligned basically gives you a sequence of r@{}l column pairs; thus there is no spacing between the two columns in a pair. However, a trick adjusts things so that x&=y gives the right spacing around the equals sign, or any other relation symbol, which is where usually alignment points are desired.

Some control is made so that lines aren't vertically too near, which isn't done in array. Here's a visual comparison. Note what must be done differently in array: one has to add {} before the = and use \dfrac.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\!\begin{aligned}
a&=bbb & c&=\frac{x}{y} \\
aa&=bbbb & cc&=\frac{x}{y}
\end{aligned}
\]
\[
\begin{array}{@{}r@{}l@{\quad}@{}r@{}l@{}}
a&{}=bbb & c&{}=\dfrac{x}{y} \\
aa&{}=bbbb & cc&{}=\dfrac{x}{y}
\end{array}
\]
\end{document}

(The \! is due to a glitch in amsmath that is kept for historical reasons; however it doesn't hurt much if no comparison has to be made. Newer versions of amsmath don't need it.)

enter image description here

In conclusion, array and aligned are different tools, each one has its pros and cons, depending on the problem that has to be solved: array is more flexible in terms of horizontal alignment of cells, aligned has other features that make it preferable in some situations. Don't forget alignedat, that allows to specify the horizontal space between column pairs.

Also to be noticed is that align and alignat have no array counterpart: with array one can't number lines (easily, at least).

egreg
  • 1,121,712
  • So if I do not write the \!, an equation with width of linewidth issues a warning (and maybe sticks out to the right that amount)? – jjdb May 02 '13 at 10:06
  • @jjdb A thin space is added (1/6 of a em, so it shouldn't really be a concern). – egreg May 02 '13 at 10:09
  • Ok, but in two-column articles one has often to fight to get the equation into one line. I encountered a few times the situation where adding a ! somewhere command made the black boxes from the draft mode vanish – jjdb May 02 '13 at 13:02
  • How does one number the lines with aligned? I can't find it in the documentation. – jjdb May 02 '13 at 16:01
  • @egreg Can you add information on the best practice for choosing either using array or aligned? I was just wondering again which one I should use. – Zelphir Kaltstahl Sep 16 '16 at 14:04
  • 1
    @Zelphir array is for, well, arrays; for aligning different equations, aligned or similar environments are better. – egreg Sep 16 '16 at 14:52
  • @egreg Ok aligned is clear for me then, but array is not so clear for me. When would you use array instead of some matrix then? Isn't an array a matrix? I don't know how to distinguish those yet. In mathematics itself I've never heard of arrays, only of matrices so far. However, I didn't study mathematics, might be I am missing something. – Zelphir Kaltstahl Sep 17 '16 at 23:40
  • 1
    @Zelphir You're right: matrices are built with array (but it's better to use the appropriate amsmath special environments); array is more general and can come handy in case some complex alignment is needed. – egreg Sep 18 '16 at 08:12
7

Note that in your example you have not used an alignment point & The = are aligned just because B and E happen to have the same width. If you changed your example, for example used W or a different expression altogether then you would have to work a bit harder to get the correct spacing around =.

David Carlisle
  • 757,742
3

Well, for one, notice the difference in spacing and the squeezing of the two lines. I am not aware of the internal mechanism that distinguishes the two, but in terms of usage, aligned is far simpler and easier, allows me to focus on the equation rather than array elements. A similar difference can be noticed in the matrix environments bmatrix, pmatrix, and vmatrix in amsmath vs creation of such matrices manually using various brackets and the array environment.

dnemoc
  • 491