在 siunitx“S 列”中选择性使用科学计数法

在 siunitx“S 列”中选择性使用科学计数法

我的问题类似于仅适用于大数的科学记数法使用 siunitx 调整指数以在符号之间切换

我有一张表格,其中大部分数字都在 .001 到 100 之间。我想用科学计数法来表示超出该范围的数字。我使用“S 列”,但这似乎与上面链接的解决方案不兼容。如果我使用常规列和上面重新定义的 \num 命令,我会遇到对齐问题,因为我有帖子文本(\Star 表示统计显著性)。

下面是包含上述链接代码的 MWE。

我如何有选择地使用科学计数法并正确对齐我的数字?

谢谢!

\documentclass[12pt]{article}
\usepackage{expl3,siunitx}
    \sisetup{
scientific-notation=true,
        detect-mode,
        tight-spacing           = true,
        input-open-uncertainty  = ,
        input-close-uncertainty = ,
        round-mode              = places,
        round-precision         = 3,
        table-space-text-pre    = ( [,
        table-space-text-post   = ) ] \Star 
        }
\protected\def\Star{$\text{*}$}

\ExplSyntaxOn
    \cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff

\newcommand*{\ThresholdLow}{0.001}
\newcommand*{\ThresholdHigh}{100}

\renewcommand*{\num}[2][]{%
    \fpcmpTF{abs(#2)<=\ThresholdLow}{%
        \OldNum[scientific-notation=true,#1]{#2}%
    }{%
        \fpcmpTF{abs(#2)>=\ThresholdHigh}{%
            \OldNum[scientific-notation=true,#1]{#2}%
        }{%
            \OldNum[scientific-notation=false,#1]{#2}%
        }%
    }%
}%

\begin{document}

 \begin{tabular}{l*{4}{S}}
2006&\num{0.0004} &-0.0282\Star&0.0003 &-0.0015\\
     &(0.0361) &(0.0229)&(0.1285) &(0.0539)\\
2011&-0.0002 &-0.0315&0.0083 &0.0037\\

\end{tabular}

\end{document}

答案1

拳头解决方案(手动操作)

使用正常首选项时,siunitx 会按照您输入的方式打印数字。0.000001不是以正常十进制模式打印,1.234e5而是以科学模式打印。这里有一个最小的工作示例:

\documentclass[12pt]{article}
\usepackage{siunitx}
    \sisetup{
        tight-spacing           = true,
        input-open-uncertainty  = ,
        input-close-uncertainty = ,
        round-mode              = places,
        round-precision         = 3,
        table-space-text-pre    = (,
        table-space-text-post   = )\Star,
        table-align-text-pre    = false
        }
\protected\def\Star{$\text{*}$}

\begin{document}

\begin{tabular}{l*{4}{S}}
2006 & 4e-4 & -0.0282\Star & 3e-4 & -0.0015\\
     & (0.0361) & (0.0229) & (0.1285) & (0.0539)\\
2011 &-2e-4 &-0.0315 & 0.0083 & 0.0037\\
\end{tabular}

\end{document}

结果如下:

Tabluar,手动符号切换

更新:第二种解决方案(自动执行)

这是一个自动检查数字并正确格式化的版本。首先,它检查数字是否在 0.001 到 100 之间,或 -0.001 到 -100 之间。然后\pgfmathprintnumberto用于创建一个包含数字的宏,该宏采用逐字的科学形式(例如1.234e5),由 siunitx 进行解释。

更新2:Mirco 对括号间距问题有正确的解决方案,我编辑了我的答案,包括他的提示。

\documentclass[12pt]{article}
\usepackage{etoolbox}
\usepackage{tikz}
\usepgflibrary{fpu}
\usepackage{siunitx}
    \sisetup{
        tight-spacing           = true,
        input-open-uncertainty  = ,
        input-close-uncertainty = ,
        round-mode              = places,
        round-precision         = 3,
        table-space-text-post   = )\Star,
        input-symbols = (
        }
\protected\def\Star{$\text{*}$}

\def\mynum{}
\newcommand{\testnum}[1]{\begingroup%
\pgfmathparse{or(and(#1<100,#1>=0.001), and(#1>-100,#1<=-0.001))}%
\globaldefs=1\relax\ifnumequal{\pgfmathresult}{1}%
   {\pgfkeys{/pgf/number format/.cd, fixed, precision=3, verbatim}}% printing 12300
   {\pgfkeys{/pgf/number format/.cd, sci, sci e, precision=3, verbatim}}% printing 1.23e4
\pgfmathprintnumberto{#1}{\mynum}\endgroup}
\begin{document}

\begin{tabular}{l*{4}{S}}
2006 & {\testnum{0.0004}}\mynum & {\testnum{-0.0282}}\mynum\Star &
{\testnum{0.0003}}\mynum & {\testnum{-0.0015}}\mynum\\
     & {\testnum{0.0361}}(\mynum) & {\testnum{0.0229}}(\mynum) &
{\testnum{0.1285}}(\mynum) & {\testnum{0.0539}}(\mynum)\\
2011 & {\testnum{-0.0002}}\mynum & {\testnum{-0.0315}}\mynum &
{\testnum{0.0083}}\mynum & {\testnum{0.0037}}\mynum\\
\end{tabular}
\end{document}

结果如下:

表格、自动符号切换

相关内容