[Tex/LaTex] Align to center

alignamsmathhorizontal alignment

I want to align some equations to the equal signs, so I wrote this:

\begin{align}
            -3z &=-9\\ 
              z &= 3\\
        -3y-3*3 &= 3\\ 
              y &=-4\\
  2x+8*(-4)+4*3 &= 2\\ 
              x &=11
\end{align}

However, because one equation is much longer than the others, all the equations appear somewhat at the right of the page:

enter image description here

How can I make the equal signs appear in the middle while still aligning all the equations to them?

Best Answer

A simple approach would be to duplicate the contents of the widest entry (either left or right) on the opposite side, accommodating for some overlap:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\lipsum[2]
\begin{align*}
            -3z &= -9 \\
              z &=  3 \\
        -3y-3*3 &=  3 \\
              y &= -4 \\
  2x+8*(-4)+4*3 &=  2 \\
              x &= 11
\end{align*}
\lipsum[2]
\begin{align*}
            -3z &= -9 \\
              z &=  3 \\
        -3y-3*3 &=  3 \\
              y &= -4 \\
  2x+8*(-4)+4*3 &=  \rlap{2}\phantom{2x+8*(-4)+4*3} \\
              x &= 11
\end{align*}
\lipsum[2]
\end{document}

Alternatively, if you're not concerned about numbering the equations, then you can use the following construction:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{array}% http://ctan.org/pkg/array
\newsavebox{\mymathbox}
\newcolumntype{R}{>{\begin{lrbox}{\mymathbox}$}r<{$\end{lrbox}\llap{\usebox{\mymathbox}}}}%
\newcolumntype{L}{>{\begin{lrbox}{\mymathbox}$}l<{$\end{lrbox}\rlap{\usebox{\mymathbox}}}}%
\begin{document}
\lipsum[2]
\begin{align*}
            -3z &= -9 \\
              z &=  3 \\
        -3y-3*3 &=  3 \\
              y &= -4 \\
  2x+8*(-4)+4*3 &=  2 \\
              x &= 11
\end{align*}
\lipsum[2]
\[
  \renewcommand{\arraystretch}{1.2}
  \begin{array}{R@{{}={}}L}
            -3z & -9 \\
              z &  3 \\
        -3y-3*3 &  3 \\
              y & -4 \\
  2x+8*(-4)+4*3 &  2 \\
              x & 11
\end{array} \]
\lipsum[2]
\end{document}

This constructs an align* in the form of an array and places the contents on either side of the equal sign in a zero-width box. Consequently, then equations seem centred based on =.

Note that \rlap and \llap uses \makebox, which inserts its contents in text mode. Hence the switch back to math mode via $ in the automated construction of the L and R column types.

Related Question