[Tex/LaTex] Using thousands separator and comma on decimals and formatting issues

dcolumnfloatsformattingsiunitxtables

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!

Best Answer

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}