0

I'm crafting a custom command for a table that has optional lines but needs to be formatted consistently across the document (there are many instances).

The problem I'm running into is that when the table doesn't reach the maximum number of columns, the multi-column commands seem to get ignored and the table compacts to a smaller width.

Here is a demonstration, the second table takes up the entire line width (correct), but the first one, despite only missing one line compacts to a much shorter width.

\documentclass{article}
\usepackage{multirow}
\begin{document}

\begin{tabular}{|m{\dimexpr.22\textwidth-2\tabcolsep}|m{\dimexpr.30\textwidth-2\tabcolsep}|m{\dimexpr.45\textwidth-2\tabcolsep}|}
\hline
\rowcolor{black!80} \multicolumn{3}{l}{\textcolor{white}{label}} \\
\hline
line1 & \multicolumn{2}{l|}{content} \\
\hline
\end{tabular}

\begin{tabular}{|m{\dimexpr.22\textwidth-2\tabcolsep}|m{\dimexpr.30\textwidth-2\tabcolsep}|m{\dimexpr.45\textwidth-2\tabcolsep}|}
\hline
\rowcolor{black!80} \multicolumn{3}{l}{\textcolor{white}{label}} \\
\hline
line1 & \multicolumn{2}{l|}{content} \\
\hline
line2 & content1 & content2 \\
\hline
\end{tabular}


\end{document}
1
  • The issue is that you're using l in your \multicolumn, you could instead use \multicolumn{2}{m{\dimexpr.75\textwidth-2\tabcolsep}|}{content}. Also your First \multicolumn misses the |, and could also use \multicolumn{3}{m{.97\textwidth-\tabcolsep}|} instead. Also note that as soon as you load the array package you'll also have to consider the \arrayrulewidth for each vertical rule to get the full width of your table.
    – Skillmon
    Jun 7, 2022 at 11:16

1 Answer 1

1

Maybe {NiceTabular} of nicematrix is a solution.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\noindent
\begin{NiceTabular}{X[22]X[30]X[48]}[hvlines]
\Block[l,fill=black!80]{1-*}{\color{white}\sffamily label} \\
line1 & \Block[l]{1-2}{content} & \\
\end{NiceTabular}

\bigskip

\noindent
\begin{NiceTabular}{X[22]X[30]X[48]}[hvlines]
\Block[l,fill=black!80]{1-*}{\color{white}\sffamily label} \\
line1 & \Block[l]{1-2}{content} & \\
line2 & content1 & content2 \\
\end{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .