使用 siunitx 包

使用 siunitx 包

我正在尝试对齐表中的十进制数。我想使用 siunitx 包。我写的代码是:

\documentclass{article}
\newcommand*{\MyIndent}
{\hspace*{1.5cm}}
\usepackage{siunitx}
\usepackage[labelfont=bf]{caption}
\begin{document}
\captionsetup{labelfont=bf}
\begin{table}
\centering
\caption{\textbf{Summary statistics \label{A2}}}
\begin{tabular}{l c c c c }{S[S[tabnumalign=center,tabformat=3.2]}\hline\hline
\multicolumn{1}{l}{\textbf{Variable}} & \textbf{Mean}
& \textbf{Std. Dev.}& \textbf{Min.} &  \textbf{Max.} \\ \hline\noalign{\medskip} \smallskip
Individual preferences for redistribution (IPR) & 0.132 & 0.339 & 0 & 1 \\ \smallskip
Subjective Freedom (SF) & 7.030 & 2.262 & 1 & 10 \\ \smallskip
Fairness (Hard work) & 0.239 & 0.426 & 0 & 1 \\ \smallskip
Gender & 0.513 & 0.5 & 0 & 1 \\ \smallskip
Age & 41.199 & 15.829 & 18 & 99 \\ \smallskip
Self-reported income & 4.907 & 2.312 & 1 & 10 \\ \smallskip
Low education & 0.27 & 0.444 & 0 & 1 \\ \smallskip
High education & 0.275 & 0.446 & 0 & 1 \\ \smallskip
Single & 0.24 & 0.427 & 0 & 1 \\ \smallskip
Married & 0.564 & 0.496 & 0 & 1 \\ \smallskip
No children & 0.285 & 0.451 & 0 & 1 \\ \smallskip
Size of town & 4.855 & 2.485 & 1 & 8 \\ \smallskip
Household income satisfaction & 5.791 & 2.515 & 1 & 10 \\ \smallskip
Chief wage earner & 0.495 & 0.5 & 0 & 1 \\ \smallskip
Health status & 3.87 & 0.87 & 1 & 5 \\ \smallskip \smallskip
Religious service attendance & & & & \\ 
\MyIndent More than once a week & 0.148 & 0.355 & 0 & 1 \\ 
\MyIndent Once a week & 0.187 & 0.39 & 0 & 1 \\
\MyIndent Once a month & 0.114 & 0.318 & 0 & 1 \\ 
\MyIndent Only on special holy days & 0.165 & 0.371 & 0 & 1 \\ 
\MyIndent Other specific holy days & 0.002 & 0.041 & 0 & 1 \\ 
\MyIndent Once a year & 0.062 & 0.241 & 0 & 1 \\ 
\MyIndent Less often & 0.11 & 0.313 & 0 & 1 \\
Religious affiliation & & & & \\
\MyIndent Catholics & 0.242 & 0.428 & 0 & 1 \\ 
\MyIndent Muslim & 0.135 & 0.342 & 0 & 1 \\ 
\MyIndent Protestant & 0.112 & 0.315 & 0 & 1 \\ 
\MyIndent Orthodox & 0.144 & 0.351 & 0 & 1 \\
Type of job & & & & \\ 
\MyIndent Manager & 0.042 & 0.2 & 0 & 1 \\ 
\MyIndent Non-manual & 0.048 & 0.213 & 0 & 1 \\
\MyIndent Manual & 0.17 & 0.376 & 0 & 1 \\
\MyIndent Full-time & 0.377 & 0.485 & 0 & 1 \\
\MyIndent Self-employed & 0.13 & 0.336 & 0 & 1 \\
\MyIndent Retired & 0.125 & 0.33 & 0 & 1 \\
\MyIndent Unemployed & 0.09 & 0.286 & 0 & 1 \\
\multicolumn{1}{c}{N} & \multicolumn{4}{c}{98673}\\ 
\hline \hline
\end{tabular}
\end{table}
\end{document}

当我添加位“{S[S[tabnumalign=center,tabformat=3.2]}”时,我无法再编译我的表。我尝试使用 sisetup 添加上述选项,但没有成功。

有人有建议吗?

多谢。

达里奥

答案1

主要错误在于错误的表格序言:

\begin{tabular}{l c c c c }{S[S[tabnumalign=center,tabformat=3.2]}

没有意义;你必须指定S[<options>]像 acl列一样;所以

\begin{tabular}{
  @{}
  l
  S[table-format=2.3]
  S[table-format=2.3]
  S[table-format=2.0]
  S[table-format=2.0]
  @{}
 }

获得一个左对齐的列,其中包含四个数字列,其数字格式如所示(整数部分的数字和小数部分的数字)。

您有几个\smallskip命令只会给表格添加噪音;只需使用标准行距,添加用于划分块的水平规则;我还使用斜体作为字幕,以强调它们不是真正的表格行。

最后,我使用是\small因为表格相当大;我还稍微减少了列之间的间隔(这可能是不必要的,取决于文档的分页参数)。

并且标题文本不应该有周围的\textbf命令:使用进行设置\captionsetup

\documentclass{article}

\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[labelfont=bf]{caption}

\captionsetup{font=bf}
\newcommand*{\MyIndent}{\hspace*{1.5cm}}

\begin{document}

\begin{table}
\centering
\caption{Summary statistics\label{A2}}

\small\addtolength{\tabcolsep}{-1pt}
\begin{tabular}{
  @{}
  l
  S[table-format=2.3]
  S[table-format=2.3]
  S[table-format=2.0]
  S[table-format=2.0]
  @{}
 }
\toprule
\textbf{Variable} & {\textbf{Mean}} & {\textbf{Std.\ Dev.}} & {\textbf{Min.}} & {\textbf{Max.}} \\
\midrule
Individual preferences for redistribution (IPR) &  0.132 &  0.339 &  0 &  1 \\
Subjective Freedom (SF)                         &  7.030 &  2.262 &  1 & 10 \\
Fairness (Hard work)                            &  0.239 &  0.426 &  0 &  1 \\
Gender                                          &  0.513 &  0.5   &  0 &  1 \\
Age                                             & 41.199 & 15.829 & 18 & 99 \\
Self-reported income                            &  4.907 &  2.312 &  1 & 10 \\
Low education                                   &  0.27  &  0.444 &  0 &  1 \\
High education                                  &  0.275 &  0.446 &  0 &  1 \\
Single                                          &  0.24  &  0.427 &  0 &  1 \\
Married                                         &  0.564 &  0.496 &  0 &  1 \\
No children                                     &  0.285 &  0.451 &  0 &  1 \\
Size of town                                    &  4.855 &  2.485 &  1 &  8 \\
Household income satisfaction                   &  5.791 &  2.515 &  1 & 10 \\
Chief wage earner                               &  0.495 &  0.5   &  0 &  1 \\
Health status                                   &  3.87  &  0.87  &  1 &  5 \\
\midrule
\itshape Religious service attendance \\
\MyIndent More than once a week                 &  0.148 &  0.355 &  0 &  1 \\ 
\MyIndent Once a week                           &  0.187 &  0.39  &  0 &  1 \\
\MyIndent Once a month                          &  0.114 &  0.318 &  0 &  1 \\ 
\MyIndent Only on special holy days             &  0.165 &  0.371 &  0 &  1 \\ 
\MyIndent Other specific holy days              &  0.002 &  0.041 &  0 &  1 \\ 
\MyIndent Once a year                           &  0.062 &  0.241 &  0 &  1 \\ 
\MyIndent Less often                            &  0.11  &  0.313 &  0 &  1 \\
\midrule
\itshape Religious affiliation \\
\MyIndent Catholics                             &  0.242 &  0.428 &  0 &  1 \\ 
\MyIndent Muslim                                &  0.135 &  0.342 &  0 &  1 \\ 
\MyIndent Protestant                            &  0.112 &  0.315 &  0 &  1 \\ 
\MyIndent Orthodox                              &  0.144 &  0.351 &  0 &  1 \\
\midrule
\itshape Type of job \\ 
\MyIndent Manager                               &  0.042 &  0.2   &  0 &  1 \\ 
\MyIndent Non-manual                            &  0.048 &  0.213 &  0 &  1 \\
\MyIndent Manual                                &  0.17  &  0.376 &  0 &  1 \\
\MyIndent Full-time                             &  0.377 &  0.485 &  0 &  1 \\
\MyIndent Self-employed                         &  0.13  &  0.336 &  0 &  1 \\
\MyIndent Retired                               &  0.125 &  0.33  &  0 &  1 \\
\MyIndent Unemployed                            &  0.09  &  0.286 &  0 &  1 \\
\midrule
\multicolumn{5}{l}{$N=\num{98673}$}\\ 
\bottomrule
\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案2

以下更改足以通过编译,但结果并不理想。初始修复包括:

  • 使用SS[<options>] 代替适当的c c列;
  • 向包中添加一个选项以实现向后兼容,以便所选的特定选项能够起作用。

那是:

  \begin{tabular}{l S S[tabnumalign=center,tabformat=3.2] c c}\hline\hline

\usepackage[version-1-compatibility]{siunitx}

表初步修复

事实上,表格对于页面来说太宽了。此外,书签'手册解释说,默认配置不会创建具有专业外观的表格。为此,您需要遵循一些一般准则并使用一些增强版本的 LaTeX 命令和环境。通常,这意味着更多的垂直空间、可变宽度的规则,而不是挤压的单宽度规则的两倍等。如果我们也可以使用更新的语法来希尼奇并避免在标题规范中明确格式化的需要。

首先,我们可以删除

\captionsetup{labelfont=bf}

因为这只是重复传递给包的参数。其次,我们可以添加

font=bf

使标题内容也变为粗体,避免需要明确的标记。

所以我们有

\usepackage[labelfont=bf,font=bf]{caption}

进而

\caption{Summary statistics \label{A2}}

让我们也加载书签

\usepackage{booktabs}

并查看其规则是否能改善表格的外观。

\toprule

指定更重的规则,用于顶部,同样

\bottomrule

在底部。

\midrule

应该将列标题与表格主体分开。

所以现在我们有

\begin{table}
  \centering
  \caption{Summary statistics \label{A2}}
  \begin{tabular}{l S S[table-number-alignment=center,table-format=3.2] c c}
    \toprule
    \multicolumn{1}{l}{\textbf{Variable}} & \textbf{Mean} & \textbf{Std. Dev.}& \textbf{Min.} &  \textbf{Max.} \\
    \midrule
...
    \bottomrule
  \end{tabular}
\end{table}

就我个人而言,我也会删除额外的垂直间距。如果你真的想要,我会使用类似\\[\smallskipamount]\\[2\smallskipamount]但保持一致性很重要:你的读者应该清楚格式的差异意味着什么。

表格的最后一行看起来很奇怪。我建议把这些信息放在标题中,或者,如果你的表格包含注释,你可以把它放在那里。

对于表格的第三列,我们目前有

S[tabnumalign=center,tabformat=3.2]

让我们首先更新新的语法,以便我们可以抛弃向后兼容模式:

S[table-number-alignment=center,table-format=3.2]

但这并不完全正确。我们的列最多有两位整数和最多三位小数,但我们请求的空间最多有两位小数的三位数整数,所以我们需要更改它

S[table-number-alignment=center,table-format=2.3]

事实上,我们可能S也应该对上一列使用相同的设置,替换

S S[table-number-alignment=center,table-format=2.3]

*{2}{S[table-number-alignment=center,table-format=2.3]}

我们还需要更多的页面空间。最简单的方法是使用几何学让我们将更多的纸张用于文本块。我们不需要更多,但是

\usepackage[scale=.75]{geometry}

就可以了。

上述更改的结果如下所示

改进表

完整代码:

\documentclass{article}
\usepackage[scale=.75]{geometry}
\newcommand*{\MyIndent}{\hspace*{1.5cm}}
\usepackage{siunitx,booktabs}
\usepackage[labelfont=bf,font=bf]{caption}
\begin{document}
\begin{table}
  \centering
  \caption{Summary statistics (N=98673) \label{A2}}
  \begin{tabular}{l *{2}{S[table-number-alignment=center,table-format=2.3]} c c}
    \toprule
    \multicolumn{1}{l}{\textbf{Variable}} & \textbf{Mean} & \textbf{Std. Dev.}& \textbf{Min.} &  \textbf{Max.} \\
    \midrule
    Individual preferences for redistribution (IPR) & 0.132 & 0.339 & 0 & 1 \\
    Subjective Freedom (SF) & 7.030 & 2.262 & 1 & 10 \\
    Fairness (Hard work) & 0.239 & 0.426 & 0 & 1 \\
    Gender & 0.513 & 0.5 & 0 & 1 \\
    Age & 41.199 & 15.829 & 18 & 99 \\
    Self-reported income & 4.907 & 2.312 & 1 & 10 \\
    Low education & 0.27 & 0.444 & 0 & 1 \\
    High education & 0.275 & 0.446 & 0 & 1 \\
    Single & 0.24 & 0.427 & 0 & 1 \\
    Married & 0.564 & 0.496 & 0 & 1 \\
    No children & 0.285 & 0.451 & 0 & 1 \\
    Size of town & 4.855 & 2.485 & 1 & 8 \\
    Household income satisfaction & 5.791 & 2.515 & 1 & 10 \\
    Chief wage earner & 0.495 & 0.5 & 0 & 1 \\
    Health status & 3.87 & 0.87 & 1 & 5 \\
    Religious service attendance & & & & \\
    \MyIndent More than once a week & 0.148 & 0.355 & 0 & 1 \\
    \MyIndent Once a week & 0.187 & 0.39 & 0 & 1 \\
    \MyIndent Once a month & 0.114 & 0.318 & 0 & 1 \\
    \MyIndent Only on special holy days & 0.165 & 0.371 & 0 & 1 \\
    \MyIndent Other specific holy days & 0.002 & 0.041 & 0 & 1 \\
    \MyIndent Once a year & 0.062 & 0.241 & 0 & 1 \\
    \MyIndent Less often & 0.11 & 0.313 & 0 & 1 \\
    Religious affiliation & & & & \\
    \MyIndent Catholics & 0.242 & 0.428 & 0 & 1 \\
    \MyIndent Muslim & 0.135 & 0.342 & 0 & 1 \\
    \MyIndent Protestant & 0.112 & 0.315 & 0 & 1 \\
    \MyIndent Orthodox & 0.144 & 0.351 & 0 & 1 \\
    Type of job & & & & \\
    \MyIndent Manager & 0.042 & 0.2 & 0 & 1 \\
    \MyIndent Non-manual & 0.048 & 0.213 & 0 & 1 \\
    \MyIndent Manual & 0.17 & 0.376 & 0 & 1 \\
    \MyIndent Full-time & 0.377 & 0.485 & 0 & 1 \\
    \MyIndent Self-employed & 0.13 & 0.336 & 0 & 1 \\
    \MyIndent Retired & 0.125 & 0.33 & 0 & 1 \\
    \MyIndent Unemployed & 0.09 & 0.286 & 0 & 1 \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

答案3

您的 MWE 无法编译...所以我更正了所有错误(在使用中siunitx),删除了奇怪的解决方案以增加行之间的垂直空间并用替换它们\renewcommand{\arraystretch}{1.1}。我还介绍了包booktabs并从中使用规则。除了第一列,我S在所有其他列中使用类型列。在此之后,您的表格如下所示:

在此处输入图片描述

MWE:

\documentclass{article}
%---------------------------------------------------------------%
\newcommand*{\MyIndent}
{\hspace*{1.5cm}}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage[labelfont=bf]{caption}

    \begin{document}
\begin{table}[h]
    \centering
   \renewcommand{\arraystretch}{1.1}
\caption{\textbf{Summary statistics \label{A2}}}
\begin{tabular}{l*{2}{S[table-format=3.4]}*{2}{S[table-format=2]}}
    \toprule
\textbf{Variable} 
    &   {\textbf{Mean}}
        &   {\textbf{Std. Dev.}}
            &   {\textbf{Min.}}
                &   {\textbf{Max.}}         \\ 
    \midrule
Individual preferences for redistribution (IPR) 
    & 0.132 & 0.339 & 0 & 1     \\ 
Subjective Freedom (SF) 
    & 7.030 & 2.262 & 1 & 10    \\
Fairness (Hard work) & 0.239 & 0.426 & 0 & 1 \\
Gender & 0.513 & 0.5 & 0 & 1 \\
Age & 41.199 & 15.829 & 18 & 99 \\
Self-reported income & 4.907 & 2.312 & 1 & 10 \\
Low education & 0.27 & 0.444 & 0 & 1 \\
High education & 0.275 & 0.446 & 0 & 1 \\
Single & 0.24 & 0.427 & 0 & 1 \\
Married & 0.564 & 0.496 & 0 & 1 \\
No children & 0.285 & 0.451 & 0 & 1 \\
Size of town & 4.855 & 2.485 & 1 & 8 \\
Household income satisfaction & 5.791 & 2.515 & 1 & 10 \\
Chief wage earner & 0.495 & 0.5 & 0 & 1 \\
Health status & 3.87 & 0.87 & 1 & 5 \\
Religious service attendance & & & & \\
\MyIndent More than once a week & 0.148 & 0.355 & 0 & 1 \\
\MyIndent Once a week & 0.187 & 0.39 & 0 & 1 \\
\MyIndent Once a month & 0.114 & 0.318 & 0 & 1 \\
\MyIndent Only on special holy days & 0.165 & 0.371 & 0 & 1 \\
\MyIndent Other specific holy days & 0.002 & 0.041 & 0 & 1 \\
\MyIndent Once a year & 0.062 & 0.241 & 0 & 1 \\
\MyIndent Less often & 0.11 & 0.313 & 0 & 1 \\
Religious affiliation & & & & \\
\MyIndent Catholics & 0.242 & 0.428 & 0 & 1 \\
\MyIndent Muslim & 0.135 & 0.342 & 0 & 1 \\
\MyIndent Protestant & 0.112 & 0.315 & 0 & 1 \\
\MyIndent Orthodox & 0.144 & 0.351 & 0 & 1 \\
Type of job & & & & \\
\MyIndent Manager & 0.042 & 0.2 & 0 & 1 \\
\MyIndent Non-manual & 0.048 & 0.213 & 0 & 1 \\
\MyIndent Manual & 0.17 & 0.376 & 0 & 1 \\
\MyIndent Full-time & 0.377 & 0.485 & 0 & 1 \\
\MyIndent Self-employed & 0.13 & 0.336 & 0 & 1 \\
\MyIndent Retired & 0.125 & 0.33 & 0 & 1 \\
\MyIndent Unemployed & 0.09 & 0.286 & 0 & 1 \\
    \midrule
\multicolumn{1}{c}{$N$} & \multicolumn{4}{c}{98673}\\
    \bottomrule
\end{tabular}
\end{table}
    \end{document}

编辑:通过确定最后两列的单独表格格式可以获得更美观的表格,所以我现在介绍这一点。

相关内容