将多列与表格的其余部分对齐

将多列与表格的其余部分对齐

我有一个多列元素,我不确定如何与表的其余部分对齐。

代码:

\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

新的 LaTeX3 软件包tabularrayhspan=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}

在此处输入图片描述

相关内容