# Multiple alignments within align environment

My minimum working example:

\documentclass{report}

\usepackage{amsmath}

\begin{document}

\begin{align}
\dot{x}_2 =  \dot{x}_{2,0}&+\frac{\partial f_2(x_1)}{\partial x_1} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})+\\
&+ \frac{1}{2}\frac{\partial {f_2}^2(x_1)}{\partial {x_1}^2} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})^2+ \\
&+ \frac{1}{6}\frac{\partial {f_2}^3(x_1)}{\partial {x_1}^3} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})^3+ \\
&+   g_2  (u-u_0)
\end{align}

\end{document}


The result:

Now I would like to obtain the following result instead:

Any ideas?

-
You could use \phantom{\frac{1}{2}} in the first line. By the way, do you really need a “+” at the end of each line? –  Manuel Apr 30 at 15:39
Is this some hideous homework exercise where you need to get everything lined up perfectly to get the marks? I had one of those as an undergrad - you got docked a point if you didn't notice a capital A was suddenly in 10pt font when the rest was 12pt... –  FionaSmith Apr 30 at 15:50
I think you mean \frac{\partial^k {f_2}(x_1)}{\partial {x_1}^k} and not \frac{\partial {f_2}^k(x_1)}{\partial {x_1}^k}. Also, do you really want each line numbered? Most of the time an equation gets only one number at most. You can use alignat* for no numbers and aligned for one number. –  Matthew Leingang Apr 30 at 23:51

Yep! alignat

\documentclass{report}

\usepackage{amsmath}

\begin{document}

\begin{alignat}{3}
\dot{x}_2 =  \dot{x}_{2,0} \,& + \,& \,\frac{\partial f_2(x_1)}{\partial x_1} \bigg|_{x_1=x_{1,0}} &       (x_1-x_{1,0}) &+\\
&+& \,\frac{1}{2}\frac{\partial {f_2}^2(x_1)}{\partial {x_1}^2} \bigg|_{x_1=x_{1,0}}  &     (x_1-x_{1,0})^2 &+ \\
&+& \,\frac{1}{6}\frac{\partial {f_2}^3(x_1)}{\partial {x_1}^3} \bigg|_{x_1=x_{1,0}}  &     (x_1-x_{1,0})^3 &+ \\
&+ \protect\makebox[0pt][l]{\,\,$g_2 (u-u_0)$}\,
\end{alignat}

\end{document}


Note: you should take care with the number in curly brackets - it's to do with the total number of ampersands. I think it's supposed to be total + 1 divided by 2, but I have four... I had just copied and pasted from a bit of my own code. Perhaps someone will explain the number of ampersands thing better! The first ampersand marks an alignment point, and the next column will be right aligned. If you put in another, it will then be left aligned for the subsequent column so you can put && in if you want to immediately left align.

Note I put a few \, spacing commands in to line stuff up with slightly bigger gaps to match what you were looking for.

Now tell me, I'm confused: since you have uploaded exactly what you wanted - how did you generate that?

-
Thanks, that's looks good. Only thing is that the term $g_2(u-u_0)$ is not located at the correct location. By the way, I used Paint to shift some terms. –  Pietair Apr 30 at 15:36
oh yeah didn't notice that! I will shamelessly copy in that part of Stephen's answer since I would have had no idea how to get that part to left align. Anyway, now you have two options! –  FionaSmith Apr 30 at 15:43

Use alignat and \mathrlap from the mathtools package for the last line.

The alignat environment has an implicit {rlrl...rlrl} alignment. The argument is the number of the r columns.

The command \mathrlap puts its argument in a zero width box and aligns it left.

\documentclass{report}

\begin{document}
\begin{alignat}{3}
\dot{x}_2 =  \dot{x}_{2,0}&+{}&\frac{\partial f_2(x_1)}{\partial x_1} \bigg|_{x_1=x_{1,0}}
&(x_1-x_{1,0})&{}+{}\\
&+{}& \frac{1}{2}\frac{\partial {f_2}^2(x_1)}{\partial {x_1}^2} \bigg|_{x_1=x_{1,0}}       &(x_1-x_{1,0})^2&{}+{} \\
&+{}& \frac{1}{6}\frac{\partial {f_2}^3(x_1)}{\partial {x_1}^3} \bigg|_{x_1=x_{1,0}}       &(x_1-x_{1,0})^3&{}+{} \\
&+\mathrlap{g_2(u-u_0)}
\end{alignat}
\end{document}


I have used &+{}& and &{}+{} to get the right spacing around the +.

-
not come across mathrlap - I'm off to investigate. Note you need to align the plus signs at the ends of the lines too for a complete solution :) –  FionaSmith Apr 30 at 15:47
The spacings of the +'s at the end are also wrong ;-) –  daleif Apr 30 at 15:54
I have aligned the + at the end and changed the spacings around of them. –  esdd Apr 30 at 16:16

Here, I did it with a tabular stack. Note, however, that it will only allow a single equation number to be applied to the result. However, since it is a single equation, I thought that might be okay (if you want the vertical position of the number to be adjusted, that can be done).

The only real quirk to the solution was getting the g_2 term left aligned in a right-aligned column. For that, I just grafted it to the right side of the preceding + sign.

\documentclass{report}
\usepackage{tabstackengine}
\stackMath
\usepackage{amsmath}
\begin{document}
$$\setstackgap{S}{6pt} \TABbinary \setstacktabulargap{0pt} \tabularShortstack{rcrlc}{ \dot{x}_2 = \dot{x}_{2,0}&+&\dfrac{\partial f_2(x_1)}{\partial x_1} \bigg|_{x_1=x_{1,0}}& (x_1-x_{1,0})&+\\ &+& \dfrac{1}{2}\dfrac{\partial {f_2}^2(x_1)}{\partial {x_1}^2} \bigg|_{x_1=x_{1,0}}& (x_1-x_{1,0})^2&+ \\ &+& \dfrac{1}{6}\dfrac{\partial {f_2}^3(x_1)}{\partial {x_1}^3} \bigg|_{x_1=x_{1,0}}& (x_1-x_{1,0})^3&+ \\ &+ \protect\makebox[0pt][l]{g_2 (u-u_0)}& &&& }$$
\end{document}


-
I just stole your answer for the last line to correct my alignat solution but I don't know how to tag you without commenting on your own post! –  FionaSmith Apr 30 at 15:48
@FionaSmith No problem. To paraphrase the humorous lyrics of Tom Lehrer (youtube.com/watch?v=HfjFnjWjDOI), "plagiarize, only be sure always to call it please, 'research'." 8^b [My retort is just humor. What you did is perfectly acceptable] –  Steven B. Segletes Apr 30 at 16:06

You can keep the alignment points in the align environment as they are now, and just insert a couple of \phantom instructions in the first row to achieve the needed spacing adjustments.

By the way, I would recommend you omit the + symbols at the ends of rows 1, 2, and 3 since they're made redundant by the + symbols at the start of rows 2, 3, and 4.

\documentclass{report}
\usepackage{amsmath}
\begin{document}
\begin{align}
\dot{x}_2 =  \dot{x}_{2,0}&+\phantom{\frac{1}{2}}\frac{\partial {f_2}\phantom{^1}(x_1)}{\partial x_1} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})\\
&+ \frac{1}{2}\frac{\partial {f_2}^2(x_1)}{\partial {x_1}^2} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})^2\\
&+ \frac{1}{6}\frac{\partial {f_2}^3(x_1)}{\partial {x_1}^3} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})^3\\
&+   g_2  (u-u_0)
\end{align}
\end{document}


However, if these + symbols must be there, I would recommend you (a) write +{} instead of just + for these symbols to get proper spacing and (b) append \phantom{^1} to (x_1-x_{1,0}) in the first row to further fine-tune the spacing.

Finally, if you wanted the vertical space between the third and fourth row to be the same as between the other rows, you could add the instruction \phantom{\bigg|} after g_2 (u-u_0). With this adjustment made, and the + symbols at the ends of the first three rows not deleted, your equations would look like this:

\documentclass{report}
\usepackage{amsmath,}
\begin{document}
\begin{align}
\dot{x}_2 =  \dot{x}_{2,0}&+\phantom{\frac{1}{2}}\frac{\partial {f_2}\phantom{^1}(x_1)}{\partial x_1} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})\phantom{^1} +{}\\
&+ \frac{1}{2}\frac{\partial {f_2}^2(x_1)}{\partial {x_1}^2} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})^2 +{}\\
&+ \frac{1}{6}\frac{\partial {f_2}^3(x_1)}{\partial {x_1}^3} \bigg|_{x_1=x_{1,0}}       (x_1-x_{1,0})^3 +{}\\
&+   g_2  (u-u_0)\phantom{\bigg|}
\end{align}
\end{document}

-
Why not ^{\phantom 3} instead of \phantom{^1}? Might not the latter be thinner than the 3 superscript? –  Matthew Leingang Apr 30 at 23:54
@MatthewLeingang - the numerals of the font in use (Computer Modern) are tabular-style (as well as lining style). Hence, the 1 and the 3 take up the same horizontal space. The objects \phantom{^1} and \phantom{^3} also take up the same amount of space. –  Mico May 1 at 0:11