5

I am really new in the use of LaTex and would like some help to make this table using booktabs formatting. I use R and would ideally generate it using xtables package. However, I could not deal with the column spanning thing.

That is why I tried to manually insert \multicolumn command.

Here is the code:

\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}
\begin{document}


 \begin{table}[ht]
\centering
\begin{tabular}{@{\extracolsep{4pt}}llccccccc}
\toprule   
{} & {} & {Observations} & \multicolumn{3}{c}{Median}  & \multicolumn{3}{c}{SD}\\
 \cline{3-3} 
 \cline{4-6} 
 \cline{7-9} 
 Year & Group & N & V1 & V2 & V3 & V1 & V2 & V3 \\ 
   \hline
   \multirow{2}{*}{2012}
  & Control & 2 & 0.052 & 0.294 & 0.115 & 0.304 & 0.619 & 0.611 \\ 
  & Treat & 2 & 0.511 & 0.083 & 0.123 & 0.573 & 0.541 & 0.734 \\ 
  \multirow{2}{*}{2016}
  & Control & 3 & 0.320 & 0.344 & 0.382 & 0.382 & 0.494 & 0.477 \\ 
  & Treat & 3 & 0.378 & 0.296 & 0.123 & 0.386 & 0.668 & 0.732 \\ 
 \hline
\end{tabular}
\caption{Mean by Year and Group} 
\end{table}
\end{document}

However, I think this table is not really well formatted, not according to booktabs standards, is it? How to make manually a table with the same design, but better formatted?

Thank you

  • Welcome to TeX.SX! \multirow is also valid in booktabs, but read the manual for design suggestions. You should avoid commands like \cline and \hline and use their equivalents however. – TeXnician Jul 11 '17 at 17:23
7

The main problem with the table is that you are not using the booktabs rules, but instead a mixture of standard tabular rules (\hline and \cline) and booktabs rules (\toprule).

Replacing these rules with the booktabs versions (\midrule, \cmidrule and \bottomrule) gives you the nice formatting you want.

I've removed the multirow stuff. This adds nothing to your table; it's perfectly clear just to put the year on the first row of the pairs of rows for that year. In general I would avoid using multirow at all. It has its uses, but usually for things like forms that have boxed cells. For standard research tables it's almost never appropriate.

P.S. I hope the data is made up! Your Ns are really small... :)

\documentclass{article}

\usepackage{booktabs}
\begin{document}


 \begin{table}[ht]
\centering
\begin{tabular}{@{\extracolsep{4pt}}llccccccc}
\toprule   
{} & {} & {Observations} & \multicolumn{3}{c}{Median}  & \multicolumn{3}{c}{SD}\\
 \cmidrule{3-3} 
 \cmidrule{4-6} 
 \cmidrule{7-9} 
 Year & Group & N & V1 & V2 & V3 & V1 & V2 & V3 \\ 
\midrule
2012  & Control & 2 & 0.052 & 0.294 & 0.115 & 0.304 & 0.619 & 0.611 \\ 
  & Treat & 2 & 0.511 & 0.083 & 0.123 & 0.573 & 0.541 & 0.734 \\ 
2016  & Control & 3 & 0.320 & 0.344 & 0.382 & 0.382 & 0.494 & 0.477 \\ 
  & Treat & 3 & 0.378 & 0.296 & 0.123 & 0.386 & 0.668 & 0.732 \\ 
\bottomrule
\end{tabular}
\caption{Mean by Year and Group} 
\end{table}
\end{document}

output of code

Some comments on typesetting numeric data

Using the c column type is really not the best plan when you're typesetting numeric data. Usually we want numeric data to be aligned in some way, so that if the numbers are of different sizes, they line up properly.

For decimal data, it's normal to line up on the decimal point. For non-decimal data, simple right alignment is fine.

To align with the decimal point, you can use the siunitx package or the dcolumn package. siunitx has many fancy capabilities for formatting numeric data automatically, including digit grouping for large numbers. It also provides ways of properly typesetting SI units. The dcolumn package is designed specifically for decimal point alignment. Here's an example to show you the differences.

\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{siunitx}

% Shorthand for multicolumn
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}
\begin{document}
\begin{tabular}{ccrD{.}{.}{2.3}S}
\toprule
Center & Center & \mc{right} & \mc{D-column} & \mc{S-column}  \\
\midrule
1 & 32.6 & 1 & 32.6 & 32.6  \\
10 & 335.434 & 10 & 335.434 & 335.434  \\
100 & 0.99 & 100 & 0.99 & 0.99  \\
5 & 1 & 5 & 1 & 1  \\
\bottomrule
\end{tabular}
\end{document}

output of sample

  • Thank you! They are just an example, later I will deal with the original dataset. – Arthur Carvalho Brito Jul 11 '17 at 17:37
  • 1
    @ArthurCarvalhoBrito I would also tend not to use c columns for numbers, r columns are more sensible, or even smarter columns like those provided in the siunitx package. – Alan Munn Jul 11 '17 at 17:39
  • sorry, what do you mean? I am really a novice on this – Arthur Carvalho Brito Jul 11 '17 at 17:43
  • @AlanMunn: Where are the grey \cmidrules in your code? – Bernard Jul 11 '17 at 18:38
  • @Bernard There are none. It must be an artefact of the screenshot. – Alan Munn Jul 11 '17 at 18:41
8

First all your rules should be done with booktabs, as it adds some padding to your table rules (table rows in basic latex of often much too tight).

Second, table captions should be above tables, as tables may spread over pages, and readers shouldn't have to turn the page to know what the table is about. For this the caption package is useful, basic latex forgets to swap the values of \abovecaptionskip and \belowdisplayskip when the caption is over the table.

Third, I would replace Observations with an abbreviation, as I find it ugly to have a column of single digits with a such a wide column head.

Fourth, I would set a smaller intercolumn space for the two groups of 3 columns at the end of the table. Also, the (lr) parameter for `\cmidrules is usefule here to improve the general look of the table.

\documentclass{article}
\usepackage{array, multirow, caption, booktabs}

\begin{document}

 \begin{table}[ht]
\centering\setlength\extrarowheight{2pt}
\caption{Mean by Year and Group}
\begin{tabular}{@{\extracolsep{4pt}}llc*{2}{c@{\enspace}c@{\enspace}c}}
\toprule
{} & {} & {Obsv\textsuperscript{ns}} & \multicolumn{3}{c}{Median} & \multicolumn{3}{c}{SD}\\
 \cmidrule(lr){3-3}
 \cmidrule{4-6}
 \cmidrule(lr){7-9}
 Year & Group & N & V1 & V2 & V3 & V1 & V2 & V3 \\
   \midrule
   \multirow{2}{*}{2012}
  & Control & 2 & 0.052 & 0.294 & 0.115 & 0.304 & 0.619 & 0.611 \\
  & Treat & 2 & 0.511 & 0.083 & 0.123 & 0.573 & 0.541 & 0.734 \\
\addlinespace
  \multirow{2}{*}{2016}
  & Control & 3 & 0.320 & 0.344 & 0.382 & 0.382 & 0.494 & 0.477 \\
  & Treat & 3 & 0.378 & 0.296 & 0.123 & 0.386 & 0.668 & 0.732 \\
 \bottomrule
\end{tabular}
\end{table}

\end{document} 

enter image description here

Note: If you don't want to abbreviate `Observations, you should consider reorganising the table swapping rows and columns.

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.