# using the package 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

• Use S within the column specification you pass to tabular. Don't add an additional specification as the environment does not expect one.
– cfr
Jan 1, 2016 at 23:40
• sorry...i am not sure i understood what you mean. Jan 2, 2016 at 0:00
• You wrote {S[S[tabnumalign=center,tabformat=3.2]}, that's the double specification cfr is referring to, I think. Jan 2, 2016 at 0:12
• @Alenanno I more meant the fact that there's \begin{tabular}{...}{...} rather than \begin{tabular}{...} because the S is being given in addition to the standard column specs rather than replacing them.
– cfr
Jan 2, 2016 at 0:25
• @cfr Ah, didn't notice that. :P I'll blame the late hour. Jan 2, 2016 at 0:30

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}}

\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}


The following changes are sufficient for compilation but the result is not at all ideal. The initial fix involves:

• using S and S[<options>] in place of the appropriate c c columns;
• adding an option to the package for backwards compatibility so that the specific options chosen work.

That is:

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


and

\usepackage[version-1-compatibility]{siunitx}


As it is, the table is too wide for the page. Moreover, as booktabs' manual explains, the default configuration does not create professional-looking tables. For that, you need to follow some general guidelines and use some enhanced versions of LaTeX's commands and environments. Typically, this would mean more vertical space, rules of variable widths rather than squished doubling of monowidth rules etc. It would be good if we could also use the updated syntax for siunitx and avoid the need for explicit formatting in the caption specification.

First, we can remove

\captionsetup{labelfont=bf}


since this just repeats the argument passed to the package. Second, we can add

font=bf


to make the content of the caption bold as well, avoiding the need for explicit markup.

So we'll have

\usepackage[labelfont=bf,font=bf]{caption}


and then

\caption{Summary statistics \label{A2}}


\usepackage{booktabs}


and see if its rules can improve the appearance of the table.

\toprule


specifies a heavier rule intended for use at the top and likewise

\bottomrule


at the bottom.

\midrule


should separate our column headers from the main body of the table.

So now we have

\begin{table}
\centering
\caption{Summary statistics \label{A2}}
\begin{tabular}{l S S[table-number-alignment=center,table-format=3.2] c c}
\toprule
\multicolumn{1}{l}{\textbf{Variable}} & \textbf{Mean} & \textbf{Std. Dev.}& \textbf{Min.} &  \textbf{Max.} \\
\midrule
...
\bottomrule
\end{tabular}
\end{table}


Personally, I would also remove the additional vertical spacing. If you really want it, I'd use something like \\[\smallskipamount] or \\[2\smallskipamount] but it is important to maintain consistency: it should be clear to your reader what a difference in formatting indicates.

The final line of the table looks very strange. I'd recommend putting this information in the caption instead or, if your table will include notes, you could include it there.

For the third column of the table, we currently have

S[tabnumalign=center,tabformat=3.2]


Let's start by updating to the new syntax so we can jettison backwards-compatibility mode:

S[table-number-alignment=center,table-format=3.2]


This isn't quite right, though. Our column has at most a two-digit integer and at most three decimal places, but we've requested space for a three-digit integer with at most two decimal places, so we need to change it

S[table-number-alignment=center,table-format=2.3]


In fact, we should probably use the same settings for the previous S column as well, replacing

S S[table-number-alignment=center,table-format=2.3]


with

*{2}{S[table-number-alignment=center,table-format=2.3]}


We also need more room in the page. The easiest way to do this is to use geometry to let us use a larger proportion of the paper for the text block. We don't need much more but

\usepackage[scale=.75]{geometry}


will do the trick.

The result of the above changes looks like this

Complete code:

\documentclass{article}
\usepackage[scale=.75]{geometry}
\newcommand*{\MyIndent}{\hspace*{1.5cm}}
\usepackage{siunitx,booktabs}
\usepackage[labelfont=bf,font=bf]{caption}
\begin{document}
\begin{table}
\centering
\caption{Summary statistics (N=98673) \label{A2}}
\begin{tabular}{l *{2}{S[table-number-alignment=center,table-format=2.3]} c c}
\toprule
\multicolumn{1}{l}{\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 \\
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 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

• thanks a lot guys for point out all the problems in my code. all the best. Jan 2, 2016 at 0:23
• @Dario I just gave it a further tweak, but I think I'm done now. Hope it helps.
– cfr
Jan 2, 2016 at 0:25

Your MWE is not possible to compile ... so I correct all errors (in using siunitx), remove to strange solution to increase vertical space between rows and repalce them with \renewcommand{\arraystretch}{1.1}. Also I introduce package booktabs and from them use rules. Except for the first column, I use S type column in all others columns. After this, your table looks as you can see below:

And MWE:

\documentclass{article}
%---------------------------------------------------------------%
\newcommand*{\MyIndent}
{\hspace*{1.5cm}}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage[labelfont=bf]{caption}

\begin{document}
\begin{table}[h]
\centering
\renewcommand{\arraystretch}{1.1}
\caption{\textbf{Summary statistics \label{A2}}}
\begin{tabular}{l*{2}{S[table-format=3.4]}*{2}{S[table-format=2]}}
\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 \\
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 \\
\midrule
\multicolumn{1}{c}{$N$} & \multicolumn{4}{c}{98673}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}


Edit: nicer looks of table is obtained with determining separate table format for last two columns, so I introduce this now.