[Tex/LaTex] Center-set column headers in siunitx S columns

siunitxtables

I'm using the siunitx package to automatically put thousand separators in all my tabular environments.

It works fine, but not if you have a column header. This seemingly innocuous MWE fails to compile with an utterly bizarre error:

\documentclass{article}
\usepackage[group-separator={,}]{siunitx} 
% Adds the S column for formatting numbers like 10,000

\begin{document}
\begin{tabular}{llSS}
&     & Trade Value &  Estimation Error \\
&     & (\$US M) &  (\$US M) \\
USA & GBR &    15574 & -5753 \\
GBR & USA &    15668 & -4127 \\
USA & JPN &     4261 &  3092 \\
GBR & LUX &     1590 &  2766 \\
\end{tabular}
\end{document}

I've gleaned from questions like this that the answer is to do multicolumn{1}{c}{My Header}.

This seems like a bore, given all my tables have headers.

The alternatives all seem to involve putting every number into an environment like \num. This is clearly no solution at all, if your tables are large.

Is there a package which will put thousand separators into my tabulars without the need for either the multicolumn headers or the \num{} around every number in the table?

Best Answer

Centering the material in the header cells of a column of type S may be achieved easily by encasing the material in curly braces. For sure, typing {...} is a lot quicker than \multicolumn{1}{c}{...} is, right?

In order to properly center the numeric material in columns three and four, you may want to replace the basic S declarations with S[table-format=5.0] and S[table-format=-4.0], respectively. Finally, I'd add the option group-minimum-digits=4 so that the use of , as the thousands-separator applies to all numbers in the table.

enter image description here

\documentclass{article}
\usepackage[group-separator={,},
            group-minimum-digits = 4]  % default is 5
            {siunitx} % Adds the S column type

\begin{document}

\begin{tabular}{ll S[table-format=5.0] S[table-format=-4.0]}
&     & {Trade Value} &  {Estimation Error} \\
&     & {(\$US M)} &  {(\$US M)} \\[1ex]
USA & GBR &    15574 & -5753 \\
GBR & USA &    15668 & -4127 \\
USA & JPN &     4261 &  3092 \\
GBR & LUX &     1590 &  2766 \\
\end{tabular}

\end{document} 
Related Question