TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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

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
@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

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.

\begin{alignat*}{4} % 4 is the number of equation columns
  x_1&+x_2&     & =3\\
  x_1&    &{}+x_3 &= 4

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.

     x_1 &{}+{}&x_2    &      &     &{}={}&3\\
     x_1 &     &       &{}+{} & x_3 &{}={}&4

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
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
up vote 7 down vote accepted

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

8x_1  &{}+{}&    &4x_2  &{}+{}& y_1                     &&&&&& &{}={}&160\\
4x_1  &{}+{}&    &6x_2  &&       &{}+{}& y_2              &&&& &{}={}&120\\
x_1   &&         &      &&       &&       &{}+{}& y_3       && &{}={}& 34\\
      &&         &x_2   &&       &&          &&    &{}+{}& y_4 &{}={}&14

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:


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

The systeme package easily enables such alignments:

share|improve this answer
It just misses an English doc :'( – ℝaphink Sep 29 '11 at 6:02
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
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

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 {}.

\usepackage{amsmath}% http://ctan.org/pkg/amsmath
  x_1+x_2\phantom{{}+{}x_3} &= 3 \\
  x_1\phantom{{}+{}x_2}{}+x_3 &= 4 \\
  x_1+x_2+x_3 &= 5

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

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


x_1 & + &  x_2 &   &     & = & 3 \\
x_1 &   &      & + & x_3 & = & 4 \\
    &   & 2x_2 & + & x_3 & = & 5

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.

A different solution uses the systeme package, together with some trickery for getting the series of inequalities as the last line:



    8x_1 + 4x_2 + y_1                   = 160,
    4x_1 + 6x_2       + y_2             = 120,
     x_1                    + y_3       = 34,
            x_2                   + y_4 = 14
x_1\geq0,\; x_2\geq0,\; y_1\geq0,\; y_2\geq0,\; y_3\geq0,\; y_4\geq0


enter image description here

The systeme package is quite flexible and allows also for “named” subscripts instead of numeric ones, albeit with substitutions.



  200a_1 + 150a_2 + 200a_3 + 100a_4 + 100a_5 <= 250000,
  150a_1 + 100a_2 + 100a_3 + 100a_4          <= 5000000,
    4a_1 +   4a_2 +   4a_3 +   4a_4          <= 20000,
   15a_1 +  10a_2 +  10a_3 +  15a_4 +   5a_5 <= 129600,
    2a_1 +   1a_2 + 1.5a_3 + 1.5a_4 +   1a_5 <= 129600


enter image description here

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

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



        x1 & +x2 &  & = & 3 \\
        x1 &  & +x3 & = & 4 \\
         & -x2 & x3 & = & 5 \\
share|improve this answer
Thx Mateus. I will try this solution. Best! – Eric Sep 27 '11 at 4:10

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

    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 }}
  x_1 \ge 0x_2 \ge 0y_1 \ge 0y_2 \ge 0y_3 \ge 0y_4 \ge 0
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

Your Answer


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.