Alinhe multicolunas com o resto de uma tabela

Alinhe multicolunas com o resto de uma tabela

Eu tenho um elemento de várias colunas que não sei como alinhar com o restante da tabela.

O código:

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

O que produz:

Quero que a parte inferior fique alinhada com a linha vermelha (das outras colunas). Qual é a abordagem para conseguir isso?

Responder1

Experimente este código. A solução define a largura das duas colunas antecipadamente e depois calcula a largura da última linha como a soma das duas larguras anteriores mais 2 vezes o espaçamento das colunas tabulares. Adicionei o calcpacote para tornar o cálculo explícito.

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

saída

Responder2

O novo pacote LaTeX3tabularrayfornece uma opção hspan=minimalpara calcular larguras de vão a partir de larguras de colunas:

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

insira a descrição da imagem aqui

informação relacionada