
array
列の 1 つが多項式である があり、配列の各行の多項式の項を揃えたいと考えています。私のハックは、あらゆる\phantom
場所で s を使用することですが、これを自動的に行う方法はありますか? たとえば、配列のこの部分を何らかのalign
環境のように動作させることができればどうでしょうか?
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}
答え1
次のような列を追加することもできます
\[ \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} \]
ここで、は\mc
後続の符号なしの位置合わせを処理するために定義されます+
。
コード:
\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}