4

I have a multicolumn element which I am not sure how to align with the rest of the table.

The code:

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

Which produces:

I want the bottom part to align with the red line (from the other columns). What is the approach to achieve this?

2 Answers2

1

Try this code. The solution defines the width of the two columns in advance and then calculates the width of the last row as the sum of the previous two widths plus 2 times the tabular column spacing. I added the calc package to make the calculation explicit.

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

output

Simon Dispa
  • 39,141
0

The new LaTeX3 package tabularray provides an option hspan=minimal for calculating span widths from column widths:

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

enter image description here

L.J.R.
  • 10,932