3

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}

What I have vs. what I want

  • 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
2

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}

enter image description here

  • 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
  • 1
    @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
  • 1
    @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
  • 1
    @CoreyHarris Define \newcommand{\mymc}[1]{\multicolumn{1}{r@{{}-{}}}{#1}} and use it in the previous column like 11 & \mc{x} & \mymc{} & 10x^3\\ . However, if the previous column is not empty, you have to define yet another macro like \newcommand{\myplusmc}[1]{\multicolumn{1}{r@{{}+{}}}{#1}} and use 11 & \myplusmc{x} & \mymc{5x^2} & 10x^3\\ . Well, life is getting complicated. – user11232 Apr 22 '15 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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