
Ich habe ein Array array
, in dem eine der Spalten ein Polynom ist, und ich möchte die Terme des Polynoms für jede Zeile des Arrays ausrichten. Mein Trick besteht darin, \phantom
überall s zu verwenden, aber gibt es eine Möglichkeit, dies automatisch zu tun? Wenn ich beispielsweise diesen Teil des Arrays wie eine Art align
Umgebung agieren lassen könnte?
Hier ist ein 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}
Antwort1
Sie können weitere Spalten verwenden, wie
\[ \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} \]
wobei \mc
definiert ist, dass die Ausrichtung ohne das nachfolgende Vorzeichen erfolgt +
.
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}