2

I'm making a table in the tabular environment, and column width is doing something strange. In the example here, the first row uses multicolumn to merge cells by groups of 2. Then the next row has 2 separate cells. I would like it to divide these evenly below the first row of headers, rather than having a minimum width for the first entries (A) and filling the rest of the space with the second entries (B).

\begin{table}
\caption{Caption.}
\centering
\begin{tabular}{c|c|c|c|c|c}
    \hline \hline
    \multicolumn{2}{c|}{Long Header 1} & \multicolumn{2}{c|}{Long Header 2} & \multicolumn{2}{c}{Long Header 3} \\
    \hline
    A & B & A & B & A & B \\
    \hline \hline
\end{tabular}
\end{table} 
1

You can use a centered version of tabularx's X column type:

enter image description here

\documentclass{article}

\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
\begin{table}
\caption{Caption.}
\centering
\begin{tabularx}{0.7\textwidth}{C|C|C|C|C|C}
    \hline \hline
    \multicolumn{2}{c|}{Long Header 1} & \multicolumn{2}{c|}{Long Header 2} & \multicolumn{2}{c}{Long Header 3} \\
    \hline
    A & B & A & B & A & B \\
    \hline \hline
\end{tabularx}
\end{table}
\end{document}

Edit:

To make it work with different headers you have to inform tabularx that you are using \multicolumn:

\documentclass{article}

\usepackage{tabularx}

\newcolumntype{C}{>{\centering\arraybackslash}X}
% Number of multicolumns      here ↓   and ↓ here (source: https://tex.stackexchange.com/a/236156/134574)
\newcolumntype{D}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}C}

\begin{document}
\begin{table}
\caption{Caption.}
\centering
\begin{tabularx}{0.85\textwidth}{C|C|C|C|C|C}
    \hline \hline
    \multicolumn{2}{D|}{This is a very, very long Header 1} & \multicolumn{2}{D|}{Long Header 2} & \multicolumn{2}{D}{Long Header 3} \\
    \hline
    A & B & A & B & A & B \\
    \hline \hline
\end{tabularx}
\end{table}
\end{document}

enter image description here

Edit 2:

I'm not proud of this one...

If you insist in one line, a more hacky version can be done using two tabularx's in the same table. You read it right, Jimmy, two!

enter image description here

\documentclass{article}

\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{D}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}C}

\begin{document}
\begin{table}
\caption{Caption.}
\centering
\begin{tabularx}{0.5\textwidth}{C|C|}
    \hline \hline
    \multicolumn{2}{D|}{This is a very, very long Header 1}\\
    \hline
    A & B\\
    \hline \hline
\end{tabularx}%
\begin{tabularx}{0.5\textwidth}{C|C|C|C}
    \hline \hline
    \multicolumn{2}{c|}{Long Header 2} & \multicolumn{2}{c}{Long Header 3} \\
    \hline
    A & B & A & B \\
    \hline \hline
\end{tabularx}
\end{table}
\end{document}
  • Thanks! One more question: so this approach divides all 6 evenly across. What about a scenario where, for example, Header 1 is much longer than Headers 2 and 3, so the A & B cells under Header 1 should be wider than the rest? – Amy Mar 8 '18 at 22:44
  • @Amy I couldn't find a way to make the first two columns wider as you said (unless setting the width manually). I proposed a different solution. – Phelype Oleinik Mar 8 '18 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.