
我有一個array
其中一列是多項式,我想為數組的每一行對齊多項式的項。我的技巧是\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} \]
where\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}