[Tex/LaTex] \usepackage[group-separator={,}]{siunitx} Not working with longtable

formattinglongtablesiunitxtables

I have a table with very long counts (in the millions), so I would like to add commas. I have tried to use siunitx in a variety of ways in the preamble:

  1. \usepackage[group-separator={,}]{siunitx}
  2. \usepackage{siunitx}
    \sisetup{group-separator={,},group-minimum-digits={3},output-decimal-marker={.}}
  3. \sisetup{
    group-digits=true,
    group-separator={\,},
    }
    \pgfkeys{/pgf/number format/.cd, set thousands separator={\,}}%

    But none of these insert the commas. Could the problem be that I am using longtable? When I specify \number{} around each table item, the commas are inserted, but there are too many tables to do this manually.

Here is an example:

\documentclass[12pt]{article} 
\usepackage[group-separator={,}]{siunitx}
\usepackage{lscape,longtable}
\begin{document}
\begin{center}
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{longtable}{l*{3}{cc}}
\hline\hline\endfirsthead\hline\endhead\hline\endfoot\endlastfoot
            &\multicolumn{2}{c}{Managed Care}&\multicolumn{2}{c}{No Managed 1Care}&\multicolumn{2}{c}{Total}\\
            &       Count&          \%&       Count&          \%&       Count&          \%\\
Adult       &     7178237&        23.0&     6715828&        37.5&    13894065&        28.3\\
Aged        &     1278137&        49.5&     3482900&        58.3&     4761037&        55.7\\
Child       &    20562118&        60.9&     7015119&        29.4&    27577237&        47.8\\
\end{longtable}
}
\end{center}
\end{document}

Best Answer

The columns should be specified as S-type, with a table-format setting telling the number of digits: 8 means eight digits in the integer part, no decimal part; 2.1 means two digits in the integer part and one in the decimal part.

With booktabs the table will be better; you probably want to repeat the header after a page break.

Note that there's no need to use a center environment around a longtable.

\documentclass{article}
\usepackage[group-separator={,}]{siunitx}
\usepackage{longtable,booktabs}
\begin{document}

\begin{longtable}{l*{3}{S[table-format=8.0]S[table-format=2.1]}}
\toprule
 &\multicolumn{2}{c}{Managed Care}
 &\multicolumn{2}{c}{No Managed 1Care}
 &\multicolumn{2}{c}{Total}
\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}
 & Count & \% & Count & \% & Count & \% \\
\midrule
\endhead
\bottomrule
\endfoot
\bottomrule
\endlastfoot
Adult &  7178237 & 23.0 & 6715828 & 37.5 & 13894065 & 28.3 \\
Aged  &  1278137 & 49.5 & 3482900 & 58.3 &  4761037 & 55.7 \\
Child & 20562118 & 60.9 & 7015119 & 29.4 & 27577237 & 47.8 \\
\end{longtable}

\end{document}

enter image description here

Related Question