# Align-like environment in a column of array

I have an array in which one of the columns is a polynomial, and I would like to align the terms of the polynomial for each row of the array. My hack is to use \phantoms all over the place, but is there a way to do this automatically? For instance, if I could make this part of the array act like some sort of align environment?

Here is a MWE:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

What I have:
$\begin{array}{|r|c|} \hline S & P(x) \\ \hline 9 & x + x^2 + x^3 \\ 10 & 3x + x^2 + x^3 \\ 11 & x + 10x^3\\ \hline \end{array}$

What I want:
$\begin{array}{|r|c|} \hline S & P(x) \\ \hline 9 & \phantom{3}x + x^2 + \phantom{10}x^3 \\ 10 & 3x + x^2 + \phantom{10}x^3 \\ 11 & \phantom{3}x \phantom{{} + x^2 } + 10x^3\\ \hline \end{array}$

\end{document}


• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Symbol 1 Apr 20 '15 at 3:07

You can use more columns like

$\begin{array}{|r|r@{{}+{}}c@{{}+{}}r|} \hline S & \multicolumn{3}{|c|}{P(x)} \\ \hline 9 & x & x^2 &x^3 \\ 10 & 3x & x^2 & x^3 \\ 11 & \mc{x} & & 10x^3\\ \hline \end{array}$


where \mc is defined to take care of alignment without the succeding + sign.

Code:

\documentclass{article}
\usepackage{amsmath}
\newlength{\mylen}
\settowidth{\mylen}{${}+{}$}
\newcommand{\mc}[1]{\multicolumn{1}{r@{\hspace{\mylen}}}{#1}}
\begin{document}

What I have:
$\begin{array}{|r|c|} \hline S & P(x) \\ \hline 9 & x + x^2 + x^3 \\ 10 & 3x + x^2 + x^3 \\ 11 & x + 10x^3\\ \hline \end{array}$

What I want:
$\begin{array}{|r|r@{{}+{}}c@{{}+{}}r|} \hline S & \multicolumn{3}{|c|}{P(x)} \\ \hline 9 & x & x^2 &x^3 \\ 10 & 3x & x^2 & x^3 \\ 11 & \mc{x} & & 10x^3\\ \hline \end{array}$

\end{document}


• this is so simple! It is almost exactly what I had in mind, except that there are now too many +-signs. In my actual document this would cause several unnecessary pluses to be added back-to-back, as in x + + + + x^5. – Corey Harris Apr 20 '15 at 2:58
• @CoreyHarris See the update please. – user11232 Apr 20 '15 at 5:45
• @CoreyHarris Glad it worked. You may do \newcommand{\mc}[2][1]{\multicolumn{#1}{r@{\hspace{\mylen}}}{#2}} so that \mc{x} can also be used. – user11232 Apr 20 '15 at 15:08
• @CoreyHarris You can define another \mc like command (say \mymc ) where instead of @{\hspace{\mylen}}} (before r ofcourse) you add @{{}-{}} and use it like \mymc{10x^3} – user11232 Apr 22 '15 at 15:41