[Tex/LaTex] Centering numbers in table

horizontal alignmenttables

I would like to generate table which looks approximately (not necessarily) like

enter image description here

If you have any suggestion for this sort of data representation, don't hesitate to tell. So far I have come up with

\documentclass[12pt]{book}

\usepackage{booktabs}
\usepackage{caption}
\usepackage{siunitx}   


\begin{document}

\begin{table}[h!]
  \centering
  \captionsetup
  {
  singlelinecheck = off,
  justification   = raggedright,
  labelfont       = bf,
  }
  \begin{minipage}[b]{1.0\linewidth}    
    \caption{ABC}%
    \begin{tabular}{@{}l SSSSSSSSS }
      \toprule
      & \multicolumn{2}{l}{2010} & \multicolumn{2}{l}{2011} & \multicolumn{2}{l}{2012} & \multicolumn{2}{l}{2013} \\
      \cmidrule{2-9}
    Name    & {bil}~\$ & \% & {bil}~\$ & \% & {bil}~\$ & \%     & {bil}~\$ & \%   \\ 
    \midrule
    {Aaaaaaaaaaaa}  &  20 456 & 1.2 &    20150 & 90.5 & 20150 & 90.5 & 20150 & 90.5    \\
    {Bbbbbbb}       & 5 256 & 21.3  \\  
    {Ccccccc}       & 58 & 0.5 \\
    {Dddd}          & 125 000\\
    {Eeeeeeeeeee}   & \\
    {FFFFFF}        & \\
    {ggggggg}       & \\
    \bottomrule
    $\sum$      & \\
    \bottomrule
    \end{tabular}

    \medskip \noindent Reference: John Johnson
    \label{1111}
  \end{minipage}
\end{table}

\end{document}

which unfortunately results in two problems:

  1. it doesn't center the data entries, nor the columns' names "year", "bil $" and "%" right

  2. table is too wide. Should I decrease font size in the table or is there anything better to do?

EDIT: Another code

\documentclass[12pt]{book}

\usepackage{booktabs}
\usepackage{caption}
\usepackage{siunitx}   


\begin{document}

\begin{tabular}{ccccccccccccccc}

\toprule
\multicolumn{1}{l}{} &
\multicolumn{2}{c}{2010}    &
\multicolumn{2}{c}{2011}    &
\multicolumn{2}{c}{2012}    &
\multicolumn{2}{c}{2013}    \\ 
\cmidrule(lr){2-3}
\cmidrule(lr){4-5}
\cmidrule(lr){6-7}
\cmidrule(lr){8-9}

 &
\multicolumn{1}{c}{bil~\$} &
\multicolumn{1}{c}{\%}     &
\multicolumn{1}{c}{bil~\$} &
\multicolumn{1}{c}{\%}     &
\multicolumn{1}{c}{bil~\$} &
\multicolumn{1}{c}{\%}     &
\multicolumn{1}{c}{bil~\$} &
\multicolumn{1}{c}{\%}      \\
\midrule



{Aaaaaaaaaaaa}  &  20 456 & 1.2  &  20 456 & 1.2 &  20 456 & 1.2 &  20 456 & 1.2   \\
    {Bbbbbbb}       & 5 256 & 21.3 & 5 256 & 21.3& 5 256 & 21.3& 5 256 & 21.3  \\  
    {Ccccccc}       & 58 & 0.5 & 58 & 0.5& 58 & 0.5& 58 & 0.5 \\
    {Dddd}          & 125 000 \\
    {Eeeeeeeeeee}   & \\
    {FFFFFF}        & \\
    {ggggggg}       & \\
\bottomrule
{$\sum$} \\
\bottomrule
\end{tabular}

\end{document}
enter code here

Best Answer

I'm not a pro in this area, but here is a workaround.

  • First of all, to avoid the large width of the table, you can tell siunitx what are those columns supposed to be. *{4}{…} is just to avoid writing four times the same. And S[table-format = 5.0, group-minimum-digits = 3] S[table-format = 2.1] means that the first column has numbers with 5 digits before the comma and none after and the second column two digits before and one after. The group-minimum-digits = 3, as suggested by egreg, tells siunitx to add a space after every three digits (even if the number has only 4 digits).
  • To center the cells you have to enclose them in {} (see siunitx documentation).

Here is the code.

\documentclass{scrbook}

\usepackage{booktabs}
\usepackage{caption}
\usepackage{siunitx}

\begin{document}
\begin{table}[h!]
    \centering
    \captionsetup{
        singlelinecheck = off,
        justification = raggedright,
        labelfont = bf,
    }
    \begin{minipage}[b]{1.0\linewidth}
        \caption{ABC}%
        \begin{tabular}{@{}l *{4}{S[table-format = 5.0, group-minimum-digits = 3] S[table-format = 2.1]} }
            \toprule
            & \multicolumn{2}{c}{2010} & \multicolumn{2}{c}{2011} & \multicolumn{2}{c}{2012} & \multicolumn{2}{c}{2013} \\
            \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}
            Name & {bil \$} & {\%} & {bil \$} & {\%} & {bil \$} & {\%} & {bil \$} & {\%} \\ 
            \midrule
            Aaaaaaaaaaaa & 20 456 & 1.2 & 20150 & 90.5 & 20150 & 90.5 & 20150 & 90.5 \\
            Bbbbbbb & 5256 & 21.3 \\ 
            Ccccccc & 58 & 0.5 \\
            Dddd & 125 000\\
            Eeeeeeeeeee & \\
            FFFFFF & \\
            ggggggg & \\
            \bottomrule
            $\sum$ & \\
            \bottomrule
        \end{tabular}

        \medskip \noindent Reference: John Johnson
        \label{1111}
    \end{minipage}
\end{table}
\end{document}

enter image description here

Related Question