6

I am having a lot of trouble aligning numbers with different format and text within a table environment. After doing a lot of research, I only get as far as the following:

enter image description here

The rows X to Z and Fstat are not correctly centered (I have tried table-number-alignment=center and it did not work). In addition, each column does not share equal space and stars in column CC are already outside of the table. How should I resolve these issues?

My codes are posted below. Note that they are currently set up so that my estimates are aligned by decimals and parentheses are properly displayed (and these are also what I want).

Thank you in advance!

\documentclass[12pt,letterpaper,fleqn]{article}

\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{parse-numbers=false}

\begin{document}

\begin{table}[h]
\centering\small
\begin{tabular}{l *{5}S[table-format=-1.3]}
\midrule\midrule
& \multicolumn{1}{c}{AA} & \multicolumn{1}{c}{AA} & \multicolumn{1}{c}{AA} & \multicolumn{1}{c}{BB} & \multicolumn{1}{c}{CC} \\
& \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} \\
\midrule
X & -0.333^{***} & -0.222^{***} & -0.776^{***} & -0.333^{***} & -0.662^{***} \\ 
  & (0.003) & (0.002) & (0.026) & (0.048) & (0.001) \\ 
Y & & -0.004 & & \\ 
  & & (0.008) & & \\ 
Z & 0.111 & 0.122 & 0.123 & 0.122 & 0.133 \\ 

Obs & \multicolumn{1}{c}{$55,000$} & \multicolumn{1}{c}{$56,000$} & \multicolumn{1}{c}{$56,000$} & \multicolumn{1}{c}{$56,000$} & \multicolumn{1}{c}{$56,000$} \\ 
FE & & & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\
Fstat & & 0.225 & 0.221 & 0.222 & 0.220 \\
\midrule\midrule
\end{tabular}
\end{table}


\end{document}
  • You don't want the numbers to be aligned at the period? – Johannes_B Jul 11 '18 at 3:53
  • I do and after doing research, I decided to use siunitx for that. However, I cannot get them centered (note row X Y Z are not centered as row Obs and FE). – James H Jul 11 '18 at 3:55
5

You need to use braces around all of your repeated column specifier: *{5}{S[...]} not *{5}S[...] to apply the options to every column. That leads to an answer that looks very similar to Mico's:

\documentclass[12pt,letterpaper,fleqn]{article}

\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{parse-numbers = false}

\begin{document}

\begin{table}[h]
\centering
\begin{tabular}{@{}l *{5}{S[table-format = +1.5]}@{}}
\toprule
& \multicolumn{1}{c}{AA} & \multicolumn{1}{c}{AA} & \multicolumn{1}{c}{AA} & \multicolumn{1}{c}{BB} & \multicolumn{1}{c}{CC} \\
& \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} \\
\midrule
X & -0.333^{***} & -0.222^{***} & -0.776^{***} & -0.333^{***} & -0.662^{***} \\ 
  & (0.003) & (0.002) & (0.026) & (0.048) & (0.001) \\ 
Y & & -0.004 & & \\ 
  & & (0.008) & & \\ 
Z & 0.111 & 0.122 & 0.123 & 0.122 & 0.133 \\ 

Obs & \multicolumn{1}{c}{$55,000$} & \multicolumn{1}{c}{$56,000$} & \multicolumn{1}{c}{$56,000$} & \multicolumn{1}{c}{$56,000$} & \multicolumn{1}{c}{$56,000$} \\ 
FE & & & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\
Fstat & & 0.225 & 0.221 & 0.222 & 0.220 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

(When parse-numbers = false is set, siunitx uses the same approach as dcolumn for alignment. So the main issue is making sure it knows how many digits to leave space for.)

| improve this answer | |
  • +1. :-) It might be a good idea to encase the instances of , in the Obs row in curly braces, lest TeX think that the commas are punctuation marks rather than thousands-dividers. And, drop all 18 instances of \multicolumn{1}{c} -- they're not needed. – Mico Jul 11 '18 at 6:51
  • @Mico I wondered about the , too: I'd naturally use \num here as we are talking about siunitx :) Agree also that the header could be simplified, but that's probably going to distract from the core point. – Joseph Wright Jul 11 '18 at 6:54
  • Won't the parse-numbers = false setting keep \num from inserting thousands-separators in the 5-digit numbers, though? – Mico Jul 11 '18 at 7:01
  • 1
    @Mico Yes if it applies globally, but then one could use the optional argument to \num. In the real use case, parse-numbers = false might be applied in the optional argument to the S column: in that case, it won't be active if \multicolumn is used (as that applies before the standard column definition). – Joseph Wright Jul 11 '18 at 7:04
  • Ah, so there's a potential use case for not omitting the \multicolumn{1}{c} strings... – Mico Jul 11 '18 at 7:10
4

In your test document, the only use of the siunitx package seems to be its S column type, to align numbers in columns on their decimal markers. (Otherwise, why issue the instruction \sisetup{parse-numbers=false}?) If that's the case, you could simplify the document setup considerably by employing the dcolumn package, whose one and only job is -- you guessed it -- helping to align numbers in columns on their decimal markers.

Oh, and please use \toprule and \bottomrule instead of \midrule\midrule.

enter image description here

\documentclass[12pt,letterpaper,fleqn]{article}
\usepackage{booktabs}

\usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}}

\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\begin{document}

\begin{table}[h]
\centering  %\small  % "\small" doesn't seem necessary
\begin{tabular}{@{} l *{5}{d{2.5}} @{}}
\toprule
& \mc{AA}  & \mc{AA}  & \mc{AA}  & \mc{BB}  & \mc{CC} \\
& \mc{(1)} & \mc{(2)} & \mc{(3)} & \mc{(4)} & \mc{(5)} \\
\midrule
X & -0.333^{***} & -0.222^{***} & -0.776^{***} & -0.333^{***} & -0.662^{***} \\ 
  & (0.003) & (0.002) & (0.026) & (0.048) & (0.001) \\ 
Y & & -0.004  & & \\ 
  & & (0.008) & & \\ 
Z & 0.111 & 0.122 & 0.123 & 0.122 & 0.133 \\ 

Obs & \mc{$55{,}000$} & \mc{$56{,}000$} & \mc{$56{,}000$} & \mc{$56{,}000$} & \mc{$56{,}000$} \\ 
FE & & & \mc{Yes} & \mc{Yes} & \mc{Yes} \\
F-stat & & 0.225 & 0.221 & 0.222 & 0.220 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}
| improve this answer | |
  • Thank you so much for your solution, especially for your annotation! They worked as well. I have seen people recommend using siunitx instead of dcolumn, but why? Also, you recommended using toprule and bottomrule instead of double midrule, is there a particular reason? (if I like the double line look). Lastly, this is probably a very basic question, what is the role of "#1" in the setups? My guess is that it indicates an input?? – James H Jul 11 '18 at 15:21
  • @JamesH - If one's objective is solely to align numbers in a column on their decimal markers, I don't think it's correct to claim that siunitx package (and its S column type) is somehow better than the dcolumn package. In fact, I'd say that the setting-up overhead is larger with siunitx. About dual lines at the top and bottom of a table: Professional typographers tend to frown on them as they (the dual lines) can look amateurish. Finally, about the #1 marker: Indeed, it marks where the first (and in this case only) argument of the \mc command should be inserted. – Mico Jul 11 '18 at 16:47
1

A different siunitx realization: the numbers in the bottom part need not be aligned at the decimal point.

\documentclass[12pt,letterpaper,fleqn]{article}

\usepackage{booktabs}
\usepackage{siunitx}

\NewDocumentCommand{\snote}{sm}{%
  \IfBooleanTF{#1}
    {\ensuremath{^{#2}}}
    {\makebox[0pt][l]{$^{#2}$}}%
}

\begin{document}

\begin{table}[h]
\centering

\sisetup{
  table-align-text-post=false,
  table-space-text-pre={x(},
  table-space-text-post={)x},
  input-symbols={()},
  group-separator={,},
}
\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  *{5}{S[table-format = -2.4]}
%  @{\quad}
}
\toprule
& {AA} & {AA} & {AA} & {BB} & {CC} \\
& {(1)} & {(2)} & {(3)} & {(4)} & {(5)} \\
\midrule
X & -0.333\snote{***} & -0.222\snote{***} & -0.776\snote{***} & -0.333\snote{***} & -0.662\snote{***} \\ 
  & (0.003) & (0.002) & (0.026) & (0.048) & (0.001) \\ 
Y & & -0.004 & & \\ 
  & & (0.008) & & \\ 
Z & 0.111 & 0.122 & 0.123 & 0.122 & 0.133 \\ 
\midrule
Obs & \num{55000} & \num{56000} & \num{56000} & \num{56000} & \num{56000} \\ 
FE & & & {Yes} & {Yes} & {Yes} \\
Fstat & & \num{0.225} & \num{0.221} & \num{0.222} & \num{0.220} \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}

Use \snote{...} for getting zero space, \snote*{...} for the natural width.

enter image description here

| improve this answer | |

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.