將多列與表格的其餘部分對齊

將多列與表格的其餘部分對齊

我有一個多列元素,我不確定如何與表格的其餘部分對齊。

代碼:

\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}

在此輸入影像描述

相關內容