3

I am really a rookie on LaTex and would like some help with formatting the table below:

\documentclass[12pt]{article}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{times}
\usepackage{dcolumn}
\usepackage[vmargin=3cm, hmargin=2.5cm]{geometry}
\usepackage[group-separator={.}]{siunitx}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage[group-separator={,}]{siunitx}
\begin{document}




\begin{table}[ht]
\centering\setlength\extrarowheight{2pt}
\caption{Estat\'{i}sticas - Contribui\c{c}\~{o}es} 
\begin{tabular}{@{\extracolsep{4pt}}ll*{6}{d{5.2}}}
  \toprule
  {} & {}  & \multicolumn{3}{c}{Mediana}  & \multicolumn{3}{c}{Desvio Padr\~{a}o}\\
  \cmidrule(lr){3-5} 
  \cmidrule(lr){6-8} 
Ano & Vari\'{a}vel & {Controle} & {Tratado} & {Total} & {Controle} & {Tratado} & {Total} \\ 
  \midrule
2012 & Total de receitas & 3413.90 & 8645.19 & 3967.80 & 18077.13 & 94897.57 & 45339.62 \\ 
   & \hspace{4mm}Pessoas f\'{i}sicas & 300.00 & 2500.00 & 570.00 & 6201.29 & 20180.83 & 10763.53 \\ 
   & \hspace{4mm} Empresas & 0.00 & 1000.00 & 0.00 & 0.00 & 27854.24 & 12448.01 \\ 
   & \hspace{4mm} Partidos & 0.00 & 0.00 & 0.00 & 7193.99 & 54075.33 & 24547.63 \\ 
   & \hspace{4mm} Recursos pr\'{o}prios & 1550.00 & 1850.00 & 1598.00 & 5694.75 & 12012.86 & 7382.93 \\ 
   & \hspace{4mm} Outros recursos & 91.88 & 230.00 & 103.13 & 8456.98 & 21893.65 & 12259.07 \\ \\
   & Pessoas f\'{i}sicas (\%) & 12.10 & 30.20 & 16.90 & 31.06 & 28.57 & 30.73 \\ 
   & Empresas (\%) & 0.00 & 14.20 & 0.00 & 0.00 & 24.69 & 14.21 \\ 
   & Partidos (\%) & 0.00 & 0.00 & 0.00 & 9.78 & 9.50 & 9.73 \\ 
   & Recursos pr\'{o}prios (\%) & 57.90 & 22.70 & 49.90 & 36.33 & 28.93 & 36.31 \\ 
   & Outros recursos (\%) & 2.90 & 2.50 & 2.80 & 27.84 & 16.73 & 26.23 \\
   \midrule
  2016 & Total de receitas & 3600.00 & 7106.27 & 4009.00 & 11740.78 & 52605.28 & 25877.30 \\ 
   & \hspace{4mm} Pessoas f\'{i}sicas & 800.00 & 2344.70 & 1000.00 & 6136.54 & 27232.84 & 13392.53 \\ 
   & \hspace{4mm} Empresas & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\ 
   & \hspace{4mm} Partidos & 0.00 & 0.00 & 0.00 & 4343.52 & 28402.71 & 13027.55 \\ 
   & \hspace{4mm} Recursos pr\'{o}prios & 1695.00 & 3000.00 & 1857.72 & 6576.61 & 20169.18 & 10796.12 \\ 
   & \hspace{4mm} Outros recursos & 37.50 & 88.00 & 44.80 & 1544.50 & 6476.34 & 3160.15 \\ \\
   & Pessoas f\'{i}sicas (\%) & 26.70 & 37.30 & 28.90 & 31.18 & 31.46 & 31.35 \\ 
   & Empresas (\%) & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\ 
   & Partidos (\%) & 0.00 & 0.00 & 0.00 & 13.50 & 13.18 & 13.44 \\ 
   & Recursos pr\'{o}prios (\%) & 56.00 & 48.80 & 54.60 & 33.71 & 32.59 & 33.54 \\ 
   & Outros recursos (\%) & 0.70 & 0.80 & 0.80 & 20.21 & 16.07 & 19.51 \\
   \bottomrule

\end{tabular}
\end{table}

\end{document}

I dont know why but after using the dcolumn package some of the column names were displayed on italic and were not aligned to their respective values. Why is that and how can it be fixed?

How can I display the numbers on the table with thousands separators and commas on the decimals? Like 1.000.000,00 for instance.

Another thing: The table is not fitting well on the PDF page using the margins that are required for the document. How can I center it and make it fit? The final document will contain a lot of other tables and I do not really understand what are the pros/cons of using floating options.

Really appreciate your help! Any other comments/suggestions are welcome!

Thank you!

  • 3
    Since you're using siunitx you should use the S column not the d column for the alignment. Try using an S column instead and adding \sisetup{ table-format = 5.2,group-separator={.},output-decimal-marker={,}} . – Alan Munn Jul 13 '17 at 6:28
5

To make the tabular material fit inside the text block, you should (a) remove the @{\extracolsep{4pt}} directives, (b) use a tabular* environment instead of tabular environment, and (c) issue the instruction \setlength\tabcolsep{0pt} before starting the tabular* environment and use the directive @{\extracolsep{\fill}} inside the header of the tabular* definition. That way, you'll force LaTeX calculate the best possible value of the intercolumn whitespace.

Putting these suggestions to work, I suggest you use the following setup:

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} 
     ll *{3}{S[table-format=4.2]} *{3}{S[table-format=5.2]} }

With regard to the formatting of the numbers: Don't load both dcolumn and siunitx. Since you wish to have LaTeX insert thousands-separators automatically, I suggest you concentrate on the siunitx package and its S column type.

The full MWE -- do note that the times package is deprecated; I suggest you load the newtxtext and newtxmath packages:

enter image description here

\documentclass[12pt]{article}
\usepackage{booktabs}
\usepackage[skip=0.333\baselineskip]{caption} % <-- new
\usepackage{newtxtext,newtxmath} % <-- new
\usepackage[vmargin=3cm, hmargin=2.5cm]{geometry}
\usepackage[group-separator={.},
            group-four-digits,
            output-decimal-marker={,}]{siunitx}
\begin{document}
\begin{table}[ht]
\setlength\tabcolsep{0pt} % <-- new
\setlength\extrarowheight{2pt}
\caption{Estat\'{i}sticas -- Contribui\c{c}\~{o}es}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}         % <-- new
   ll *{3}{S[table-format=4.2]} *{3}{S[table-format=5.2]} } % <-- new
  \toprule
  & & \multicolumn{3}{c}{Mediana} & \multicolumn{3}{c}{Desvio Padr\~{a}o}\\
  \cmidrule{3-5} \cmidrule{6-8}
Ano & Vari\'{a}vel & {Controle} & {Tratado} & {Total} & {Controle} & {Tratado} & {Total} \\
  \midrule
2012 & Total de receitas & 3413.90 & 8645.19 & 3967.80 & 18077.13 & 94897.57 & 45339.62 \\
   & \hspace{4mm}Pessoas f\'{i}sicas & 300.00 & 2500.00 & 570.00 & 6201.29 & 20180.83 & 10763.53 \\
   & \hspace{4mm}Empresas & 0.00 & 1000.00 & 0.00 & 0.00 & 27854.24 & 12448.01 \\
   & \hspace{4mm}Partidos & 0.00 & 0.00 & 0.00 & 7193.99 & 54075.33 & 24547.63 \\
   & \hspace{4mm}Recursos pr\'{o}prios & 1550.00 & 1850.00 & 1598.00 & 5694.75 & 12012.86 & 7382.93 \\
   & \hspace{4mm}Outros recursos & 91.88 & 230.00 & 103.13 & 8456.98 & 21893.65 & 12259.07 \\ 
   \addlinespace
   & Pessoas f\'{i}sicas (\%) & 12.10 & 30.20 & 16.90 & 31.06 & 28.57 & 30.73 \\
   & Empresas (\%) & 0.00 & 14.20 & 0.00 & 0.00 & 24.69 & 14.21 \\
   & Partidos (\%) & 0.00 & 0.00 & 0.00 & 9.78 & 9.50 & 9.73 \\
   & Recursos pr\'{o}prios (\%) & 57.90 & 22.70 & 49.90 & 36.33 & 28.93 & 36.31 \\
   & Outros recursos (\%) & 2.90 & 2.50 & 2.80 & 27.84 & 16.73 & 26.23 \\
   \midrule
  2016 & Total de receitas & 3600.00 & 7106.27 & 4009.00 & 11740.78 & 52605.28 & 25877.30 \\
   & \hspace{4mm}Pessoas f\'{i}sicas & 800.00 & 2344.70 & 1000.00 & 6136.54 & 27232.84 & 13392.53 \\
   & \hspace{4mm}Empresas & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
   & \hspace{4mm}Partidos & 0.00 & 0.00 & 0.00 & 4343.52 & 28402.71 & 13027.55 \\
   & \hspace{4mm}Recursos pr\'{o}prios & 1695.00 & 3000.00 & 1857.72 & 6576.61 & 20169.18 & 10796.12 \\
   & \hspace{4mm}Outros recursos & 37.50 & 88.00 & 44.80 & 1544.50 & 6476.34 & 3160.15 \\ 
   \addlinespace
   & Pessoas f\'{i}sicas (\%) & 26.70 & 37.30 & 28.90 & 31.18 & 31.46 & 31.35 \\
   & Empresas (\%) & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
   & Partidos (\%) & 0.00 & 0.00 & 0.00 & 13.50 & 13.18 & 13.44 \\
   & Recursos pr\'{o}prios (\%) & 56.00 & 48.80 & 54.60 & 33.71 & 32.59 & 33.54 \\
   & Outros recursos (\%) & 0.70 & 0.80 & 0.80 & 20.21 & 16.07 & 19.51 \\
   \bottomrule
\end{tabular*} % <-- new
\end{table}

\end{document} 
  • Thank you! How can I center the table on the page?: – Arthur Carvalho Brito Jul 13 '17 at 15:29
  • 1
    @ArthurCarvalhoBrito - The tabular* environment is defined to take up the entire width of the text block -- there's nothing left to "center" (horizontally, that is). – Mico Jul 13 '17 at 15:40
2

Let me elaborate AllanMunn comment:

  • for what you like is simple achievable with S column type from siunitx
  • in \sisetup{...} define minimum number for grupoing digits
  • it is sufficient to load siunitx ones

So, see, if the following changes of your MWE, gives, what you like to have:

\documentclass[12pt]{article}
\usepackage[vmargin=3cm, hmargin=2.5cm]{geometry}
\usepackage{times}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{caption}

\begin{document}
    \begin{table}[ht]
% \small
    \centering
    \setlength\extrarowheight{2pt}
\sisetup{table-format = 5.2,group-separator={.},
         group-separator={.},
         output-decimal-marker={,},
         group-four-digits}
\caption{Estat\'{i}sticas - Contribui\c{c}\~{o}es}
\begin{tabular}{ll*{6}{S}}
  \toprule
...
   \bottomrule
\end{tabular}
    \end{table}
\end{document}

enter image description here

Note: Your table is to wide to fit in text width, even if I remove extracolsep.

  • Removing the directive @{\extracolsep{4pt}} may not suffice to make the tabular material fit inside the available textblock. – Mico Jul 13 '17 at 8:29
  • @Mico, I say the same. However, I didn't understand, that the table width is OP concern, so I didn't do anything beside this comment about this :( – Zarko Jul 13 '17 at 8:49
  • How is it too wide? – Arthur Carvalho Brito Jul 13 '17 at 15:28
  • @ArthurCarvalhoBrito, your table spill-out of right text area border. to see this, add option showframe to geometry. A good workaround you find in Mico answer. – Zarko Jul 13 '17 at 16:07

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.