Math & ScienceSystem of equations

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
tonyjnel
Posts: 11
Joined: Thu May 12, 2011 3:51 pm

System of equations

Post by tonyjnel »

Thanks Juanjo for explaining what all those ampersands are for. I couldn't find a good explanation but that makes me a lot more comfortable using it. Also thanks for catching that superfluous +.

Can you explain why "+{}" produces the correct spacing while "{}+{}" does not? It seems to me that they produce the same result.

Code: Select all

\begin{alignat*}{3}
5x_1&{}+{}&3x_2&{}+{}&x_3&= 0\\
x_1&&&{}-{}&4x_3&= 0\\
   &{}-{}&9x_2&{}+{}&2x_3&= 0
\end{alignat*}
\begin{alignat*}{3}
5x_1&+{}&3x_2&+{}&x_3&= 0\\
x_1&&&-{}&4x_3&= 0\\
   &-{}&9x_2&{}+{}&2x_3&= 0
\end{alignat*}
Is it just that the {} before the + is unnecessary?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

System of equations

Post by Juanjo »

It is a bit tricky to align formulas at binary operators (or relations). Assume first that the alignment point comes before the operator. As a rule of thumb, you can safely write an ampersand directly before the operator: since LaTeX finds some material right after the operator, LaTeX correctly treats the operator as a binary one and leaves the corresponding space. This is the case of the equal signs in your system of equations.

Now assume that the alignment point comes after the operator. If LaTeX finds an ampersand right after the operator, LaTeX doesn't know if it is really a binary operator or just a character. So, one has to add «something», like a pair of braces. Of course, this pair is superfluous before the operator. For example, suppose that, for some reason, you also want the equal signs aligned at their right. You should write something like this:

Code: Select all

\begin{alignat*}{3}
  5x_1 &+{}& 3x_2 &+{}&  x_3 ={}& 0 \\
   x_1 &   &      &-{}& 4x_3 ={}& 0 \\
       &-{}& 9x_2 &+{}& 2x_3 ={}& 0
\end{alignat*}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Post Reply