
У меня есть элемент из нескольких столбцов, и я не знаю, как выровнять его с остальной частью таблицы.
Код:
\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}
Я хочу, чтобы нижняя часть совпадала с красной линией (из других столбцов). Как этого добиться?
решение1
Попробуйте этот код. Решение определяет ширину двух столбцов заранее, а затем вычисляет ширину последней строки как сумму предыдущих двух значений ширины плюс 2 интервала между табличными столбцами. Я добавил пакет, calc
чтобы сделать расчет явным.
\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}
решение2
Новый пакет LaTeX3tabularray
предоставляет возможность hspan=minimal
расчета ширины пролета на основе ширины столбцов:
\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}