Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In LaTeX align, I'm struggling to align polynomial terms and assume there's an easy way to do it. But I cannot find the simple/elegant solution. What I would like is something like this:

x1 + x2 ______ = 3

x1_______+ x3 = 4

etc with more lines and variables (the underlines are just for space/alignment). Any help appreciated.

UPDATE Here is the solution I found:

\begin{alignat*}{7}
8x_1 &{}+ 4x_2 &{}+ y_1   &        &        &        &= 160     \\
4x_1 &{}+ 6x_2 &          &{}+ y_2 &        &        &= 120     \\
x_1  &         &          &        &{}+ y_3 &        &= 34      \\
\phantom{{}+{}x_1}&{}+x_2&&        &        &{}+ y_4 &= 14      \\
x_1\geq0&x_2\geq0&y_1\geq0&y_2\geq0&y_3\geq0& y_4\geq0
\end{alignat*}

with result:

linear system

share|improve this question
    
You should not include solutions in your question but post them as answers to your own question. –  N.N. Sep 27 '11 at 15:19
    
@N.N. Thx for the info. This is my first post so I didn't know of the rule/practice. –  Eric Sep 27 '11 at 17:59
    
If you found one of the answers appropriate, don't forget to accept it- this helps people reading your post in the future. –  cmhughes Sep 27 '11 at 18:28
1  
@Eric: Your solution here is not properly spaced. Look at the equal signs and your last line. See the details in (cmhughes answer)[tex.stackexchange.com/questions/29612/aligning-polynomial-terms/…. –  mforbes Sep 28 '11 at 3:13
add comment

7 Answers

You could also use the alignat* environment from the amsmath package- see page 5 of the documentation for details and other examples.

Note that without the {} you don't get correct spacing after the + sign.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat*}{4} % 4 is the number of equation columns
  x_1&+x_2&     & =3\\
  x_1&    &{}+x_3 &= 4
\end{alignat*}
\end{document}

enter image description here

EDIT Following mforbes' comment, and a few extra test cases, it is probably more robust to make extra columns for the + and =. For example, say that you wanted to put some coefficients in front of some of the terms.

\begin{alignat*}{4}
     x_1 &{}+{}&x_2    &      &     &{}={}&3\\
     x_1 &     &       &{}+{} & x_3 &{}={}&4
\end{alignat*}

As mforbes pointed out, the amsmath documentation says 'count the maximum number of &s in any row, add 1 and divide by 2'; I've found that if you get a fraction, then you should round up, hence {4} and not {3}.

share|improve this answer
    
I think to get the correct spacing you want to enclose the operators + and = in ampersands: &x_1 &+& x_2 & & &=& 3 \\ , &x_1 & & &+& x_3 &=& 4 \\ , and &x_1 &+& x_2 &+& x_3 &=& 5. (Also, there should be 7 & per line: "This environment takes one argument, the number of “equation columns”: count the maximum number of &s in any row, add 1 and divide by 2.") –  mforbes Sep 27 '11 at 4:43
    
@mforbes: thanks for the feedback. I tried putting the + and = in ampersands- to get the correct spacing I then need to introduce many more {}. I read that line from the documentation- I understood it to apply when horizontal space had been added, as in the example immediately before section 3.7 in which there are only 2 equation columns, but 4 actual columns. –  cmhughes Sep 27 '11 at 4:56
    
Hmm. I see what you mean about the spaces. Without the {}, the operators are not seen as binary, and are not spaced appropriately (see for example this discussion). I still think that your solution has only 2 equation columns though. –  mforbes Sep 27 '11 at 6:16
    
@mforbes: Thanks again. I think your method is more robust- I did a few more test cases, and yours is much better. I've made the edit. –  cmhughes Sep 27 '11 at 15:14
1  
My understanding is that the odd &s provide the alignment spots and the even ones provide the breaks (as with the align environment). So you are aligning with the back end of the x_1, x_2, etc. and the last & before the numbers is redundant: there are really only 3 equation columns here, but alignat does not complain if the number is too large. If you want to align the front end of the x_i then you need one more ampersand. –  mforbes Sep 28 '11 at 3:12
add comment

The systeme package easily enables such alignments:

\documentclass{minimal}
\usepackage{systeme}
\begin{document}
\sysdelim..
\systeme{x_1+x_3=6,x_2-x_3=7,x_1-x_2=4}
\end{document}
share|improve this answer
1  
It just misses an English doc :'( –  ℝaphink Sep 29 '11 at 6:02
2  
Like so many other packages which do not provide a french doc. Why the default behaviour would be to provide an english doc? –  unbonpetit Sep 29 '11 at 10:55
3  
Why are we speaking English now? –  ℝaphink Sep 29 '11 at 12:11
    
I speak English to make an answer as you focus on something that is missing in my package. It is always easy to find something missing in the work of others.I noted that you answer a question with a question, thus avoiding to answer ... –  unbonpetit Sep 29 '11 at 12:54
    
And the package is very nice indeed, it would be useful to provide an English doc since most people don't understand French. I answered the question with another question because I happen to be French. Don't take it personally, not having an English doc doesn't make the package bad, it might just prevent some users from using it (just like quite a few people won't use advanced features in Koma because it's only documented in German). –  ℝaphink Sep 29 '11 at 13:19
show 2 more comments

A fairly intuitive way would be to use \phantom to hide away components of the equations that you don't want to typeset, but have the space left for it. Additionally, since \phantom causes some spacing issues if used untouched, you need to (re-)specify the binary relation of + using \mathrel or a combination of atom-related use of {}.

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{align*}
  x_1+x_2\phantom{{}+{}x_3} &= 3 \\
  x_1\phantom{{}+{}x_2}{}+x_3 &= 4 \\
  x_1+x_2+x_3 &= 5
\end{align*}
\end{document}

enter image description here

Alternatively, one can also typeset this using an array environment. However, in terms of elegance, I don't think this ranks very high. It reduces the code readability somewhat.

share|improve this answer
    
Thx Werner. I will try that in my document. ciao –  Eric Sep 27 '11 at 4:13
    
@Eric: I've corrected a slight misalignment in my spacing (see the updated answer). \mathrel works, but I found it easier using a combination of {} around the binary relations + that accompany the use of \phantom. –  Werner Sep 27 '11 at 4:25
add comment
up vote 4 down vote accepted

Ok, here is my final solution, thanks to everyone's suggestions and guidance. Everything is now clean and well aligned :

\begin{alignat*}
8x_1  &{}+{}&    &4x_2  &{}+{}& y_1                     &&&&&& &{}={}&160\\
4x_1  &{}+{}&    &6x_2  &&       &{}+{}& y_2              &&&& &{}={}&120\\
x_1   &&         &      &&       &&       &{}+{}& y_3       && &{}={}& 34\\
\phantom{{}+{}x_1}
      &&         &x_2   &&       &&          &&    &{}+{}& y_4 &{}={}&14
\end{alignat*}

I've split the inequalities line from the alignat because it was messing with the columns width.

I learned:

  • surround the operators in ${}+{}$
  • use the exact same number of & on each line
  • use \phantom{{}+{}x_1} for missing terms at the head of the polynomial
  • don't put anything else (text) in the structure. That will mess thing up
  • there are many many ways of doing the same thing and I have a lot to learn

Here's the output:

Output

share|improve this answer
    
This looks good. One suggestion for this specific example: Replace the last &x_2 with &\phantom{6}x_2 . –  Peter Grill Sep 29 '11 at 3:57
    
@PeterGrill Thx for the suggestion. I will try that in my document. I really have to go and read the documentation to get a better understanding of all the options. –  Eric Sep 29 '11 at 15:27
    
@PeterGrill Unfortunately that did not work. The line's alignment is broken. Blindly tried to change the 6 to an 8 to no good. –  Eric Sep 29 '11 at 15:43
    
Huh?? Perhaps you have more than what you show here as yor final solution. Replacing the last as && &\phantom{6}x_2 && && && &{}+{}& y_4 &{}={}&14 in the posted solution works for me. This will align the x_2 into one column. –  Peter Grill Sep 29 '11 at 15:47
add comment

This is probably not the elegant solution you look for, but the following code works for me:

\usepackage{mathtools}

...

\[\begin{matrix*}[r]
        x1 & +x2 &  & = & 3 \\
        x1 &  & +x3 & = & 4 \\
         & -x2 & x3 & = & 5 \\
\end{matrix*}\]
share|improve this answer
    
Thx Mateus. I will try this solution. Best! –  Eric Sep 27 '11 at 4:10
add comment

This requires array, and has the advantage that spacing is taken care of automatically; the disadvantage is the number of & symbols.

\usepackage{array}
\newenvironment{polyalign}[1][9]
  {\array{c*{#1}{@{}>{{}}c<{{}}@{}r@{}}}}
  {\endarray}

\[
\begin{polyalign}
x_1 & + &  x_2 &   &     & = & 3 \\
x_1 &   &      & + & x_3 & = & 4 \\
    &   & 2x_2 & + & x_3 & = & 5
\end{polyalign}
\]

The optional argument to the environment (default is 9) is intended for helping when very big systems are needed: it can be seen as the maximum number of monomials in the left hand side.

share|improve this answer
    
I tried this and got errors. With the \usepackage{array}[1][9] line, I get an error: Missing \begin{document}. When I remove the [1][9] parameters, I get another error: "Illegal parameter number in definition of \polyalign". Any idea? –  Eric Sep 27 '11 at 13:02
    
@Eric, egreg: I also am unable to get this to work and experience the same error messages. –  Peter Grill Sep 29 '11 at 4:01
    
@PeterGrill I will add a comment if/when I get this to work. It really seems to be a good solution but I don't have time to troubleshoot right now, I have to finish my paper for tomorrow. –  Eric Sep 29 '11 at 15:31
    
There was a copying error. Sorry. –  egreg Sep 29 '11 at 15:33
add comment

I am so going to get flak for this. Oh well :-D

\documentclass{minimal}
\begin{document}
\[
  \vcenter{\halign{&${}#$\hfil\cr
    8x_1 & +4x_2 &    +y_1 &&&& = 160 \cr
    4x_1 & +6x_2 &&   +y_2 &&&  = 120 \cr
    x_1  &       &&&  +y_3 &&   = 34 \cr
         & +x_2  &&&& +y_4 &    = 14 \cr }}
  \qquad
  x_1 \ge 0x_2 \ge 0y_1 \ge 0y_2 \ge 0y_3 \ge 0y_4 \ge 0
\]
\end{document}
share|improve this answer
    
Thx for the suggestion. I tried something similar (not using vcenter{...}) and my only pbm was that if you remove the + sign in front of x_2 in the 4th line, the alignment is not nice. –  Eric Sep 27 '11 at 17:55
    
Oh, right! I had a feeling I was missing something. A \hphantom{{}+{}} infront of the x_2 sets it in the same align, but there's so many more details missing that, if I could upvote the \usepackage{systeme}-answer again, I would. –  morbusg Sep 28 '11 at 3:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.