Math & ScienceI'm having trouble with aligning a really long equation

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
chrishans
Posts: 3
Joined: Sun May 15, 2011 4:24 am

I'm having trouble with aligning a really long equation

Post by chrishans »

I have the standard math packages (amsmath, etc), but I was running into trouble trying to use the following code:

Code: Select all

\begin{align*}
w &=& f(z,w) \\
&=& z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3 \\
&=& z^3 + (a_1z + a_2z^2)f(z,w) + (a_3 + a_4z)f(z,w)^2 + a_6f(z,w)^3 \\
&=& z^3 + (a_1z + a_2z^2)(z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3) + \\
& & (a_3 + a_4z)(z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3)^2 + a_6(z^3 + \\
& & (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3)^3 \\
&=& \ldots \\
&=& z^3 + a_1z^4 + (a_1^2 + a_2)z^5 + (a_1^3 + 2a_1a_2 + a_3)z^6 + \\
& & (a_1^4 + 3a_1^2a_2 + 3a_1a_3 + a_2^2 + a_4)z^7 + \cdots \\
&=& z^3(1 + A_1z + A_2z^2 + \cdots) 
\end{align*}
When I compile it into a pdf, it looks like this:
align.png
align.png (44.33 KiB) Viewed 3672 times
I want the equation to be left-aligned, but after searching for hours on end and trying many different things, I could not get it to work. I suspect it is because of the way I broke the lines with \\, but I do not know another way to fix this.

Any help would be appreciated, thank you.
Last edited by Stefan Kottwitz on Sun May 15, 2011 3:52 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

I'm having trouble with aligning a really long equation

Post by torbjorn t. »

Unlike eqnarray, the align environments from amsmath only requires one alignment character (&) per "column". Hence, you have to write

Code: Select all

\begin{align}
a &= b \\
c &= d
and not

Code: Select all

\begin{align}
a &=& b \\
c &=& d
See the amsmath documentation (texdoc amsldoc in a terminal) for more information.
chrishans
Posts: 3
Joined: Sun May 15, 2011 4:24 am

I'm having trouble with aligning a really long equation

Post by chrishans »

I see, thank you. I'm still pretty new to LaTeX. I guess as a follow-up question (not that it is necessary any more, since it looks fine without it) I would like to ask how do I keep the exceptionally long lines from going to the left of the equal sign?

That is, after removing the superfluous ampersands, the pdf now looks like this:
Image

And just for posterity, here is the code:

Code: Select all

\begin{align*}
w &= f(z,w) \\
&= z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3 \\
&= z^3 + (a_1z + a_2z^2)f(z,w) + (a_3 + a_4z)f(z,w)^2 + a_6f(z,w)^3 \\
&= z^3 + (a_1z + a_2z^2)(z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3) + \\
& (a_3 + a_4z)(z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3)^2 + a_6(z^3 + \\
& (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3)^3 \\
&= \ldots \\
&= z^3 + a_1z^4 + (a_1^2 + a_2)z^5 + (a_1^3 + 2a_1a_2 + a_3)z^6 + \\
& (a_1^4 + 3a_1^2a_2 + 3a_1a_3 + a_2^2 + a_4)z^7 + \cdots \\
&= z^3(1 + A_1z + A_2z^2 + \cdots) 
\end{align*}
Is it possible to get all of the multiline equations to stay to the right of the equal sign?
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

I'm having trouble with aligning a really long equation

Post by torbjorn t. »

There are probably better ways:

Code: Select all

\begin{align*}
w &= f(z,w) \\
&= z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3 \\
&= z^3 + (a_1z + a_2z^2)f(z,w) + (a_3 + a_4z)f(z,w)^2 + a_6f(z,w)^3 \\
&= z^3 + (a_1z + a_2z^2)(z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3)
+ \\
&\hspace{1.6em} (a_3 + a_4z)(z^3 + (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 +
a_6w^3)^2 + a_6(z^3 + \\
&\hspace{1.6em} (a_1z + a_2z^2)w + (a_3 + a_4z)w^2 + a_6w^3)^3 \\
&= \ldots \\
&= z^3 + a_1z^4 + (a_1^2 + a_2)z^5 + (a_1^3 + 2a_1a_2 + a_3)z^6 + \\
&\hspace{1.6em} (a_1^4 + 3a_1^2a_2 + 3a_1a_3 + a_2^2 + a_4)z^7 + \cdots \\
&= z^3(1 + A_1z + A_2z^2 + \cdots) 
\end{align*}
I've just added some horizontal space at the beginning of the line.
chrishans
Posts: 3
Joined: Sun May 15, 2011 4:24 am

Re: I'm having trouble with aligning a really long equation

Post by chrishans »

You are probably right that there are better ways, but I appreciate your help nonetheless. Many thanks!
Post Reply