alignat problem

Whenever I try to use the alignat environment, it doesn't seem to work on my computer! I tried the following simple code (my file had \usepackage{amsmath}):

\begin{alignat}{3}
\min \quad x_1        & + 2x_2 & + 4x_3 &      &   & \\
\text{s.t.} \quad x_1 & + x_2  & + 3x_3 & =    & 5 & \\
2x_1                  & + x_2  & + 6x_3 & =    & 8 & \\
x_1                   &, x_2   &, x_3   & \geq & 0 & .
\end{alignat}


On compilation, I get the following error:

    ! Missing # inserted in alignment preamble.  <to be read again>
\crcr l.114   \end{alignat}


Any ideas why this might be?

-
That code works for me. Does it work for you if you just use that in an otherwise empty document? –  TH. Aug 18 '10 at 3:51

It's been a while, and you probably fixed your problem by now. But since another question involving alignat (well, my answer involves it anyway) came up, and this question scores quite high for the google query latex alignat, I wanted to make sure that there's more information here....

I'm not sure what you are trying to align. I'm assuming you want the x_i's aligned, in which case the correct solution would be

\begin{alignat}{3}
\min \quad x_1& {}+{}& 2x_2& {}+{}& 4x_3& \\
\text{s.t.} \quad x_1& {}+{}&x_2&{}+{} & 3x_3&=5\\
2x_1& {}+{}& x_2& {}+{}&6x_3& = 8\\
x_1&, &x_2&, &x_3& \geq 0 .
\end{alignat}


which produces:

As you see, you need to put alignment characters between the columns to be aligned, not only inside the columns.

-
There is a lot of missing white space in your example. You should perhaps write {}+{} instead of +? –  Jukka Suomela Oct 18 '10 at 19:45
Thanks Jukka! Fixed it. –  Yossi Farjoun Oct 18 '10 at 20:33
Explaining why the {}+{} are needed: (I think) LaTeX typesets the spaces around binary operators differently than it does around unitary ones. we would want +r to have less space between the two characters than b+r. And since each column in alignat is typeset separately, it doesn't "see" that the +'s are binary operators. –  Yossi Farjoun Oct 18 '10 at 20:35

Your code also compiles for me, and as Lev suggested constructing a minimal example will really help to narrow down where your problem lies, but I was able to generate a similar error to what you have by incorrectly using the tabular environment inside the alignat environment.

For example,

\begin{document}
\begin{alignat}{3}
\begin{tabular}{||}
test
\end{tabular}
\end{alignat}


results in the error

! Missing # inserted in alignment preamble.
\cr
l.9 \end{alignat}


Since the l.9 and l.114 just refer to line numbers in the code, the main difference is that my error includes \cr where yours says \crcr.

I can solve my alignment preamble error by using at least one correct column alignment character in the tabular environment, for example by switching to \begin{tabular}{|c|}.

-