Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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

share|improve this answer
    
Probably, in such a case, also 5256 should have the separator after the third digit from the right. –  egreg Jan 31 at 23:47
    
@egreg I don't know how to fix that apart from manually. –  Manuel Jan 31 at 23:52
    
@Manuel: It does look good. However, when I use for example {Aaaaaaaaa aaaaaaaaa aaaaaaaaa aa} instead of {Aaaaaaaaaaaa}, the table is still wide (I should have mentioned that in the original post). Probably the easiest way to repair this is changing font size. Any idea on that?___ Speaking of 5256, ~ as an invisible character doesn't work. –  Vochmelka Jan 31 at 23:53
1  
@Vochmelka then instead of using l column you can use p{3cm}. –  Manuel Jan 31 at 23:56
1  
group-minimum-digits=3 in the specification for the column should do. –  egreg Jan 31 at 23:56
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.