使用 siunitx 时出现列宽和文本对齐问题

使用 siunitx 时出现列宽和文本对齐问题

我在格式化包含变量汇总统计信息的大型表格时遇到了问题。我的变量名称很长,我想保留这些名称,但如果没有办法,我可以使用它们的名称的快捷方式。问题是我想按小数点对齐数字,并且还希望表格的列之间具有相同的间距,并且列的名称恰好位于列上方的中心。我设法使用 S 列按小数点对齐,但表格仍然看起来不太好,因为列标题似乎并不正好位于每列数字上方的中间(例如列中位数和标准差),列标题中的文本似乎与数字相比向右移动。此外,最后一列和列“中位数”之间的空间显然比任何其他列之间的空间都大,所以如果可能的话,我想减少它,并且我想增加第一个“变量”列和第二列之间的空间。我认为问题在于我不知道 S 列选项 table-format=.... 中的数字代表什么:在不同的数字对齐问题中,他们建议使用不同的表格格式,但他们从来没有说明为什么 table-format=3.2 或 table-format=1.5,所以我很困惑应该使用哪一个以及为什么......下面是我的代码:

\documentclass[a4paper,11pt]{article}
\usepackage[cp1250]{inputenc} 
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{amsmath, amsthm, amssymb} 
\usepackage{booktabs}
\usepackage[
singlelinecheck=false % <-- important
]{caption}
\usepackage{subcaption}
\usepackage[detect-all]{siunitx}
\usepackage{multirow}

\usepackage{epstopdf}
\usepackage{rotating}
\usepackage{fullpage}
\usepackage{enumerate}
\usepackage{setspace}
\usepackage{authblk}
\usepackage[round]{natbib}
\usepackage{natbib} 
\usepackage{hyperref} 
\hypersetup{pdfstartview={XYZ null null 1.00},bookmarksnumbered,hypertexnames=false} 

\setlength\parindent{34pt}
\onehalfspacing


\begin{document}

\newcommand*{\SuperScriptSameStyle}[1]{%
  \ensuremath{%
    \mathchoice
      {{}^{\displaystyle #1}}%
      {{}^{\textstyle #1}}%
      {{}^{\scriptstyle #1}}%
      {{}^{\scriptscriptstyle #1}}%
  }%
}

\newcommand*{\one}{\SuperScriptSameStyle{*}}
\newcommand*{\two}{\SuperScriptSameStyle{**}}
\newcommand*{\three}{\SuperScriptSameStyle{*{*}*}}

\begin{table}
\begin{footnotesize}
\captionsetup{font=footnotesize}
\begin{center}
\begin{tabular}{
  @{}l  *{6}{S[table-format=1.5,table-space-text-post=*,table-align-text-post=false,group-digits=false,table-number-alignment=center]}@{}}
\toprule
\multirow{2}{*}{\textbf{Variable}} & \multirow{2}{*}{\textbf{mean}} & \multirow{2}{*}{\textbf{st.dev}} & \multirow{2}{*}{\textbf{min}} & \multirow{2}{*}{\textbf{max}} & \multirow{2}{*}{\textbf{median}} &  {\textbf{Jarque - Bera}} \\
&      &       &       &     &   &  {\textbf{statistic}}      \\
\midrule
Variable 1 that has a long name      & -2.198      &  0.948        &  -4.273     &   2.356    & -1.085     &   0.989\one    \\[1pt]
Variable 2 that has a long name      & -4.140      &  4.432        &  -16.019    &   6.289    & -2.846     &   1.919\three  \\[1pt]
Variable 3 that has a long name      & -0.899      &  0.592        &  -3.980     &   1.320    & -5.397     &   2.916\two    \\[1pt]
Variable 4 that has a long name      &  2.654      &  2.924        &  -1.482     &   9.295    &  1.992     &   0.878\three  \\[1pt]
Variable 5 that has a long name      &  -3.412     &  1.422        &  -5.768     &   8.929    & -0.813     &   0.898\two    \\[1pt]
Variable 6 that has a long name      & -0.250      &  4.382        &  -11.357    &   7.473    & -0.190     &   0.747\three  \\[1pt]
Variable 7 that has a long name      & -0.308      &  0.809        &  -2.726     &   4.120    & -0.243     &   4.937\one    \\[1pt]
Variable 8 that has a long name      & -3.741      &  11.595       &  -24.379    &   28.456   & -3.782     &   0.943\three  \\[1pt]
Variable 9 that has a long name      & -1.152      &  1.469        &  -9.872     &   5.253    & -0.217     &   0.991\two    \\[1pt]
Variable 10 that has a long name     & -0.409      &  2.580        &  -13.600    &   11.498   & -0.565     &   0.896\three  \\[1pt]
Variable 11 that has a long name     & 0.018       &  2.811        &  -4.874     &   5.996    & -5.003     &   1.997        \\[1pt]
Variable 12 that has a long name     & 0.772       &  1.496        &  -3.728     &   14.362   & 0.213      &  0.539\three   \\[1pt]
\bottomrule
$N = 220$ & & & & & &  \\
\bottomrule
\end{tabular}
\end{center}
\vspace{-10pt}

\caption{Sample table}
\label{}
\end{footnotesize}
\end{table}


\end{document}

生成下表: 在此处输入图片描述

有人能帮帮我吗?

答案1

这是我能想到的解决方案:

\documentclass[a4paper,11pt]{article}
\usepackage[cp1250]{inputenc} 
\usepackage{setspace}

\usepackage{booktabs}
\usepackage{makecell}
\usepackage[singlelinecheck=false]{caption}

\usepackage[detect-all]{siunitx}
\usepackage{fullpage}

\setlength\parindent{34pt}
\onehalfspacing
\usepackage{showframe}

\newcommand*{\SuperScriptSameStyle}[1]{%
  \ensuremath{%
    \mathchoice
      {{}^{\displaystyle #1}}%
      {{}^{\textstyle #1}}%
      {{}^{\scriptstyle #1}}%
      {{}^{\scriptscriptstyle #1}}%
  }%
}

\newcommand*{\one}{\SuperScriptSameStyle{*}}
\newcommand*{\two}{\SuperScriptSameStyle{**}}
\newcommand*{\three}{\SuperScriptSameStyle{*{*}*}}


\begin{document}


\begin{table}
\begin{footnotesize}
\captionsetup{font=footnotesize}
\begin{center}
\begin{tabular}{
  @{}l  S[table-format=-1.3]S[table-format=2.3]S[table-format=-2.3]S[table-format=2.3]S[table-format=-1.3]S[table-format=1.6,table-align-text-post=false]@{}}
\toprule
{\textbf{Variable}} & {\textbf{mean}}   &  {\textbf{st.dev}}     &   {\textbf{min}}   & {\textbf{max}}   & {\textbf{median}}  &  {\makecell{\textbf{Jarque-Bera} \\ \textbf{statistics}}}      \\
\midrule
Variable 1 that has a long name      & -2.198      &  0.948        &  -4.273     &   2.356    & -1.085     &   0.989\one    \\[1pt]
Variable 2 that has a long name      & -4.140      &  4.432        &  -16.019    &   6.289    & -2.846     &   1.919\three  \\[1pt]
Variable 3 that has a long name      & -0.899      &  0.592        &  -3.980     &   1.320    & -5.397     &   2.916\two \\[1pt]
Variable 4 that has a long name      &  2.654      &  2.924        &  -1.482     &   9.295    &  1.992     &   0.878\three  \\[1pt]
Variable 5 that has a long name      &  -3.412     &  1.422        &  -5.768     &   8.929    & -0.813     &   0.898\two    \\[1pt]
Variable 6 that has a long name      & -0.250      &  4.382        &  -11.357    &   7.473    & -0.190     &   0.747\three  \\[1pt]
Variable 7 that has a long name      & -0.308      &  0.809        &  -2.726     &   4.120    & -0.243     &   4.937\one    \\[1pt]
Variable 8 that has a long name      & -3.741      &  11.595       &  -24.379    &   28.456   & -3.782     &   0.943\three  \\[1pt]
Variable 9 that has a long name      & -1.152      &  1.469        &  -9.872     &   5.253    & -0.217     &   0.991\two    \\[1pt]
Variable 10 that has a long name     & -0.409      &  2.580        &  -13.600    &   11.498   & -0.565     &   0.896\three  \\[1pt]
Variable 11 that has a long name     & 0.018       &  2.811        &  -4.874     &   5.996    & -5.003     &   1.997        \\[1pt]
Variable 12 that has a long name     & 0.772       &  1.496        &  -3.728     &   14.362   & 0.213      &  0.539\three   \\[1pt]
\midrule
$N = 220$ & & & & & &  \\
\bottomrule
\end{tabular}
\end{center}
\vspace{-10pt}

\caption{Sample table}
\label{}
\end{footnotesize}
\end{table}

\end{document}

在上面的例子中,我将multirows 替换为。此外,我根据其内容分别\makecell更改了每列table-format单元格的。例如,对于“平均值”列,我选择了,因为您有带有S-1.3最多有 小数点分隔符前的数字,最多分隔符后的数字。

上述 MWE 产生以下输出:(请注意,图像两侧的垂直线表示文本宽度。它们由 产生\usepackage{showframe})。

在此处输入图片描述

这是摆脱脚注大小的文本的另一种方法。

\documentclass[a4paper,11pt]{article}
\usepackage[cp1250]{inputenc} 
\usepackage{setspace}

\usepackage{booktabs}
\usepackage{makecell}
\usepackage{tabularx}
\usepackage[
singlelinecheck=false % <-- important
]{caption}

\usepackage[detect-all]{siunitx}
\usepackage{fullpage}

\setlength\parindent{34pt}
\onehalfspacing
\usepackage{showframe}

\newcommand*{\SuperScriptSameStyle}[1]{%
  \ensuremath{%
    \mathchoice
      {{}^{\displaystyle #1}}%
      {{}^{\textstyle #1}}%
      {{}^{\scriptstyle #1}}%
      {{}^{\scriptscriptstyle #1}}%
  }%
}

\newcommand*{\one}{\SuperScriptSameStyle{*}}
\newcommand*{\two}{\SuperScriptSameStyle{**}}
\newcommand*{\three}{\SuperScriptSameStyle{*{*}*}}


\begin{document}

\begin{table}
\captionsetup{font=footnotesize}
\begin{center}
\begin{tabularx}{\textwidth}{
  @{}X  S[table-format=-1.3]S[table-format=2.3]S[table-format=-2.3]S[table-format=2.3]S[table-format=-1.3]S[table-format=1.6,table-align-text-post=false]@{}}
\toprule
{\textbf{Variable}} & {\textbf{mean}}   &  {\textbf{st.dev}}     &   {\textbf{min}}   & {\textbf{max}}   & {\textbf{median}}  &  {\makecell{\textbf{Jarque-Bera} \\ \textbf{statistics}}}      \\
\midrule
Variable 1 that has a long name      & -2.198      &  0.948        &  -4.273     &   2.356    & -1.085     &   0.989\one    \\[1pt]
Variable 2 that has a long name      & -4.140      &  4.432        &  -16.019    &   6.289    & -2.846     &   1.919\three  \\[1pt]
Variable 3 that has a long name      & -0.899      &  0.592        &  -3.980     &   1.320    & -5.397     &   2.916\two    \\[1pt]
Variable 4 that has a long name      &  2.654      &  2.924        &  -1.482     &   9.295    &  1.992     &   0.878\three  \\[1pt]
Variable 5 that has a long name      &  -3.412     &  1.422        &  -5.768     &   8.929    & -0.813     &   0.898\two    \\[1pt]
Variable 6 that has a long name      & -0.250      &  4.382        &  -11.357    &   7.473    & -0.190     &   0.747\three  \\[1pt]
Variable 7 that has a long name      & -0.308      &  0.809        &  -2.726     &   4.120    & -0.243     &   4.937\one    \\[1pt]
Variable 8 that has a long name      & -3.741      &  11.595       &  -24.379    &   28.456   & -3.782     &   0.943\three  \\[1pt]
Variable 9 that has a long name      & -1.152      &  1.469        &  -9.872     &   5.253    & -0.217     &   0.991\two    \\[1pt]
Variable 10 that has a long name     & -0.409      &  2.580        &  -13.600    &   11.498   & -0.565     &   0.896\three  \\[1pt]
Variable 11 that has a long name     & 0.018       &  2.811        &  -4.874     &   5.996    & -5.003     &   1.997        \\[1pt]
Variable 12 that has a long name     & 0.772       &  1.496        &  -3.728     &   14.362   & 0.213      &  0.539\three   \\[1pt]
\midrule
$N = 220$ & & & & & &  \\
\bottomrule
\end{tabularx}
\end{center}
\vspace{-10pt}

\caption{Sample table}
\label{}
\end{table}

\end{document}

这种方法对列的规范S与前面所述相同。为了去掉脚注大小的文本,同时仍使表格适合文本宽度,我使用了包X的可变宽度列tabularx

下图显示了替代方法的输出:

在此处输入图片描述

答案2

\documentclass{article}
\begin{document}




\begin{table}

\begin{tabular}{|p{4cm}|p{1.5cm}|p{1cm}|p{1.5cm}|p{1cm}|p{1cm}|p{1cm}|}\hline
Variable & Mean & St.Dev & Min & Max & Median & Jaqua Berra Statistics\\\hline

Variable 1 that has a long name      & -2.198      &  0.948        &  -4.273     &   2.356    & -1.085     &   0.989${}^{*}$   \\
Variable 2 that has a long name      & -4.140      &  4.432        &  -16.019    &   6.289    & -2.846     &   1.919${}^{***}$  \\
Variable 3 that has a long name      & -0.899      &  0.592        &  -3.980     &   1.320    & -5.397     &   2.916${}^{**}$    \\
Variable 4 that has a long name      &  2.654      &  2.924        &  -1.482     &   9.295    &  1.992     &   0.878${}^{***}$  \\
Variable 5 that has a long name      &  -3.412     &  1.422        &  -5.768     &   8.929    & -0.813     &   0.898${}^{**}$    \\
Variable 6 that has a long name      & -0.250      &  4.382        &  -11.357    &   7.473    & -0.190     &   0.747${}^{***}$  \\
Variable 7 that has a long name      & -0.308      &  0.809        &  -2.726     &   4.120    & -0.243     &   4.937${}^{*}$    \\
Variable 8 that has a long name      & -3.741      &  11.595       &  -24.379    &   28.456   & -3.782     &   0.943${}^{***}$   \\
Variable 9 that has a long name      & -1.152      &  1.469        &  -9.872     &   5.253    & -0.217     &   0.991${}^{**}$   \\
Variable 10 that has a long name     & -0.409      &  2.580        &  -13.600    &   11.498   & -0.565     &   0.896${}^{***}$  \\
Variable 11 that has a long name     & 0.018       &  2.811        &  -4.874     &   5.996    & -5.003     &   1.997        \\
Variable 12 that has a long name     & 0.772       &  1.496        &  -3.728     &   14.362   & 0.213      &  0.539${}^{***}$   \\\hline
&&&&&&\\
N = 220 & & & & & &  \\\hline
\end{tabular}
\caption{Sample Table}
\end{table}

\end{document}

我已经这样做了。这很简单,而且你不需要额外的包。

相关内容