Anda di halaman 1dari 7

math mode - Aligning polynomial terms - TeX - LaTeX Stack Exchange 11/28/17, 22(06

Aligning polynomial terms

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:

{math-mode} {horizontal-alignment} {amsmath} {align}

edited Mar 20 at 23:01 asked Sep 27 '11 at 3:28


David Carlisle Eric
420k 33 1008 151 1 8
1706

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

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

https://tex.stackexchange.com/questions/29612/aligning-polynomial-terms Page 1 of 7
math mode - Aligning polynomial terms - TeX - LaTeX Stack Exchange 11/28/17, 22(06

x_1& &{}+x_3 &= 4


\end{alignat*}
\end{document}

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

edited Mar 20 at 23:02 answered Sep 27 '11 at 4:37


David Carlisle cmhughes
420k 33 1008 72.9k 12 181 290
1706

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

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

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 ${}+{}$

https://tex.stackexchange.com/questions/29612/aligning-polynomial-terms Page 2 of 7
math mode - Aligning polynomial terms - TeX - LaTeX Stack Exchange 11/28/17, 22(06

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:

edited Mar 20 at 23:02 answered Sep 29 '11 at 3:19


David Carlisle Eric
420k 33 1008 151 1 8
1706

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:

\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}

answered Sep 27 '11 at 9:10


unbonpetit
5,045 1 10 21

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

https://tex.stackexchange.com/questions/29612/aligning-polynomial-terms Page 3 of 7
math mode - Aligning polynomial terms - TeX - LaTeX Stack Exchange 11/28/17, 22(06

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.

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

\documentclass{article}
\usepackage{amsmath,systeme}

\begin{document}

\begin{equation*}
\begin{cases}
\sysdelim..
\systeme{
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
\end{cases}
\end{equation*}

\end{document}

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

\documentclass{article}
\usepackage{amsmath,systeme}

https://tex.stackexchange.com/questions/29612/aligning-polynomial-terms Page 4 of 7
math mode - Aligning polynomial terms - TeX - LaTeX Stack Exchange 11/28/17, 22(06

\begin{document}

\begin{equation*}
\syssubstitute{{a_1}{x_R}{a_2}{x_C}{a_3}{x_M}{a_4}{x_T}{a_5}{x_L}}
\systeme{
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
}
\end{equation*}

\end{document}

edited Jun 15 '15 at 20:00 answered Sep 27 '11 at 8:58


egreg
610k 73 1620
2813

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

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}

https://tex.stackexchange.com/questions/29612/aligning-polynomial-terms Page 5 of 7
math mode - Aligning polynomial terms - TeX - LaTeX Stack Exchange 11/28/17, 22(06

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.

edited Mar 20 at 23:04 answered Sep 27 '11 at 3:45


David Carlisle Werner
420k 33 1008 387k 49 802
1706 1377

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 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*}\]

edited Jul 18 at 7:04 answered Sep 27 '11 at 3:45


David Carlisle Mateus Arajo
420k 33 1008 4,603 5 29 37
1706

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

\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}

answered Sep 27 '11 at 15:19


morbusg
19.1k 2 56 133

Thx for the suggestion. I tried something similar (not using vcenter{...}) and my only pbm was that if you

https://tex.stackexchange.com/questions/29612/aligning-polynomial-terms Page 6 of 7
math mode - Aligning polynomial terms - TeX - LaTeX Stack Exchange 11/28/17, 22(06

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

https://tex.stackexchange.com/questions/29612/aligning-polynomial-terms Page 7 of 7

Anda mungkin juga menyukai