# Get equations aligned

This is the result I need: there are four places to be aligned.

This is the code that I tried, and many other unsuccessful trials not shown here. Can you tell me how the symbol & really works in the align environment. As I don't know the principle, I just try things randomly and it costs me too much time. Thanks.

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{align*}
f(x)&= \cos x&&,  f'(x)&=& -\sin x &&, \\
f''(x)&= -\cos x&&,  f^{(3)}(x)&=&  \sin x &&,\\
&\vdots  &&  &\vdots&  && \\
f^{(2n)}(x)&=(-1)^n\cos x&&,  f^{(2n+1)}(x)&=&(-1)^{n+1}\sin x &&.
\end{align*}
\end{document}


## 2 Answers

You can do it with alignat. The space in the middle is inserted using the \vdots line just for keeping the symmetry of code in the other lines.

\documentclass{article}
\usepackage{mathtools}
\begin{document}

\begin{alignat*}{4}
f(x)      &={} &  \cos x      &&  f'(x)         &={} & -\sin x \\
f''(x)      &={} & -\cos x      &&  f^{(3)}(x)    &={} & \sin x \\
&\enspace\vdots  &&\hspace{4em}&&\enspace\vdots \\
f^{(2n)}(x) &={} & (-1)^n\cos x &&  f^{(2n+1)}(x) &={} & (-1)^{n+1}\sin x
\end{alignat*}

\end{document}


I'm not sure it's an effective way to show the property, though.

How does it work? With \begin{alignat*}{4} we're asking LaTeX to build four pairs of columns, each pair consisting of a right- and a left-aligned column. No space is added between columns and it has to be added manually where needed.

Note ={} because otherwise the spacing around the equals sign would not be correct. The left-aligned columns are set up so that they can start with an operation or relation sign and the spacing at their left is correct.

An alternative is using IEEEeqnarray from IEEEtrantools.

\documentclass{article}
\usepackage{mathtools}
\usepackage{IEEEtrantools}

\begin{document}

\begin{IEEEeqnarray*}{rCr c rCr}
f(x)      &=&  \cos x      &&  f'(x)         &=& -\sin x \\
f''(x)      &=& -\cos x      &&  f^{(3)}(x)    &=& \sin x \\
&\vdots     &         &\hspace{4em}&           &\vdots \\
f^{(2n)}(x) &=& (-1)^n\cos x &&  f^{(2n+1)}(x) &=& (-1)^{n+1}\sin x
\end{IEEEeqnarray*}

\end{document}


There are a few differences in input, but for complex alignments this might be a better choice.

The C column specifier means “centered operation or relation symbol”, so spacings are taken care of.

• Thank you so much egreg! I will explore more based on your code. Commented Mar 31 at 9:08
• @HaoranChen I added an alternative approach. I see that I forgot your commas, but you just add them after x (directly, not behind &). Commented Mar 31 at 9:55
• Thanks. That works! Commented Apr 1 at 15:04

Multiple equations per line are exactly the use case of the default spacing for align you use one & per equation and one & between each equation (never &=& for align)

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{align*}
f(x)       &= \cos x\,,     &  f'(x)        &= -\sin x\,,       \\
f''(x)     &= -\cos x\,,    &  f^{(3)}(x)   &=  \sin x\,,       \\
&\qquad\vdots    &               &\qquad\vdots       \\
f^{(2n)}(x)&=(-1)^n\cos x\,,&  f^{(2n+1)}(x)&=(-1)^{n+1}\sin x \,.
\end{align*}
\end{document}

• The commas need to be aligned as well. Commented Mar 31 at 9:09
• @HaoranChen I was suggesting that you don't align the commas (or don't have commas at all) aligning them makes the display harder to read. Note egreg simply removed them Commented Mar 31 at 9:11
• Egreg's code, with commas added, still works well. I am now using it. But if this can also be done with align, I am willing to see it as well. Thanks! Commented Mar 31 at 9:16
• @HaoranChen No, align is designed to give a standard layout with one alignment point per group. If you need more you have to space manually with alignat. In this case I think that produces a worse, harder to read, layout so decided to offer this alternative, but it's fine if you choose the answer that shows how to do exactly what you ask, you don't have to accept this one, but still others who come later may see this alternative. Commented Mar 31 at 9:19
• Sure. Thank you @David Carlisle Commented Apr 1 at 15:05