# Problems with column width and text alignment using siunitx

I have problems with formatting a large table with summary statistics of variables. My variables have long names, which I would like to keep, but if there is no way, I can make shortcuts of their names. The problem is that I would like to align numbers by decimal points and I would like also the table to have the same space between columns and the names of the columns nicely centered above the column. I somehow managed to get the alignment by decimal point using S columns, but still the table does not look good, because the column headers do not seem to be just in the middle above the numbers of each column (e.g. columns median and st.dev), the text in column headers seem to be shifted to the right as compared to numbers. Also, the space between the last column and column "median" is apparently bigger than the space between any other columns, so I would like to reduce it if that's possible and I would like to increase space between the first "Variable" column and the second column. I think the problem is I don't know what does the numbers in S column option table-format=.... stand for: in different number alignment questions, they suggest to use different table-format but they never say why table-format=3.2 or table-format=1.5, for example, so I am confused which one I should use and why.... Below is my code:

\documentclass[a4paper,11pt]{article}
\usepackage[cp1250]{inputenc}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{amsmath, amsthm, amssymb}
\usepackage{booktabs}
\usepackage[
singlelinecheck=false % <-- important
]{caption}
\usepackage{subcaption}
\usepackage[detect-all]{siunitx}
\usepackage{multirow}

\usepackage{epstopdf}
\usepackage{rotating}
\usepackage{fullpage}
\usepackage{enumerate}
\usepackage{setspace}
\usepackage{authblk}
\usepackage[round]{natbib}
\usepackage{natbib}
\usepackage{hyperref}
\hypersetup{pdfstartview={XYZ null null 1.00},bookmarksnumbered,hypertexnames=false}

\setlength\parindent{34pt}
\onehalfspacing

\begin{document}

\newcommand*{\SuperScriptSameStyle}[1]{%
\ensuremath{%
\mathchoice
{{}^{\displaystyle #1}}%
{{}^{\textstyle #1}}%
{{}^{\scriptstyle #1}}%
{{}^{\scriptscriptstyle #1}}%
}%
}

\newcommand*{\one}{\SuperScriptSameStyle{*}}
\newcommand*{\two}{\SuperScriptSameStyle{**}}
\newcommand*{\three}{\SuperScriptSameStyle{*{*}*}}

\begin{table}
\begin{footnotesize}
\captionsetup{font=footnotesize}
\begin{center}
\begin{tabular}{
@{}l  *{6}{S[table-format=1.5,table-space-text-post=*,table-align-text-post=false,group-digits=false,table-number-alignment=center]}@{}}
\toprule
\multirow{2}{*}{\textbf{Variable}} & \multirow{2}{*}{\textbf{mean}} & \multirow{2}{*}{\textbf{st.dev}} & \multirow{2}{*}{\textbf{min}} & \multirow{2}{*}{\textbf{max}} & \multirow{2}{*}{\textbf{median}} &  {\textbf{Jarque - Bera}} \\
&      &       &       &     &   &  {\textbf{statistic}}      \\
\midrule
Variable 1 that has a long name      & -2.198      &  0.948        &  -4.273     &   2.356    & -1.085     &   0.989\one    \\[1pt]
Variable 2 that has a long name      & -4.140      &  4.432        &  -16.019    &   6.289    & -2.846     &   1.919\three  \\[1pt]
Variable 3 that has a long name      & -0.899      &  0.592        &  -3.980     &   1.320    & -5.397     &   2.916\two    \\[1pt]
Variable 4 that has a long name      &  2.654      &  2.924        &  -1.482     &   9.295    &  1.992     &   0.878\three  \\[1pt]
Variable 5 that has a long name      &  -3.412     &  1.422        &  -5.768     &   8.929    & -0.813     &   0.898\two    \\[1pt]
Variable 6 that has a long name      & -0.250      &  4.382        &  -11.357    &   7.473    & -0.190     &   0.747\three  \\[1pt]
Variable 7 that has a long name      & -0.308      &  0.809        &  -2.726     &   4.120    & -0.243     &   4.937\one    \\[1pt]
Variable 8 that has a long name      & -3.741      &  11.595       &  -24.379    &   28.456   & -3.782     &   0.943\three  \\[1pt]
Variable 9 that has a long name      & -1.152      &  1.469        &  -9.872     &   5.253    & -0.217     &   0.991\two    \\[1pt]
Variable 10 that has a long name     & -0.409      &  2.580        &  -13.600    &   11.498   & -0.565     &   0.896\three  \\[1pt]
Variable 11 that has a long name     & 0.018       &  2.811        &  -4.874     &   5.996    & -5.003     &   1.997        \\[1pt]
Variable 12 that has a long name     & 0.772       &  1.496        &  -3.728     &   14.362   & 0.213      &  0.539\three   \\[1pt]
\bottomrule
$N = 220$ & & & & & &  \\
\bottomrule
\end{tabular}
\end{center}
\vspace{-10pt}

\caption{Sample table}
\label{}
\end{footnotesize}
\end{table}

\end{document}


which produces the following table:

Could anyone please help me?

• BTW, the second last \bottomrule should rather be a \midrule (because of vertical spacing and line thickness). – Heiko Oberdiek Dec 9 '17 at 14:09
• Which font is used for the variable names? – Heiko Oberdiek Dec 9 '17 at 14:13

## 2 Answers

Here is a solution I could come up with:

\documentclass[a4paper,11pt]{article}
\usepackage[cp1250]{inputenc}
\usepackage{setspace}

\usepackage{booktabs}
\usepackage{makecell}
\usepackage[singlelinecheck=false]{caption}

\usepackage[detect-all]{siunitx}
\usepackage{fullpage}

\setlength\parindent{34pt}
\onehalfspacing
\usepackage{showframe}

\newcommand*{\SuperScriptSameStyle}[1]{%
\ensuremath{%
\mathchoice
{{}^{\displaystyle #1}}%
{{}^{\textstyle #1}}%
{{}^{\scriptstyle #1}}%
{{}^{\scriptscriptstyle #1}}%
}%
}

\newcommand*{\one}{\SuperScriptSameStyle{*}}
\newcommand*{\two}{\SuperScriptSameStyle{**}}
\newcommand*{\three}{\SuperScriptSameStyle{*{*}*}}

\begin{document}

\begin{table}
\begin{footnotesize}
\captionsetup{font=footnotesize}
\begin{center}
\begin{tabular}{
@{}l  S[table-format=-1.3]S[table-format=2.3]S[table-format=-2.3]S[table-format=2.3]S[table-format=-1.3]S[table-format=1.6,table-align-text-post=false]@{}}
\toprule
{\textbf{Variable}} & {\textbf{mean}}   &  {\textbf{st.dev}}     &   {\textbf{min}}   & {\textbf{max}}   & {\textbf{median}}  &  {\makecell{\textbf{Jarque-Bera} \\ \textbf{statistics}}}      \\
\midrule
Variable 1 that has a long name      & -2.198      &  0.948        &  -4.273     &   2.356    & -1.085     &   0.989\one    \\[1pt]
Variable 2 that has a long name      & -4.140      &  4.432        &  -16.019    &   6.289    & -2.846     &   1.919\three  \\[1pt]
Variable 3 that has a long name      & -0.899      &  0.592        &  -3.980     &   1.320    & -5.397     &   2.916\two \\[1pt]
Variable 4 that has a long name      &  2.654      &  2.924        &  -1.482     &   9.295    &  1.992     &   0.878\three  \\[1pt]
Variable 5 that has a long name      &  -3.412     &  1.422        &  -5.768     &   8.929    & -0.813     &   0.898\two    \\[1pt]
Variable 6 that has a long name      & -0.250      &  4.382        &  -11.357    &   7.473    & -0.190     &   0.747\three  \\[1pt]
Variable 7 that has a long name      & -0.308      &  0.809        &  -2.726     &   4.120    & -0.243     &   4.937\one    \\[1pt]
Variable 8 that has a long name      & -3.741      &  11.595       &  -24.379    &   28.456   & -3.782     &   0.943\three  \\[1pt]
Variable 9 that has a long name      & -1.152      &  1.469        &  -9.872     &   5.253    & -0.217     &   0.991\two    \\[1pt]
Variable 10 that has a long name     & -0.409      &  2.580        &  -13.600    &   11.498   & -0.565     &   0.896\three  \\[1pt]
Variable 11 that has a long name     & 0.018       &  2.811        &  -4.874     &   5.996    & -5.003     &   1.997        \\[1pt]
Variable 12 that has a long name     & 0.772       &  1.496        &  -3.728     &   14.362   & 0.213      &  0.539\three   \\[1pt]
\midrule
$N = 220$ & & & & & &  \\
\bottomrule
\end{tabular}
\end{center}
\vspace{-10pt}

\caption{Sample table}
\label{}
\end{footnotesize}
\end{table}

\end{document}


In the above example, I have replaced the multirows by \makecell. Additionally, I have changed the table-format of the S cells for every column individually, according to its contents. For the 'mean' colum I have for example chosen -1.3, because you have entries with minus signs, that have a maximum of one digit before the decimal separator and a maximum of three digits after the separator.

The above MWE results in the following output: (Please note, that the vertical lines on both sides of the image indicate the textwidth. They are produced by \usepackage{showframe}).

And here is an alternative method that gets rid of the footnotesized text.

\documentclass[a4paper,11pt]{article}
\usepackage[cp1250]{inputenc}
\usepackage{setspace}

\usepackage{booktabs}
\usepackage{makecell}
\usepackage{tabularx}
\usepackage[
singlelinecheck=false % <-- important
]{caption}

\usepackage[detect-all]{siunitx}
\usepackage{fullpage}

\setlength\parindent{34pt}
\onehalfspacing
\usepackage{showframe}

\newcommand*{\SuperScriptSameStyle}[1]{%
\ensuremath{%
\mathchoice
{{}^{\displaystyle #1}}%
{{}^{\textstyle #1}}%
{{}^{\scriptstyle #1}}%
{{}^{\scriptscriptstyle #1}}%
}%
}

\newcommand*{\one}{\SuperScriptSameStyle{*}}
\newcommand*{\two}{\SuperScriptSameStyle{**}}
\newcommand*{\three}{\SuperScriptSameStyle{*{*}*}}

\begin{document}

\begin{table}
\captionsetup{font=footnotesize}
\begin{center}
\begin{tabularx}{\textwidth}{
@{}X  S[table-format=-1.3]S[table-format=2.3]S[table-format=-2.3]S[table-format=2.3]S[table-format=-1.3]S[table-format=1.6,table-align-text-post=false]@{}}
\toprule
{\textbf{Variable}} & {\textbf{mean}}   &  {\textbf{st.dev}}     &   {\textbf{min}}   & {\textbf{max}}   & {\textbf{median}}  &  {\makecell{\textbf{Jarque-Bera} \\ \textbf{statistics}}}      \\
\midrule
Variable 1 that has a long name      & -2.198      &  0.948        &  -4.273     &   2.356    & -1.085     &   0.989\one    \\[1pt]
Variable 2 that has a long name      & -4.140      &  4.432        &  -16.019    &   6.289    & -2.846     &   1.919\three  \\[1pt]
Variable 3 that has a long name      & -0.899      &  0.592        &  -3.980     &   1.320    & -5.397     &   2.916\two    \\[1pt]
Variable 4 that has a long name      &  2.654      &  2.924        &  -1.482     &   9.295    &  1.992     &   0.878\three  \\[1pt]
Variable 5 that has a long name      &  -3.412     &  1.422        &  -5.768     &   8.929    & -0.813     &   0.898\two    \\[1pt]
Variable 6 that has a long name      & -0.250      &  4.382        &  -11.357    &   7.473    & -0.190     &   0.747\three  \\[1pt]
Variable 7 that has a long name      & -0.308      &  0.809        &  -2.726     &   4.120    & -0.243     &   4.937\one    \\[1pt]
Variable 8 that has a long name      & -3.741      &  11.595       &  -24.379    &   28.456   & -3.782     &   0.943\three  \\[1pt]
Variable 9 that has a long name      & -1.152      &  1.469        &  -9.872     &   5.253    & -0.217     &   0.991\two    \\[1pt]
Variable 10 that has a long name     & -0.409      &  2.580        &  -13.600    &   11.498   & -0.565     &   0.896\three  \\[1pt]
Variable 11 that has a long name     & 0.018       &  2.811        &  -4.874     &   5.996    & -5.003     &   1.997        \\[1pt]
Variable 12 that has a long name     & 0.772       &  1.496        &  -3.728     &   14.362   & 0.213      &  0.539\three   \\[1pt]
\midrule
$N = 220$ & & & & & &  \\
\bottomrule
\end{tabularx}
\end{center}
\vspace{-10pt}

\caption{Sample table}
\label{}
\end{table}

\end{document}


This approach uses the same specification for the S columns as described before. To get rid of the footnotesized text, yet still fit the table into the textwidth, I have used the variable width X column of the tabularx package.

The following images shows the output of the alternative approach:

• This is amazing! Thank you very much! Now my table looks so much better and it was simple for me to orientate in your code solution. – doremi Dec 9 '17 at 14:50

\documentclass{article} \begin{document}

\begin{table}

\begin{tabular}{|p{4cm}|p{1.5cm}|p{1cm}|p{1.5cm}|p{1cm}|p{1cm}|p{1cm}|}\hline Variable & Mean & St.Dev & Min & Max & Median & Jaqua Berra Statistics\\hline

Variable 1 that has a long name & -2.198 & 0.948 & -4.273 & 2.356 & -1.085 & 0.989${}^{}$ \ Variable 2 that has a long name & -4.140 & 4.432 & -16.019 & 6.289 & -2.846 & 1.919${}^{}$ \ Variable 3 that has a long name & -0.899 & 0.592 & -3.980 & 1.320 & -5.397 & 2.916${}^{}$ \ Variable 4 that has a long name & 2.654 & 2.924 & -1.482 & 9.295 & 1.992 & 0.878${}^{}$ \ Variable 5 that has a long name & -3.412 & 1.422 & -5.768 & 8.929 & -0.813 & 0.898${}^{}$ \ Variable 6 that has a long name & -0.250 & 4.382 & -11.357 & 7.473 & -0.190 & 0.747${}^{}$ \ Variable 7 that has a long name & -0.308 & 0.809 & -2.726 & 4.120 & -0.243 & 4.937${}^{}$ \ Variable 8 that has a long name & -3.741 & 11.595 & -24.379 & 28.456 & -3.782 & 0.943${}^{}$ \ Variable 9 that has a long name & -1.152 & 1.469 & -9.872 & 5.253 & -0.217 & 0.991${}^{}$ \ Variable 10 that has a long name & -0.409 & 2.580 & -13.600 & 11.498 & -0.565 & 0.896${}^{*}$ \ Variable 11 that has a long name & 0.018 & 2.811 & -4.874 & 5.996 & -5.003 & 1.997 \ Variable 12 that has a long name & 0.772 & 1.496 & -3.728 & 14.362 & 0.213 & 0.539${}^{***}$ \\hline &&&&&&\ N = 220 & & & & & & \\hline \end{tabular} \caption{Sample Table} \end{table}

\end{document}

This way I have done. It is simple and you do not extra packages.