# alignat makes 3 good alignments and 1 bad

I'm having a problem with the alignat environment. This code:

\begin{alignat*}{9}
&X_{12}&+X_{13}&       &       &       &       &       &       &        &=1 \\
&      & X_{13}&+X_{23}&       &       &-X_{35}&       &       &        &=0 \\
&X_{12}&       &-X_{23}&-X_{24}&-X_{25}&       &       &       &        &=0 \\
&      &       &       & X_{24}&       &       &-X_{45}&-X_{46}&        &=0 \\
&      &       &       &       & X_{25}&+X_{35}&+X_{45}&       &-X_{56} &=0 \\
&      &       &       &       &       &       &       & X_{46}&+X_{56} &=1 \\
\end{alignat*}


produces this matrix:

I don't understand why the X_{25} doesn't align, when X_{13}, X_{24} and X_{46} does. Can anybody help me understand this, and even better, how to fix it.

-
Welcome to TeX.sx! A tip: You can use backticks  to mark your inline code – Corentin Nov 12 '12 at 23:05
For understanding: align-type environments are for aligning equations which consist of a left and a right part, like this: left & right & next equation.... So the & signs you used have different meanings to the align environment, and your formula parts are interpreted, in turn, as left or right parts, and aligned accordingly (also note that every second operator has different spacing). – Stephan Lehmke Nov 12 '12 at 23:13
Related answer: aligning equals as well as plus signs – Qrrbrbirlbel Nov 12 '12 at 23:15

The environment alignat is intended for aligning several equation systems on a line:

abc = def   ghi = jkl
m = n       o = p


Therefore there are two aligments per system, the equal sign in the middle and inbetween two systems:

abc &= def & ghi &= jkl \\
m   &= n   &   o &= p \\


The part left from the equal sign is right justified, the part afterwards left justified. In your case you probably want right justification, thus you need two &&:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{alignat*}{9}
X_{12}&&{}+X_{13}&&         &&         &&         &&         &&         &&         &&          &=1 \\
&& X_{13}  &&{}+X_{23}&&         &&         &&{}-X_{35}&&         &&         &&          &=0 \\
X_{12}&&         &&{}-X_{23}&&{}-X_{24}&&{}-X_{25}&&         &&         &&         &&          &=0 \\
&&         &&         && X_{24}  &&         &&         &&{}-X_{45}&&{}-X_{46}&&          &=0 \\
&&         &&         &&         && X_{25}  &&{}+X_{35}&&{}+X_{45}&&         &&{}-X_{56} &=0 \\
&&         &&         &&         &&         &&         &&         && X_{46}  &&{}+X_{56} &=1 \\
\end{alignat*}

\end{document}


At some places I have also added an empty math subformula {} in front of + or - right after &. This way TeX knows that + and - should be set as binary operators (between {} and X) and the spacing is better.

-

alignat builds alternatively right and left aligned columns; your X_{25} falls in an even numbered column.

Thus this is not the best way to typeset these equations, because the spacing around the operation signs is uneven; better using an array:

\documentclass{article}
\usepackage{amsmath}
\usepackage{array} % for extended syntax in array

\begin{document}
\begin{equation*}
\begin{array}{*{18}{@{}>{{}}l<{{}}@{}}}
X_{12}&+&X_{13}& &      & &      & &      & &      & &      & &      & &      &=1 \          & &X_{13}&+&X_{23}& &      & &      &-&X_{35}& &      & &      & &      &=0 \    X_{12}& &      &-&X_{23}&-&X_{24}&-&X_{25}& &      & &      & &      & &      &=0 \          & &      & &      & &X_{24}& &      & &      &-&X_{45}&-&X_{46}& &      &=0 \          & &      & &      & &      & &X_{25}&+&X_{35}&+&X_{45}& &      &-&X_{56}&=0 \          & &      & &      & &      & &      & &      & &      & &X_{46}&+&X_{56}&=1
\end{array}
\end{equation*}
\end{document}


I build 18 columns, each left aligned; each will become ${}#{}$\hfil, where # denotes the actual cell entry. So if the entry is an operation symbol, say +, ${}+{}$ will leave the correct spacing around +. In case it's X_{12} the empty groups do nothing.

A different solution is with systeme; the setup is not much more complicated than as above, although it requires doing some substitutions; however the input is quite clearer:

\documentclass{article}
\usepackage{amsmath}
\usepackage{systeme}

\begin{document}
\begin{equation*}
\syssubstitute{%
{x_1}{X_{12}}%
{x_2}{X_{13}}%
{x_3}{X_{23}}%
{x_4}{X_{24}}%
{x_5}{X_{25}}%
{x_6}{X_{35}}%
{x_7}{X_{45}}%
{x_8}{X_{46}}%
{x_9}{X_{56}}%
}
\sysdelim..
\systeme{
x_1+x_2                            =1,
x_2+x_3        -x_6            =0,
x_1    -x_3-x_4-x_5                =0,
x_4        -x_7-x_8    =0,
x_5+x_6+x_7    -x_9=0,
x_8+x_9=1
}
\end{equation*}
\end{document}


-

$X_{25}$is left-aligned with $-X_{25}$, that's what you asked for in your environment. I suggest you add alignment tabs around each +and -symbol as well, to achieve the visual effect you want.

Please note that $X_{24}$ is correctly aligned, because that column is right-aligned. The rule in that environment is :

• first column right-aligned
• second column left-aligned
• ...
• fifth column (X_{24}) right-aligned, hence correct in your sense
• sixth column (X_{25}`) left-aligned
• etc
-