# How to create complicated tables (multiple nested columns and rows)

I have an image of a table that I want to replicate but I can't figure out how to do the multiple rows part (see left side of column) and the multiple columns part (see right side).

Is there a general way to do this? How do you go about creating such tables? The numbers are irrelevant, I'm looking for a piece of code that I can easily extend as necessary (add more rows/colums).

Edit:

Thanks to @leandriis, the following works well:

\begin{tabular}{c|c|cccccc}
%\toprule
Metric & Dataset &
%\multicolumn{6}{c}{SGAN (Ours)}\\ \cmidrule{3-8} & &
\makecell[b]{20V-20 \\ (pretrained)} & \makecell[b]{20VP-20 \\ (pretrained)} & \makecell[b]{20VP-20 \\ (retrained)} & \makecell[b]{Mean \\ Pooling} & \makecell[b]{Random \\ Pooling} & \makecell[b]{Gumble \\ Pooling}\\
\midrule
& ETH & 3 & 3 & 3 \\
& ETH & 3 & 3 & 3 \\
ADE & ETH & 3 & 3 & 3 \\
& ETH & 3  & 3 & 3\\
& ETH & 3 & 3 & 3 \\
AVG & & 3 & 3 & 3 \\
\midrule\midrule
& ETH & 3 & 3 & 3 \\
& ETH & 3 & 3 & 3 \\
ADE  & ETH & 3 & 3 & 3 \\
& ETH & 3  & 3 & 3\\
& ETH & 3 & 3 & 3 \\
AVG & & 3 & 3 & 3 \\
\bottomrule\bottomrule
\end{tabular}


• It is not very hard to do. What have you tried? – Bernard Jul 21 at 14:07
• Do you need a reproduction of the exact format, or do you prefer a more classic, and nicer, layout? – Sveinung Jul 21 at 14:09
• My question was if you needed an exact reproduction of you example. Nicer means a classic, formal book style table, without vertical rules, and more space between rows. – Sveinung Jul 21 at 14:16
• Here at stackexchange. Search on "booktabs". I will give you an example, if not Bernhard or leandriis does it befor me (or Mico) – Sveinung Jul 21 at 14:18
• \begin{tabular}{c|c|c|c|c|c|c|c|c} \multirow{2}{*}{\textbf{Metric}} &\multirow{2}{*}{\textbf{Dataset}} & \multirow{2}{*}{\textbf{Linear}} & \multirow{2}{*}{\textbf{LSTM}}& \multirow{2}{*}{\textbf{S-LSTM}}& \multicolumn{4}{c}{\textbf{SGAN (Ours)}}\\\&&&&& 1V-I&1V-20&20V20&20VP-20\\\\\hline\hline multirow{5}{*}{\textbf{ADE}}& \textbf{ETH} & ...& 061 / \textbf{0.81} & \textbf{0.60} / 0.87 \\ & \textbf{HOTEL} & ... & 0.52 / 0.67 \\\ This is a code for a simple format without siunitx package and booktabs etc... You just need to complete it and to ad multirow package in your preamble. – koleygr Jul 21 at 14:18

Here is a short code, that might serve as a place to start from. I have included some \multicolumn an \multirow commands in order to get the alignment that was mentioned in the question.

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

\begin{tabular}{c|c|c|c|c}
\multirow{2}{*}{\textbf{Metric}} & \multirow{2}{*}{\textbf{Dataset}} &
\multicolumn{3}{c}{\textbf{SGAN (Ours)}}\\
& & 1V-1 & 1V-20 & 20V-20\\
\hline \hline
& \textbf{ETH} & 3 & 3 & 3 \\
& \textbf{ETH} & 3 & 3 & 3 \\
ADE  & \textbf{ETH} & 3 & 3 & 3 \\
& \textbf{ETH} & 3  & 3 & 3\\
& \textbf{ETH} & 3 & 3 & 3 \\
\hline
\textbf{AVG} & & & & \\
\hline\hline
& \textbf{ETH} & 3 & 3 & 3 \\
& \textbf{ETH} & 3 & 3 & 3 \\
ADE  & \textbf{ETH} & 3 & 3 & 3 \\
& \textbf{ETH} & 3  & 3 & 3\\
& \textbf{ETH} & 3 & 3 & 3 \\
\hline
\textbf{AVG} & & & &
\end{tabular}

\end{document}


Here is also a suggestion on how to use the booktabs package in order to create a table that has no vertical and just a few horizontal lines with some additional white space around them. I'd also suggest to not use bold for the column headers and the contents of the first/second column:

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\begin{tabular}{ccccc}
\toprule
Metric & Dataset &
\multicolumn{3}{c}{SGAN (Ours)}\\ \cmidrule{3-5}
& & 1V-1 & 1V-20 & 20V-20\\
\midrule
ADE & ETH & 3 & 3 & 3 \\
& ETH & 3 & 3 & 3 \\
& ETH & 3 & 3 & 3 \\
& ETH & 3  & 3 & 3\\
& ETH & 3 & 3 & 3 \\
AVG & & 3 & 3 & 3 \\
\midrule
ADE & ETH & 3 & 3 & 3 \\
& ETH & 3 & 3 & 3 \\
& ETH & 3 & 3 & 3 \\
& ETH & 3  & 3 & 3\\
& ETH & 3 & 3 & 3 \\
AVG & & 3 & 3 & 3 \\
\bottomrule
\end{tabular}

\end{document}


Here is a comparison of both tables side by side:

For an improved alignment of the numbers in column 3-9 I'd suggest tue usage of the siunitx package.

• Is there a reason you left out lines between for example "Metric" and "Dataset" or "ADE and "AVG"? – Anna Vopureta Jul 21 at 14:33
• @AnnaVopureta: Do you refer to the vertical lines that I completely removed in my second example? If so, this is due to the "more classic, and nicer, layout" that Sveinung already mentioned/explained in their comment. – leandriis Jul 21 at 14:35
• @AnnaVopureta: Personally, I don't agree that vertical lines would be useful in this table. But if you prefer to have them, you can use | inbetween the column specifiers (\begin{tabular}{cc} without line and \begin{tabular}{c|c}with vertical line) just as in the first example. Please also note, that booktabs lines are incompatible with vertical lines, so it might be better to stay with the first example code (usage of \hline) then. – leandriis Jul 21 at 14:40
• @AnnaVopureta: Please have a closer look at the intersections of the horizontal and vertical lines if you add | to the second example. You will notice that there is a small white space in the vertical lines. This is due to the booktabs rules being incompatible with vertical lines. – leandriis Jul 21 at 14:46
• @AnnaVopureta: Sorry for the typo, I meant \multirow{5}{*}{ADE}. This should work. Alternatively, you can also go without multirow if you move the text "ADE" two rows down (to the third row of the respective block of five rows) as I have done in the first example code. – leandriis Jul 21 at 14:58

Here is your tabular typeset approximately how I would have done in my documents. I have one grade smaller letters in the heading, and italic instead of bold to emphasise headings and numbers. this is based on the recommendation in a text book for the typography study in Norway. I increased the margins so the tabular fits between the margin.

In the preamble, I have used three commands to change the tabular’s spacing:

\setlength{\extrarowheight}{1pt}
\renewcommand{\arraystretch}{1.1}
\setlength{\tabcolsep}{4pt}


The first command moves the cell content a little bit down. The second increases the spacing between rows (creates some air). The third command decreases the spacing between columns.

I have removed all vertical rules and used a heavier rule and added space instead of double \hlines; all according to recommendations in the booktabs manual. That package is used to typeset all rules, so you have suitable distance between rules and cell content. I have also used the array package to have more flexible table commands.

NB! The booktabs manual is a must read if you want to typeset nice tabulars.

As you also will see, I use two right align column to typeset the numbers. Since all cells have the same number of decimals, you can use simple r-columns in combination with an array-trick to typeset 1.21 / 2.35. To typeset the slash between each pair of numbers, I use r@{\,/\,}r, which removes all space between the two columns and replaces it with two tiny spaces (\,) and the slash in between.

In the headings, I have merged two columns using multicolumn. For additional tips on how to typeset a tabular, I checked a text book used from the mid-fifties for educating typographers in Norway. I have updated my example to be more in:

\documentclass{article}
\usepackage[left=1.5cm, right=1.5cm]{geometry}
\usepackage{makecell, array, booktabs, multirow, caption}

\setlength{\extrarowheight}{1pt}
\renewcommand{\arraystretch}{1.2}
\setlength{\tabcolsep}{4.5pt}

\begin{document}

\begin{table}[!t]
\caption{Tabular with lots of columns}
\centering
\begin{tabular}{@{\kern0.1em}cl r@{\kern0.05em/\kern0.05em}r r@{\kern0.05em/\kern0.05em}r r@{\kern0.05em/\kern0.05em}r r@{\kern0.05em/\kern0.05em}r r@{\kern0.05em/\kern0.05em}r r@{\kern0.05em/\kern0.05em}r r@{\kern0.05em/\kern0.05em}r@{\kern0.1em}}
\toprule
\multirow{2}{*}[-0.75ex]{\small{Metric}} & \multirow{2}{*}[-0.75ex]{\small{Dataset}} & \multicolumn{2}{c}{\multirow{2}{*}[-0.75ex]{\small{Linear}}}&\multicolumn{2}{c}{\multirow{2}{*}[-0.75ex]{\small{LSTM}}}&\multicolumn{2}{c}{\multirow{2}{*}[-0.75ex]{\small{S-LSTM}}}&\multicolumn{8}{c@{}}{\small{SGAN (Ours)}}    \\\cmidrule(l){9-16}
\multicolumn{8}{l}{}&\multicolumn{2}{c}{\small{1V-1}}&\multicolumn{2}{c}{\small{1V-20}}&\multicolumn{2}{c}{\small{20V-20}}&\multicolumn{2}{c@{}}{\small{20VP-20}}    \\\midrule
&ETC&0.84&1.33&0.70&1.09&0.73&1.09&0.79&1.13&0.75&1.03&0.61&\emph{0.81}&\emph{0.60}&0.87 \\
&HOTEL&\emph{0.35}&\emph{0.39}&0.55&0.86&0.49&079&0.71&1.01&0.63&0.90&0.48&0.72&0.52&0.67  \\
&ZARA1&0.41&0.62&0.25&0.41&0.27&0.47&0.25&0.42&0.23&0.38&\emph{0.21}&\emph{0.34}&0.22&0.35  \\
&ZARA2&0.53&0.77&0.31&0.52&0.33&0.56&0.32&0.52&0.29&0.47&\emph{0.27}&0.42&0.29&\emph{0.42}  \\\midrule
&ETC&1.60&2.94&1.45&2.41&1.48&2.35&1.61&2.21&1.52&2.02&1.22&\emph{1.52}&\emph{1.19}&1.62 \\
&HOTEL&\emph{0.60}&\emph{0.72}&1.17&.1.91&1.01&1.76&1.44&2.18&1.32&1.97&0.95&1.61&1.02&1.37 \\
FDE&UNIV&1.01&1.59&0.77&1.31&0.84&1.40&0.75&1.28&\emph{0.73}&\emph{1.22}&0.75&1.26&0.84&1.52 \\
&ZARA1&0.74&1.21&0.53&0.88&0.56&1.00&0.53&0.91&0.48&0.84&\emph{0.42}&0.69&0.43&\emph{0.68}  \\
&ZARA2&0.95&1.48&0.65&1.11&0.70&1.17&0.66&1.11&0.61&1.01&\emph{0.54}&\emph{0.84}&0.58&0.84  \\\midrule
\multicolumn{1}{@{}l}{\emph{AVG}}&&0.98&1.59&0.91&1.52&0.91&1.54&1.00&1.54&0.93&1.41&\emph{0.78}&\emph{1.18}&0.81&1.21  \\\bottomrule
\end{tabular}
\end{table}
\end{document}