Thursday, August 6, 2009

Aligning Equations in Latex

Let's say that you have an equation like this:

And want to align it in a pretty way, say:

or:

as opposite to the sloppy:

Then, you can use the environment split within the package amsmath, just as in the example in the box below. By the way, the sloppy way is the last bit of code in the example.


\documentclass{article} \usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{split}
x = a + b + c
\end{split}
\end{equation}

\begin{equation}
\begin{split}
x = {} & a \\
+ & b \\
+ & c \\
\end{split}
\end{equation}

\begin{equation}
\begin{split}
x = {} & a + b \\
+ & c \\
\end{split}
\end{equation}

\begin{equation}
\begin{array}{l}
x = a+ b \\
+ c \\
\end{array}
\end{equation}
\end{document}


5 comments:

  1. It's just a matter of personal preference or personal style, but I would probably use this code:

    \begin{eqnarray}
    x &=& a + b \\ \nonumber
    & & + c
    \end{eqnarray}

    or:

    \begin{eqnarray*}
    x &=& a + b \\
    & & + c
    \end{eqnarray*}

    ReplyDelete
  2. You may then find useful the article about eqnarray in the Practex journal:
    http://www.tug.org/pracjourn/2006-4/madsen/

    ReplyDelete
  3. My question then Nelson is the following... things are not really ligned up nicely in your version. Is this something that is fixable? for example, the spacing is not the same on the first line and the next lines.

    ReplyDelete
  4. Here is something I found if you want to make it look nice. (Courtesy of: ftp://ftp.ams.org/pub/tex/doc/amsmath/short-math-guide.pdf)

    \begin{equation}\label{xx}
    \begin{split}
    a& =b+c-d\\
    & \quad +e-f\\
    & =g+h\\
    & =i
    \end{split}
    \end{equation}

    ReplyDelete