4

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).

Complicated table

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}

Resulting table

PlsWork
  • 157
  • 1
    It is not very hard to do. What have you tried? – Bernard Jul 21 '19 at 14:07
  • You might be interested in \multicolumn and \multirow. – leandriis Jul 21 '19 at 14:08
  • 1
    Do you need a reproduction of the exact format, or do you prefer a more classic, and nicer, layout? – Sveinung Jul 21 '19 at 14:09
  • @Sveinung I'm unfamiliar with the options there are (classic, nicer, ??), For now, I'm fine with learning how to create tables with whatever is easier (less code, readable code). – PlsWork Jul 21 '19 at 14:12
  • 1
    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 '19 at 14:16
  • @Sveinung Where can I learn how to create such nicer tables? – PlsWork Jul 21 '19 at 14:16
  • 2
    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 '19 at 14:18
  • 1
    \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 '19 at 14:18
  • You do not need makecell to typeset the first headers. Just use two rows – Sveinung Jul 21 '19 at 20:47

2 Answers2

4

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:

enter image description here

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

leandriis
  • 62,593
  • Is there a reason you left out lines between for example "Metric" and "Dataset" or "ADE and "AVG"? – PlsWork Jul 21 '19 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 '19 at 14:35
  • Yes, I mean the vertical lines from the second example. I think it would look good to at least add vertical lines to separate table structure and the data (maybe I'm wrong let's see). How can I add such vertical lines? – PlsWork Jul 21 '19 at 14:37
  • 3
    @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 '19 at 14:40
  • \begin{tabular}{c|c} works on the second example too thanks! Another question, is there a reason why you didn't center "ADE" and "ADE" (under metrics) in the second example? How can I center them to see how it looks? – PlsWork Jul 21 '19 at 14:44
  • 1
    @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 '19 at 14:46
  • Regarding the centered contents of the first column: Personally, I don't think vertically centering them adds to the readability of the table, but if you prefer this, replace ADE by \multirow{5}{=}{ADE}. – leandriis Jul 21 '19 at 14:47
  • Unfortunately, \multirow{5}{=}{ADE}causes "ADE" not only to be centered, but it also shifts it to the right a bit (then the table looks odd because "ADE is not vertically alligned with the rest) – PlsWork Jul 21 '19 at 14:50
  • 1
    @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 '19 at 14:58
  • Huh, when I try to extend the table (add more rows) the table goes off the page instead of being resized to fit the page. – PlsWork Jul 21 '19 at 15:00
  • @AnnaVopureta: Tables don't automatically resize to fit into the textwidth. You could do one of the following: a) reduce the font size b) decrease teh margins c) decrease the horizontal white space between columns... ore ideas can be found for example here: My table doesn't fit; what are my options? If you have problems implementing the solutions presented there, please ask a new question and include a minimal working example (MWE) there that allows others to easily reproduce teh output you get. – leandriis Jul 21 '19 at 15:04
  • At a second glance I read that you added more rows (not columns as I initially thought). In that case, you might also be interested in the longtable package that allows long table s to be split over multiple pages. – leandriis Jul 21 '19 at 15:05
  • I meant columns, not rows sorry. I ended up using \usepackage{makecell} to shorten the column names, now everything fits. – PlsWork Jul 21 '19 at 15:19
4

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:

enter image description here

\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  \\
ADE&UNIV&0.56&0.82&0.36&0.61&0.41&0.67&0.37&0.60&0.36&0.58&\emph{0.36}&\emph{0.60}&0.44&0.76  \\
&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
\multicolumn{1}{@{}l}{\emph{AVG}}&&0.54&0.79&0.43&0.70&0.45&0.72&0.49&0.74&0.45&0.67&\emph{0.39}&\emph{0.58}&0.41&0.61 \\\bottomrule\addlinespace[1ex]
&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}
Sveinung
  • 20,355