Alinear varias columnas con el resto de una tabla.

Alinear varias columnas con el resto de una tabla.

Tengo un elemento de varias columnas que no estoy seguro de cómo alinear con el resto de la tabla.

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

Que produce:

Quiero que la parte inferior se alinee con la línea roja (de las otras columnas). ¿Cuál es el enfoque para lograr esto?

Respuesta1

Pruebe este código. La solución define el ancho de las dos columnas de antemano y luego calcula el ancho de la última fila como la suma de los dos anchos anteriores más 2 veces el espaciado de las columnas tabulares. Agregué el calcpaquete para que el cálculo sea 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}

producción

Respuesta2

El nuevo paquete LaTeX3tabularrayproporciona una opción hspan=minimalpara calcular los anchos de los tramos a partir del ancho de las columnas:

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

ingrese la descripción de la imagen aquí

información relacionada