[Tex/LaTex] using the package siunitx

siunitx

I am trying to align the decimal number in my table. I would like to use the siunitx package. The code I wrote is:

\documentclass{article}
\newcommand*{\MyIndent}
{\hspace*{1.5cm}}
\usepackage{siunitx}
\usepackage[labelfont=bf]{caption}
\begin{document}
\captionsetup{labelfont=bf}
\begin{table}
\centering
\caption{\textbf{Summary statistics \label{A2}}}
\begin{tabular}{l c c c c }{S[S[tabnumalign=center,tabformat=3.2]}\hline\hline
\multicolumn{1}{l}{\textbf{Variable}} & \textbf{Mean}
& \textbf{Std. Dev.}& \textbf{Min.} &  \textbf{Max.} \\ \hline\noalign{\medskip} \smallskip
Individual preferences for redistribution (IPR) & 0.132 & 0.339 & 0 & 1 \\ \smallskip
Subjective Freedom (SF) & 7.030 & 2.262 & 1 & 10 \\ \smallskip
Fairness (Hard work) & 0.239 & 0.426 & 0 & 1 \\ \smallskip
Gender & 0.513 & 0.5 & 0 & 1 \\ \smallskip
Age & 41.199 & 15.829 & 18 & 99 \\ \smallskip
Self-reported income & 4.907 & 2.312 & 1 & 10 \\ \smallskip
Low education & 0.27 & 0.444 & 0 & 1 \\ \smallskip
High education & 0.275 & 0.446 & 0 & 1 \\ \smallskip
Single & 0.24 & 0.427 & 0 & 1 \\ \smallskip
Married & 0.564 & 0.496 & 0 & 1 \\ \smallskip
No children & 0.285 & 0.451 & 0 & 1 \\ \smallskip
Size of town & 4.855 & 2.485 & 1 & 8 \\ \smallskip
Household income satisfaction & 5.791 & 2.515 & 1 & 10 \\ \smallskip
Chief wage earner & 0.495 & 0.5 & 0 & 1 \\ \smallskip
Health status & 3.87 & 0.87 & 1 & 5 \\ \smallskip \smallskip
Religious service attendance & & & & \\ 
\MyIndent More than once a week & 0.148 & 0.355 & 0 & 1 \\ 
\MyIndent Once a week & 0.187 & 0.39 & 0 & 1 \\
\MyIndent Once a month & 0.114 & 0.318 & 0 & 1 \\ 
\MyIndent Only on special holy days & 0.165 & 0.371 & 0 & 1 \\ 
\MyIndent Other specific holy days & 0.002 & 0.041 & 0 & 1 \\ 
\MyIndent Once a year & 0.062 & 0.241 & 0 & 1 \\ 
\MyIndent Less often & 0.11 & 0.313 & 0 & 1 \\
Religious affiliation & & & & \\
\MyIndent Catholics & 0.242 & 0.428 & 0 & 1 \\ 
\MyIndent Muslim & 0.135 & 0.342 & 0 & 1 \\ 
\MyIndent Protestant & 0.112 & 0.315 & 0 & 1 \\ 
\MyIndent Orthodox & 0.144 & 0.351 & 0 & 1 \\
Type of job & & & & \\ 
\MyIndent Manager & 0.042 & 0.2 & 0 & 1 \\ 
\MyIndent Non-manual & 0.048 & 0.213 & 0 & 1 \\
\MyIndent Manual & 0.17 & 0.376 & 0 & 1 \\
\MyIndent Full-time & 0.377 & 0.485 & 0 & 1 \\
\MyIndent Self-employed & 0.13 & 0.336 & 0 & 1 \\
\MyIndent Retired & 0.125 & 0.33 & 0 & 1 \\
\MyIndent Unemployed & 0.09 & 0.286 & 0 & 1 \\
\multicolumn{1}{c}{N} & \multicolumn{4}{c}{98673}\\ 
\hline \hline
\end{tabular}
\end{table}
\end{document}

When I add the bit "{S[S[tabnumalign=center,tabformat=3.2]}" I cannot compile my table anymore. I tried to add the above options using sisetup, but it did not work.

Anyone has a suggestion?

Thanks a lot.

Dario

Best Answer

The main error is in the wrong table preamble:

\begin{tabular}{l c c c c }{S[S[tabnumalign=center,tabformat=3.2]}

makes no sense; you have to specify S[<options>] just like a c or l column; so

\begin{tabular}{
  @{}
  l
  S[table-format=2.3]
  S[table-format=2.3]
  S[table-format=2.0]
  S[table-format=2.0]
  @{}
 }

to get a left aligned column, with four numeric columns with the indicated format for numbers (digits for the integer part and digits for the decimal part).

You have several \smallskip commands that only add noise to the table; just use the standard line spacing, adding horizontal rules for dividing the blocks; I also used italics for the subtitles, in order to emphasize them as not really rows of the table.

Finally, I used \small because the table is quite large; I also reduced a bit the separation between columns (this might be unnecessary, depending on the pagination parameters of your document).

Also the text of a caption shouldn't have a surrounding \textbf command: do the setup with \captionsetup.

\documentclass{article}

\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[labelfont=bf]{caption}

\captionsetup{font=bf}
\newcommand*{\MyIndent}{\hspace*{1.5cm}}

\begin{document}

\begin{table}
\centering
\caption{Summary statistics\label{A2}}

\small\addtolength{\tabcolsep}{-1pt}
\begin{tabular}{
  @{}
  l
  S[table-format=2.3]
  S[table-format=2.3]
  S[table-format=2.0]
  S[table-format=2.0]
  @{}
 }
\toprule
\textbf{Variable} & {\textbf{Mean}} & {\textbf{Std.\ Dev.}} & {\textbf{Min.}} & {\textbf{Max.}} \\
\midrule
Individual preferences for redistribution (IPR) &  0.132 &  0.339 &  0 &  1 \\
Subjective Freedom (SF)                         &  7.030 &  2.262 &  1 & 10 \\
Fairness (Hard work)                            &  0.239 &  0.426 &  0 &  1 \\
Gender                                          &  0.513 &  0.5   &  0 &  1 \\
Age                                             & 41.199 & 15.829 & 18 & 99 \\
Self-reported income                            &  4.907 &  2.312 &  1 & 10 \\
Low education                                   &  0.27  &  0.444 &  0 &  1 \\
High education                                  &  0.275 &  0.446 &  0 &  1 \\
Single                                          &  0.24  &  0.427 &  0 &  1 \\
Married                                         &  0.564 &  0.496 &  0 &  1 \\
No children                                     &  0.285 &  0.451 &  0 &  1 \\
Size of town                                    &  4.855 &  2.485 &  1 &  8 \\
Household income satisfaction                   &  5.791 &  2.515 &  1 & 10 \\
Chief wage earner                               &  0.495 &  0.5   &  0 &  1 \\
Health status                                   &  3.87  &  0.87  &  1 &  5 \\
\midrule
\itshape Religious service attendance \\
\MyIndent More than once a week                 &  0.148 &  0.355 &  0 &  1 \\ 
\MyIndent Once a week                           &  0.187 &  0.39  &  0 &  1 \\
\MyIndent Once a month                          &  0.114 &  0.318 &  0 &  1 \\ 
\MyIndent Only on special holy days             &  0.165 &  0.371 &  0 &  1 \\ 
\MyIndent Other specific holy days              &  0.002 &  0.041 &  0 &  1 \\ 
\MyIndent Once a year                           &  0.062 &  0.241 &  0 &  1 \\ 
\MyIndent Less often                            &  0.11  &  0.313 &  0 &  1 \\
\midrule
\itshape Religious affiliation \\
\MyIndent Catholics                             &  0.242 &  0.428 &  0 &  1 \\ 
\MyIndent Muslim                                &  0.135 &  0.342 &  0 &  1 \\ 
\MyIndent Protestant                            &  0.112 &  0.315 &  0 &  1 \\ 
\MyIndent Orthodox                              &  0.144 &  0.351 &  0 &  1 \\
\midrule
\itshape Type of job \\ 
\MyIndent Manager                               &  0.042 &  0.2   &  0 &  1 \\ 
\MyIndent Non-manual                            &  0.048 &  0.213 &  0 &  1 \\
\MyIndent Manual                                &  0.17  &  0.376 &  0 &  1 \\
\MyIndent Full-time                             &  0.377 &  0.485 &  0 &  1 \\
\MyIndent Self-employed                         &  0.13  &  0.336 &  0 &  1 \\
\MyIndent Retired                               &  0.125 &  0.33  &  0 &  1 \\
\MyIndent Unemployed                            &  0.09  &  0.286 &  0 &  1 \\
\midrule
\multicolumn{5}{l}{$N=\num{98673}$}\\ 
\bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

Related Question