2

There is extra space between "Test column" and "wider column"/"narrow" in Column (1) and (2). I tried to get no space between the two line and align the text to the bottom by using multirow package. I could not get it work...Any ideas?

enter image description here

\usepackage{booktabs}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}}
\usepackage{multirow}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}

\begin{table}[h]
\centering\small
\caption{Test table}\label{table:test}
\begin{tabular}{l *{5}{d{2.4}}}
\toprule
& \mc{Test column} & \mc{Test column} & \multicolumn{3}{c}{XXXXXXX} \\
\cmidrule{4-6}
&\mc{wider column} & \mc{narrow} & \mc{AAAA} & \mc{AAAA} & \mc{AAAA} \\
& \mc{(1)} & \mc{(2)} & \mc{(3)} & \mc{(4)} & \mc{(5)} \\
\midrule
Treatment & -0.115^{***} & -0.333^{***} & -0.220^{***} & -0.573^{***} & -0.228^{**} \\ 
 & (0.005) & (0.006) & (0.006) & (0.009) & (0.009) \\ 
\bottomrule
\end{tabular}
\end{table}
2

Here is a solution with multirow and makecell. Fine-tuning the vertical placement of the text can be made playing with the first argement (the number of lines): it can be given a fractional value.

Aldo I added the caption package to have a more sensible spacing between caption above and table:

\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}}
\usepackage{multirow, makecell, caption}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}
\newcommand\mr[2]{\multicolumn{1}{c}{\multirow{#1}{*}{\makecell{#2}}}}

\begin{document}

\begin{table}[h]
\centering\small
\caption{Test table}\label{table:test}
\begin{tabular}{l *{5}{d{2.4}}}
\toprule
& \mr{2.87}{Test column\\wider column} & \mr{2.87}{Test column\\narrow} & \multicolumn{3}{c}{XXXXXXX} \\
\cmidrule(lr){4-6}
& & & \mc{AAAA} & \mc{AAAA} & \mc{AAAA} \\
& \mc{(1)} & \mc{(2)} & \mc{(3)} & \mc{(4)} & \mc{(5)} \\
\midrule
Treatment & -0.115^{***} & -0.333^{***} & -0.220^{***} & -0.573^{***} & -0.228^{**} \\
 & (0.005) & (0.006) & (0.006) & (0.009) & (0.009) \\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

enter image description here

  • Thanks!!! I also would like to align the text to (1) and (2), so "wider column" and "narrow" in the same line as AAAA. Any thoughts? – James H Jul 24 '18 at 20:13
  • That's what you had with your code. Do you mean you want the ‘Test column’ a little lower than ‘XXXXXXX’, and the second line aligned with ‘AAAA’? – Bernard Jul 24 '18 at 20:17
  • Yes! Sorry for not being clear. I hope to have both lines aligned to the bottom (so narrow stays and test column comes down) – James H Jul 24 '18 at 20:18
  • It's easy: we can use fractional values with \multirow, so by trial and error, using \mr{2.85} instead of \mr{2} does the job. – Bernard Jul 24 '18 at 20:23
  • Thanks! Playing with it. May I ask why we need \multicolumn{1}{c} before multirow? We are not merging any columns here? – James H Jul 24 '18 at 20:46
1

My idea is to smash the two row cells:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{caption}

\NewDocumentCommand{\tworowcell}{m}{%
  \smash[b]{\begin{tabular}[t]{@{}c@{}}#1\end{tabular}}%
}
\NewDocumentCommand{\tn}{m}{\ensuremath{{}^{#1}}}

\begin{document}

\begin{table}[htp]
\centering

\caption{Test table}\label{table:test}

\sisetup{
  table-format=-1.3,
  table-space-text-post={\tn{***}},
  input-symbols={()},
}
\addtolength{\tabcolsep}{-0.5pt}

\begin{tabular}{ @{} l *{5}{S} @{} }
\toprule
& \tworowcell{Test column \\ wider column}
& \tworowcell{Test column \\ narrow }
& \multicolumn{3}{c}{XXXXXXX} \\
\cmidrule(l){4-6}
& & & {AAAA} & {AAAA} & {AAAA} \\
& {(1)} & {(2)} & {(3)} & {(4)} & {(5)} \\
\midrule
Treatment & -0.115\tn{***} & -0.333\tn{***} & -0.220\tn{***} & -0.573\tn{***} & -0.228\tn{**} \\ 
 & (0.005) & (0.006) & (0.006) & (0.009) & (0.009) \\ 
\bottomrule
\end{tabular}

\end{table}

\end{document}

You can also use dcolumn, but then \tworowcell should be encapsulated in a \multicolumn{1}{c}.

enter image description here

0

An alternative solution I just figured out... I replaced \cmidrule by \cline, and the spacing between XXXXX and AAAA disappeared, so no need to adjust spacing for (1) and (2). Just an alternative way...

  • Yes, but the rules from booktabs have some vertical padding which makes them look nicer. – Bernard Jul 24 '18 at 21:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.