8

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:

matrix image

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.

David Carlisle
  • 757,742
Johan
  • 81

3 Answers3

11

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.

enter image description here

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}

enter image description here

Update 2017

The new autoaligne package allows for a different solution. Unfortunately, empty slots should be denoted by + with no following space, but with some stretch we can also get a good alignment in the input: the trick is to locally set the space to an ignored character.

\documentclass{article}
\usepackage{amsmath}
\usepackage{autoaligne}

\begin{document}

\begin{equation*}
\catcode` =9 % ignore spaces
\autoaligne{%
  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{equation*}
\end{document}

enter image description here

egreg
  • 1,121,712
11

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 alignments per system, the equal sign in the middle and in-between 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}

Result

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.

David Carlisle
  • 757,742
Heiko Oberdiek
  • 271,626
6

$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
T. Verron
  • 13,552