Table column width when using multi-column

formattinghorizontal alignmentmulticolumntables

I have created a table with the following code in Overleaf:

\begin{table}[H]
    \centering
    \begin{tabular}{|c|ccc|ccc|}
        \hline
        \multicolumn{1}{|c|}{} & \multicolumn{3}{|c|}{Partially Structured} & \multicolumn{3}{|c|}{Unstructured} \\
        \hline
         & \textbf{m1} & \textbf{m2} & \textbf{m3} & \textbf{m1} & \textbf{m2} & \textbf{m3} \\
        \hline
        \textbf{n1} & x & y & z & x & y & z \\
        \textbf{n2} & x & z & z & z & x & y \\
        \textbf{n3} & x & y & x & y & z & x \\
        \hline
    \end{tabular}
\end{table}

However, there's a bit of a formatting problem with the output as you can see here (ignore the yellow fill):

table output

Because the header Partially Structured is longer than the text that's in all the cells under it, the column width has had to expand, but it has done so only by expanding the m3 column rather than by equally expanding m1, m2 and m3 (sorry if this is difficult to follow – hard to explain in words!). Is there a way to make all three columns under this heading the same width?

Best Answer

Rather than make the first three data columns equally wide, I would like to suggest you make all six data columns equally wide.

In the following solution, the (usable) width of the data columns is calculated so as to evenly "fill" the space below the first subheader (\textbf{Partially Structured), and then all six data columns are assigned that width.

In case you're not familiar with LaTeX column width calculations, here's a step by step guide:

  • The total width of the first three data columns is \widthof{\textbf{Partialy Structured}} + 2\tabcolsep. By default, LaTeX inserts whitespace padding in the amount of \tabcolsep on each side of a cell, incl. a cell that spans three "basic" colums.

  • The total width of the first three data columns can also be expressed as 3(usable width of equal-width columns)+6\tabcolsep.

  • The usable width of the equal-spaced columns may therefore be calculated as

    ( \widthof{\textbf{Partialy Structured}} - 4\tabcolsep ) / 4
    

The following screenshot shows first the table with all six data columns having equal width, followed by a version that lets the first three data columns be markedly wider than the next three. I hope you will agree that the first table looks better.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage{calc}  % for '\widthof' macro
\usepackage{array} % for 'w' column type
\newlength\mylen
% calculate (minimal) width of the 6 data columns:
\setlength\mylen{(\widthof{\textbf{Partially Structured}}-4\tabcolsep)/3}

\begin{document}

\begin{table}[ht!]
    \centering

    %% use \mylen as width of all 6 data columns:
    \begin{tabular}{| c | *{3}{wc{\mylen}} | *{3}{wc{\mylen}} |}
        \hline
        & \multicolumn{3}{c|}{\textbf{Partially Structured}}
        & \multicolumn{3}{c|}{\textbf{Unstructured}} \\
        \hline
        & \textbf{m1} & \textbf{m2} & \textbf{m3} 
        & \textbf{m1} & \textbf{m2} & \textbf{m3} \\
        \hline
        \textbf{n1} & x & y & z & x & y & z \\
        \textbf{n2} & x & z & z & z & x & y \\
        \textbf{n3} & x & y & x & y & z & x \\
        \hline
    \end{tabular}
    
    \bigskip\bigskip
    %% same table, but with 'c' column type for final 3 columns
    \begin{tabular}{| c | *{3}{wc{\mylen}} | ccc |}
        \hline
        & \multicolumn{3}{c|}{\textbf{Partially Structured}}
        & \multicolumn{3}{c|}{\textbf{Unstructured}} \\
        \hline
        & \textbf{m1} & \textbf{m2} & \textbf{m3} 
        & \textbf{m1} & \textbf{m2} & \textbf{m3} \\
        \hline
        \textbf{n1} & x & y & z & x & y & z \\
        \textbf{n2} & x & z & z & z & x & y \\
        \textbf{n3} & x & y & x & y & z & x \\
        \hline
    \end{tabular}
    
\end{table}
\end{document}
Related Question