
Ich habe ein mehrspaltiges Element und bin mir nicht sicher, wie ich es mit dem Rest der Tabelle ausrichten soll.
Der Code:
\documentclass{article}
\begin{document}
\begin{tabular}{l|p{0.5\linewidth}}
Item 1 & This part contains multiple lines and I want the multicolumn on the bottom to align with it. \\
Item 2 & Something else here. \\
\hline
\multicolumn{2}{p{0.6\linewidth}}{I want this text to be aligned with the rest of the columns.}
\end{tabular}
\end{document}
Ich möchte, dass der untere Teil mit der roten Linie (von den anderen Spalten) übereinstimmt. Wie kann ich das erreichen?
Antwort1
Probieren Sie diesen Code aus. Die Lösung definiert die Breite der beiden Spalten im Voraus und berechnet dann die Breite der letzten Zeile als Summe der beiden vorherigen Breiten plus 2 mal dem Abstand der Tabellenspalten. Ich habe das calc
Paket hinzugefügt, um die Berechnung explizit zu machen.
\documentclass{article}
\usepackage{calc}
\begin{document}
\begin{tabular}{l|p{0.5\linewidth}}
Item 1 & This part contains multiple lines and I want the multicolumn on the bottom to align with it. \\
Item 2 & Something else here. \\
\hline
\multicolumn{2}{p{0.6\linewidth}}{I want this text to be aligned with the rest of the columns.}
\end{tabular}
\vspace{3\baselineskip}
\newlength{\colwidthi}
\settowidth{\colwidthi}{Item 1}
\newlength{\colwidthii}
\setlength{\colwidthii}{0.5\linewidth}
\newlength{\colwidthiii}
\setlength{\colwidthiii}{\colwidthi+\colwidthii+ 2\tabcolsep}
\begin{tabular}{p{\colwidthi}|p{\colwidthii}}
Item 1 &This part contains multiple lines and I want the multicolumn on the bottom to align with it. \\
Item 2 & Something else here. \\
\hline
\multicolumn{2}{p{\colwidthiii}}{I want this text to be aligned with the rest of the columns.} \\
\end{tabular}%
\end{document}
Antwort2
Das neue LaTeX3-Pakettabularray
bietet eine Option hspan=minimal
zum Berechnen der Spannweiten aus den Spaltenbreiten:
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{colspec={l|p{0.5\linewidth}},hspan=minimal}
Item 1 & This part contains multiple lines and I want the multicolumn on the bottom to align with it. \\
Item 2 & Something else here. \\
\hline
\SetCell[c=2]{l} I want this text to be aligned with the rest oofff the columns. & \\
\end{tblr}
\end{document}