在小數和格式問題上使用千位分隔符號和逗號

在小數和格式問題上使用千位分隔符號和逗號

我確實是 LaTex 的菜鳥,需要一些幫助來格式化下表:

\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}

我不知道為什麼,但使用該dcolumn套件後,某些列名稱以斜體顯示,並且與各自的值不對齊。這是為什麼?

如何在表格中顯示帶有千位分隔符號和小數點逗號的數字?比如說1.000.000,00

另一件事:使用文件所需的頁邊距,該表格與 PDF 頁面不太適合。我如何才能將其居中並使其適合?最終文件將包含許多其他表格,我不太明白使用floating選項的優點/缺點是什麼。

非常感謝您的幫忙!歡迎任何其他意見/建議!

謝謝你!

答案1

要使表格材料適合文字區塊,您應該 (a) 刪除指令@{\extracolsep{4pt}},(b) 使用tabular*環境而不是環境,以及 (c)在啟動環境之前tabular發出指令並在標題中使用指令定義。這樣,您將強制 LaTeX 計算列間空白的最佳可能值。\setlength\tabcolsep{0pt}tabular*@{\extracolsep{\fill}}tabular*

為了使這些建議發揮作用,我建議您使用以下設定:

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

關於數字的格式:不要同時載入dcolumnsiunitx。由於您希望 LaTeX 自動插入千位分隔符,因此我建議您專注於siunitx套件及其S列類型。

完整的 MWE——請注意該times軟體包已被棄用;我建議你加載newtxtextnewtxmath包:

在此輸入影像描述

\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} 

答案2

讓我詳細說明一下艾倫·穆恩評論:

  • 對於你喜歡的東西,可以透過S列類型簡單地實現siunitx
  • 定義\sisetup{...}分組數字的最小數量
  • 加載就足夠siunitx

所以,看看您的 MWE 的以下變化是否為您帶來了您想要的:

\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}

在此輸入影像描述

筆記: 即使我刪除了,您的表格也太寬,無法適應文字寬度extracolsep

相關內容