7

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?

1
  • You could use regex in your favorite text editor to ease the pain to create braces around the terms. Also, you do not have to use S-columns all the time. – henry Jun 4 '15 at 14:18
13

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} 
2
  • 2
    I know it's a faux pas to put a thank you in the comments, but this really is the best answer imaginable. Does exactly what I was hoping for. – LondonRob Jun 4 '15 at 15:02
  • 5
    @LondonRob - You're welcome! (The faux pas, if any, would be to leave verbal praise while not upvoting the answer...) – Mico Jun 4 '15 at 15:04

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.