将 siunitx 表中的数字与定义的列宽对齐

将 siunitx 表中的数字与定义的列宽对齐

我在正确更改表格的列宽时遇到问题。我希望使用 siunitx 包格式化列。在我的数据中,有一些值应该按原样打印(例如 0.0425),还有一些值应该使用浮点移位打印(3.585 * 10 ^ -5)。

当我使用 S 列的 table-column-width 属性更改列宽时,值不再对齐。似乎普通浮点数的行为符合预期,并且 10 的幂数位于不调整列大小时它们所在的位置。

要设置数据,您可以使用

\documentclass{minimal}

\begin{document}

\begin{filecontents*}{data.csv}
Parameter,200,600,No-Treatment
N-Observations,209.0,209.0,195.0
Mean,2.500170353865091e-05,2.1559213174203927e-05,0.00015240802982599504
Variance,5.5824149143594136e-08,-1.1650000181975986e-08,1.7979465607149884e-07
Skewness,-0.4393776076843506,-0.06577317175679437,-0.13945687546114036
\end{filecontents*}

\end{document}

查看此问题的 MWE 如下:

\documentclass{paper}

\usepackage{array}
\usepackage{booktabs} % version 1.61803398
\usepackage{siunitx} % version 2.8b

\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 4, % to 4
}

\usepackage{pgfplotstable} % version 1.17
\pgfplotsset{compat=newest}

\begin{document}


\begin{table}[ht]
    \centering
    \caption{Statistical parameters example}
    \pgfplotstabletypeset[
      multicolumn names, % allows to have multicolumn names
      col sep=comma, % the seperator in our .csv file
      display columns/0/.style={
        column name=Parameter, % name of first column
        column type={p{0.25\textwidth}|},string type},  % use siunitx for formatting
      display columns/1/.style={
        column type={S[table-column-width=3cm]},string type},  % use siunitx for formatting
      display columns/2/.style={
        column type={S[table-column-width=3cm]},string type},  % use siunitx for formatting
      display columns/3/.style={
        column type={S[table-column-width=3cm]},string type},  % use siunitx for formatting
      every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={\midrule},
        every last row/.style={after row=\bottomrule}
        }
    ]{data/stat-windows-mean.csv} % filename/path to file
\end{table}

\end{document}

答案1

v2 代码在这里确实存在问题,但即使在 v3(固定宽度方法中没有错误)中,我仍然会用它table-format来定义一些保留空间:

\begin{filecontents*}{data.csv}
Parameter,200,600,No-Treatment
N-Observations,209.0,209.0,195.0
Mean,2.500170353865091e-05,2.1559213174203927e-05,0.00015240802982599504
Variance,5.5824149143594136e-08,-1.1650000181975986e-08,1.7979465607149884e-07
Skewness,-0.4393776076843506,-0.06577317175679437,-0.13945687546114036
\end{filecontents*}

\documentclass{article}

\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}%[=v2]

\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 4, % to 4
}

\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}


\begin{table}[ht]
  \sisetup{table-column-width=3cm,table-format = 3.4e+1}
    \centering
    \caption{Statistical parameters example}
    \pgfplotstabletypeset[
      multicolumn names, % allows to have multicolumn names
      col sep=comma, % the seperator in our .csv file
      display columns/0/.style={
        column name=Parameter, % name of first column
        column type={p{0.25\textwidth}|},string type},  % use siunitx for formatting
      display columns/1/.style={
        column type={S},string type},  % use siunitx for formatting
      display columns/2/.style={
        column type={S},string type},  % use siunitx for formatting
      display columns/3/.style={
        column type={S},string type},  % use siunitx for formatting
      every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={\midrule},
        every last row/.style={after row=\bottomrule}
        }
    ]{data.csv} % filename/path to file
\end{table}

\end{document}

v3 代码在这里做出了更好的努力,但您需要添加,table-fixed-width因为我已经将“存储列宽”和“使用固定列宽”的想法分开了。

相关内容