使阈值与逗号作为小数分隔符兼容

使阈值与逗号作为小数分隔符兼容

我在用代码以便更好地控制何时使用科学计数法以及何时不使用科学计数法。

问题是,使用逗号作为小数点分隔符时似乎不兼容,例如:

% 
\documentclass[a4paper,12pt]{article}

\usepackage[locale=DE,per-mode=fraction,round-pad=false]{siunitx}

\usepackage[low=1e-2,high=1e3]{threshold}

\begin{document}

\SI{5.2}{m} works but %\SI{5,2}{m} not. 
\end{document}  
  
% threshold.sty
\RequirePackage{expl3,kvoptions,siunitx}
\SetupKeyvalOptions{family=threshold,prefix=threshold@}
\DeclareStringOption[1]{low}[0.001]
\DeclareStringOption[1]{high}[100]
\ProcessKeyvalOptions*
\sisetup{scientific-notation=true}
\ExplSyntaxOn
    \cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
\let\OldNum\num%
\renewcommand*{\num}[2][]{%
    \fpcmpTF{abs(#2)<=\threshold@low}{%
        \OldNum[scientific-notation=true,#1]{#2}%
    }{%
        \fpcmpTF{abs(#2)>=\threshold@high}{%
            \OldNum[scientific-notation=true,#1]{#2}%
        }{%
            \OldNum[scientific-notation=false,#1]{#2}%
        }%
    }%
}

\let\OldSI\SI%
\renewcommand*{\SI}[3][]{%
    \fpcmpTF{abs(#2)<=\threshold@low}{%
        \OldSI[scientific-notation=true,#1]{#2}{#3}%
    }{%
        \fpcmpTF{abs(#2)>=\threshold@high}{%
            \OldSI[scientific-notation=true,#1]{#2}{#3}%
        }{%
            \OldSI[scientific-notation=false,#1]{#2}{#3}%
        }%
    }%
}

我怎样才能使它也与逗号分隔符一起工作。

答案1

免责声明

我一般不建议复制并重新定义命令\SI\num,因此这个答案应该更多地被理解为“这是可以做到的,即使它不应该”。siunitx有接口来创建新的命令(或改变现有的命令),这些命令可用于实现这个问题的预期效果。引用软件包作者的话:

不应抄袭\num\SI 根本- 有一个记录的程序员 API siunitx,人们应该使用它来实现修改后的文档命令。


尽管我建议正确输入(尤其是对于您的e-2解释为1e-2),但以下内容同时实现了两者。我还放弃了kvoptions自最近版本以来内置于 LaTeX 中的 key=value 接口(回退到兼容的l3keys2e),并对您的 sty 文件进行了一些重构。

包裹

% threshold.sty
\RequirePackage{expl3,siunitx,etl}
\ProvidesExplPackage{threshold}{2022-08-30}{v0}{custom package mangling siunitx}

\etl_new_replace_once:Nn \julia_threshold_replace_comma:nn { , }
\cs_new:Npn \julia_threshold_parse_number:n #1
  {
    \exp_args:Ne \julia_threshold_mayhaps_add_one:n
      { \julia_threshold_replace_comma:nn {#1} { . } }
  }
\cs_new:Npn \julia_threshold_mayhaps_add_one:n #1
  { \tl_if_head_eq_charcode:nNT {#1} e {1} #1 }


\fp_new:N \l_julia_threshold_low_fp
\fp_new:N \l_julia_threshold_high_fp

\keys_define:nn { julia/threshold }
  {
     low  .code:n =
      \fp_set:Nn \l_julia_threshold_low_fp
        { \julia_threshold_parse_number:n {#1} }
    ,low  .initial:n = 0.001
    ,high  .code:n =
      \fp_set:Nn \l_julia_threshold_high_fp
        { \julia_threshold_parse_number:n {#1} }
    ,high .initial:n = 100
  }

\cs_if_exist:NTF \ProcessKeyOptions
  { \ProcessKeyOptions[julia/threshold] }
  {
    \RequirePackage{l3keys2e}
    \ProcessKeysOptions{julia/threshold}
  }

\NewCommandCopy\OldNum\num
\NewCommandCopy\OldSI\SI
\sisetup{scientific-notation=true}

\RenewDocumentCommand \num { O{} m }
  {
    \fp_compare:nTF
      {
        abs ( \julia_threshold_parse_number:n {#2} )
        <=
        \l_julia_threshold_low_fp
      }
      {
        \OldNum[scientific-notation=true, #1]
          { \julia_threshold_mayhaps_add_one:n {#2} }
      }
      {
        \fp_compare:nTF
          {
            abs ( \julia_threshold_parse_number:n {#2} )
            >=
            \l_julia_threshold_high_fp
          }
          {
            \OldNum[scientific-notation=true, #1]
              { \julia_threshold_mayhaps_add_one:n {#2} }
          }
          {
            \OldNum[scientific-notation=false, #1]
              { \julia_threshold_mayhaps_add_one:n {#2} }
          }
      }
  }

\RenewDocumentCommand \SI { O{} m m }
  {
    \fp_compare:nTF
      {
        abs ( \julia_threshold_parse_number:n {#2} )
        <=
        \l_julia_threshold_low_fp
      }
      {
        \OldSI[scientific-notation=true, #1]
          { \julia_threshold_mayhaps_add_one:n {#2} } {#3}
      }
      {
        \fp_compare:nTF
          {
            abs ( \julia_threshold_parse_number:n {#2} )
            >=
            \l_julia_threshold_high_fp
          }
          {
            \OldSI[scientific-notation=true, #1]
              { \julia_threshold_mayhaps_add_one:n {#2} } {#3}
          }
          {
            \OldSI[scientific-notation=false, #1]
              { \julia_threshold_mayhaps_add_one:n {#2} } {#3}
          }
      }
  }

\NewDocumentCommand \thresholdsetup { m }
  { \set_keys:nn { julia/threshold } {#1} }

文档

\documentclass[a4paper,12pt]{article}

\usepackage[locale=DE,per-mode=fraction,round-pad=false]{siunitx}

\usepackage[low=1e-2,high=1e3]{threshold}

\begin{document}
\SI{5.2}{m} works but \SI{5,2}{m} and \SI{e-2}{m} not.
\end{document}

结果

在此处输入图片描述

相关内容