Document Classesmultiple alignment in an equation

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
daviddoria
Posts: 60
Joined: Tue Sep 30, 2008 9:24 pm

multiple alignment in an equation

Post by daviddoria »

using amsmath \begin{align}, I would like to do something like this

a + 2b + 3c = 4
3a + + 7c = 5

where if there are missing terms, I can use another align character to line up the + signs, as well as the equal signs

I tried this:

Code: Select all

\begin{align*}
x_1 &+ 4x_2 &= -2 \\
x_1 &+ 2x_2 &= 6 \\
2x_1 &+ 3x_2 &= 1 
\end{align*}
But it kind of separates the equations into two columns. I didn't see an explanation of how to do this the amsldoc

can someone give me some pointers?

Thanks,
Dave

Recommended reading 2024:

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

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

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Re: multiple alignment in an equation

Post by Stefan Kottwitz »

Hi Dave,

use the alignat* environment.

Stefan
LaTeX.org admin
daviddoria
Posts: 60
Joined: Tue Sep 30, 2008 9:24 pm

multiple alignment in an equation

Post by daviddoria »

The doc says you should use (max number of & in a line + 1)/2 as the argument. I tried this:

Code: Select all

\begin{alignat*}{4}
x &+ y &+ z &= -2 \\
x &+ y &+ z &= 6 \\
x &+ &+ z &= 1 
\end{alignat*}
and

Code: Select all

\begin{alignat*}{2}
x &+ y &+ z &= -2 \\
x &+ y &+ z &= 6 \\
x &+ &+ z &= 1 
\end{alignat*}
and they seem to produce the same result. What is that argument saying exactly?

Dave
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Re: multiple alignment in an equation

Post by Stefan Kottwitz »

Hi Dave,

the argument specifies the number of columns. It should not really make a difference if you choose the number too high, but it would not compile with a value that's too low. For your example 2 columns are necessary, but you could declare more, like you've tested with 4.

Stefan
LaTeX.org admin
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

multiple alignment in an equation

Post by Juanjo »

Consider the mathematical expressions as divided in columns. As Stephan_K says, the argument of alignat is the number of columns. Inside each column fix an aligment point. Then, in every line, mark with & each aligment point and also the end of each column (except the last one). So, if you have n columns, you should have 2n-1 & signs.

For the example of your first post, you could try

Code: Select all

\begin{alignat*}{2}
x_1 &+{}& 4x_2 &= -2 \\
x_1 &+{}& 2x_2 &= 6 \\
2x_1&+{}& 3x_2 &= 1 
\end{alignat*}
Note the pairs of braces. They are generally needed if a binary operation falls before of after a column separation to get proper spacing. To see this, remove the braces and compare. I add a more complex example:

Code: Select all

\begin{alignat*}{4}
-7x_1 & & &+{}& 41x_3 &-{}& 14x_4&=9, \\
23x_1 &-{}& x_2 &+{}& 9x_3 & & &=-22, \\
x_1 &+{}& 10x_2 & & &+{}& 291x_4&=53, \\
& & 3x_2 &+{}& x_3 &+{}& 21x_4&=14.
\end{alignat*}
To obtain the same expressions, there are usually some possible alternatives on where putting the aligment points and columns. This may require some experimentation.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
daviddoria
Posts: 60
Joined: Tue Sep 30, 2008 9:24 pm

Re: multiple alignment in an equation

Post by daviddoria »

excellent, thanks everyone!
Post Reply