Create a table with different number of columns per row where multicolumn doesn’t seem to work

tables

I've created this table in Word to illustrate some data. I want to preface that this isn't an example, it's the exact table I'm using, but I cleared the data because it's not something I'm allowed to share.

enter image description here

I've read the questions I could find on this site regarding different columns size per row in tables, but I couldn't find something to make this work. The only option I think could work would be to create a table with many columns (I'm still thinking of how many columns I would need) and to do complicated multicolumns for each row that I have.

For now I took a screenshot of the table and put it as an image in my file, but that's something I would like to avoid. The last option I would have apart from that is to remove some datapoints from the table to make the number of columns fit but I can't believe this is the solution I'd have to resort to.

I've been asked to produce a minimal working example, so I picked the last three rows of my table because they illustrate very well my problem. I've created a table with 13 columns (three for the left headers, 10 for the following values). Since 4, 9 and 10 don't have a common multiplier, I don't see how I can achieve a result similar to the screenshot I posted above.

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{table}[h]
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline
\multicolumn{3}{|c|}{} & \multicolumn{10}{|c|}{Section} \\
\hline
\multicolumn{3}{|c|}{\textbf{Some name}} & \multicolumn{2}{|c|}{2} & \multicolumn{2}{|c|}{3} & \multicolumn{2}{|c|}{4} & 
\multicolumn{2}{|c|}{5} & & \\
\hline
\multicolumn{3}{|c|}{Some metric} & \multicolumn{2}{|c|}{--} & \multicolumn{2}{|c|}{--} & \multicolumn{2}{|c|}{--} & 
\multicolumn{2}{|c|}{--} & & \\
\hline

\multicolumn{3}{|c|}{\textbf{Some name}} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 & \\
\hline
\multicolumn{3}{|c|}{Some metric} & -- & -- & -- & -- & -- & -- & -- & -- & -- & \\
\hline

\multicolumn{3}{|c|}{\textbf{Some name}} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 & 24\\
\hline
\multicolumn{3}{|c|}{Some metric} & -- & -- & -- & -- & -- & -- & -- & -- & -- & -- \\
\hline
\end{tabular}
\end{table}

\end{document}

which gives the following result : enter image description here

I was wondering, isn't there a way to simply specify the amount of columns that I want on a "per row" basis or something like that?

Best Answer

To have a layout like that using \multicolumn you'd need to make the table with 90(!) columns (the LCM between the number of columns in each row of that table), then use \multicolumn appropriately to achieve that layout.

A much simpler solution is to stack tables on top of each other, each with a different number of columns, but all with the same width. The document below does that; the code could be made a lot more automagic, to make using it less painful, but that's left as an exercise :)

enter image description here

\documentclass{article}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\definecolor{tbg}{HTML}{E6E6E6}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{|p{3cm}|*6{C|}} \hline
    \cellcolor{tbg}    & \multicolumn{6}{c|}{\cellcolor{tbg} \textbf{Section 1}} \\ \hline
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline
    Some metric        &   &   &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*2{C|}}
    \textbf{Some Name} & 2 & 3 \\ \hline
    Some metric        &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*3{C|}}
    \textbf{Some Name} & 3 & 6 & 8 \\ \hline
    Some metric        &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*6{C|}}
    \textbf{Some Name} & 1 & 2 & 5 & 10 & 15 & 19 \\ \hline
    Some metric        &   &   &   &    &    &    \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*2{C|}}
    \textbf{Some Name} & 1 & 125 \\ \hline
    Some metric        &   &     \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*5{C|}}
    \cellcolor{tbg}    & \multicolumn{5}{c|}{\cellcolor{tbg} \textbf{Section 2}} \\ \hline
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 \\ \hline
    Some metric        &   &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*4{C|}}
    \textbf{Some Name} & 1 & 2 & 3 & 4 \\ \hline
    Some metric        &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*4{C|}}
    \textbf{Some Name} & 2 & 3 & 4 & 5 \\ \hline
    Some metric        &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*9{C|}}
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 \\ \hline
    Some metric        &   &   &   &   &   &   &    &    &    \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*{10}{C|}}
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 & 24 \\ \hline
    Some metric        &   &   &   &   &   &   &    &    &    &    \\ \hline
  \end{tabularx}\par\nointerlineskip
\end{table}
\end{document}