# How to align terms in polynomials in display math?

I'd like to align the terms of some polynomials in display math. For example, in the following:

\begin{align}
e &= x^5                   + x     \\
f &= x^5 + x^4 + x^3 + x^2 + x + 1 \\
g &=       x^4       + x^2     + 1 \\
h &= x^5       + x^3 + x^2 + x     \\
i &=       x^4 + x^3               \\
\end{align}


I'd like it to appear as the alignment is in the code. But I cannot seem to figure out where to place the ampersands to get this right. Can someone help?

• alignat is your environment. Also, there's systeme package, that may help you. – Manuel Jul 22 '16 at 21:16
• Do you need a number for each line? – egreg Jul 22 '16 at 21:17
• @egreg No, I don't. No need to number any of the equations. – rityzmon Jul 22 '16 at 21:17

Boring to type, but effective

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

\begin{document}

\begin{equation*}
\setlength{\arraycolsep}{0pt}
\renewcommand{\arraystretch}{1.2} % like cases
\begin{array}{
r
>{{}}c<{{}} % equals
r % degree 5
>{{}}c<{{}} % plus
r % degree 4
>{{}}c<{{}} % plus
r % degree 3
>{{}}c<{{}} % plus
r % degree 2
>{{}}c<{{}} % plus
r % degree 1
>{{}}c<{{}} % plus
r % degree 0
}
e & = & x^5 &   &     &   &     &   &     &   &   & + & 1 \\
f & = & x^5 & + & x^4 & + & x^3 & + & x^2 & + & x & + & 1 \\
g & = &     &   & x^4 &   &     & + & x^2 &   &   & + & 1 \\
h & = & x^5 &   &     & + & x^3 & + & x^2 & + & x &   &   \\
i &=  &     &   & x^4 & + & x^3 &   &     &   &   &   &
\end{array}
\end{equation*}

\end{document}


The intercolumn space is set to zero, the spacing is obtained by adding empty atoms around the relation and operation signs.

This is one of the reasons why I use mathenv rather than amsmath: More flexible equation arrays. This example is very much like egreg's, but shorter, and with the option that omitting the * gives you numbered equations:

\documentclass{article}
\usepackage{mathenv}

\begin{document}

\begin{eqnarray*}[r*{6}{>{{}}l<{{}}r}]
e & = & x^5 &   &     &   &     &   &     &   &   & + & 1 \\
f & = & x^5 & + & x^4 & + & x^3 & + & x^2 & + & x & + & 1 \\
g & = &     &   & x^4 &   &     & + & x^2 &   &   & + & 1 \\
h & = & x^5 &   &     & + & x^3 & + & x^2 & + & x &   &   \\
i &=  &     &   & x^4 & + & x^3 &   &     &   &   &   &
\end{eqnarray*}
\end{document}


Output:

• In case of coefficients, I think the alignment should be r rather than l. – Bernard Jul 22 '16 at 22:10
• @Bernard: The l is for the signs, and in case of coeffs I'd enter them with the signs, such as &+2&. Which is why I chose l rather than egreg's c to keep signs aligned in such cases. – ccorn Jul 22 '16 at 22:12
• Also feasible: If coeffs -- except their signs -- are written with the monomials, then those get right-aligned. Might be better for numbers with variable-length integer part. – ccorn Jul 22 '16 at 22:22

amsmath's alignat can also do this:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{alignat}{7}
e & ={} & x^5 &       &     &       &     &       &     &       &   & {}+{} & 1 \\
f & ={} & x^5 & {}+{} & x^4 & {}+{} & x^3 & {}+{} & x^2 & {}+{} & x & {}+{} & 1 \\
g & ={} &     &       & x^4 &       &     & {}+{} & x^2 &       &   & {}+{} & 1 \\
h & ={} & x^5 &       &     & {}+{} & x^3 & {}+{} & x^2 & {}+{} & x &       &   \\
i & ={} &     &       & x^4 & {}+{} & x^3 &       &     &       &   &       &
\end{alignat}

\end{document}