複数列を表の残りの部分と揃える

複数列を表の残りの部分と揃える

複数列の要素がありますが、これをテーブルの残りの部分とどのように揃えればよいかわかりません。

コード:

\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 つの列の幅を事前に定義し、最後の行の幅を、前の 2 つの幅の合計に表の列間隔の 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}

ここに画像の説明を入力してください

関連情報