将参数传递给 .sty 文件 - 特定情况

将参数传递给 .sty 文件 - 特定情况

在仔细阅读(即阅读并精简给出的示例以尝试将其应用于我的情况)这些文档和指南后:

我仍然不明白如何简单地将两个数字传递给一个.sty文件。

情况如下:有一个.sty文件可以改变的行为,siunitx以便只有非常小和非常大的数字以科学计数法输出,而其他数字则以通常的格式显示。该文件具有以下内容:

\RequirePackage{expl3,siunitx}
\sisetup{scientific-notation=true}
\ExplSyntaxOn
    \cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
\newcommand*{\ThresholdLow}{0.01}
\newcommand*{\ThresholdHigh}{100}
\let\OldNum\num%
\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}%
        }%
    }%
}

并且暂时保存threshold.sty在与(待)编译文档相同的文件夹中。本文档使用 -command 将其包含进去usepackage,如下所示:

\documentclass{article}
\usepackage{threshold}
\newcommand{\Row}[1]{#1 & \OldNum{#1} & \num{#1}}
\begin{document}
\begin{tabular}{l l l}
    Num & Old & New\\\hline\\[-0.7em]
    \Row{0.01}\\
    \Row{0.1}\\
    \Row{1}\\
    \Row{10}\\
    \Row{100}\\
\end{tabular}
\end{document}

.sty上面的文件中,有两行定义了应使用科学计数法打印的数字的限制:

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

现在理想情况下,这两个阈值将作为参数提供给threshold.sty使用例如

\usepackage[low=0.01,high=100]{threshold}

如果没有给出参数,文件将使用0.01和作为默认值。100

最好的方法是什么?对于和我一样对这些事情不熟悉的其他读者来说,也许更有用的是:最简单的方法是什么?

答案1

通过再次阅读文档kvoptions,我自己也搞清楚了。结果如下:

\RequirePackage{expl3,kvoptions,siunitx}
\SetupKeyvalOptions{family=threshold,prefix=threshold@}
\DeclareStringOption[1]{low}[0.01]
\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}%
        }%
    }%
}

首先需要使用 设置选项\SetupKeyvalOptions,然后使用 定义键(变量)\DeclareStringOption,最后使用 处理这些键\ProcessKeyvalOptions*。 在我的例子中,这是通过下一段代码完成的。

\SetupKeyvalOptions{family=threshold,prefix=threshold@}
\DeclareStringOption[1]{low}[0.01]
\DeclareStringOption[1]{high}[100]
\ProcessKeyvalOptions*

这里\DeclareStringOption[1]{low}[0.01]举例来说,键low的初始值为,如果将键传递给包时没有值(即),则该值1变为,否则为通过调用包分配的值(例如)。我们现在可以使用和作为变量。0.01low\usepackage[low]{threshold}\usepackage[low=1e-3]{threshold}\threshold@low\threshold@high


使用.tex与问题中描述的相同的文件,我们现在可以例如仅使用下限的阈值:

\documentclass{article}
\usepackage[low]{threshold}
\newcommand{\Row}[1]{#1 & \OldNum{#1} & \num{#1}}
\begin{document}
\begin{tabular}{l l l}
    Num & Old & New\\\hline\\[-0.7em]
    \Row{0.01}\\
    \Row{0.1}\\
    \Row{1}\\
    \Row{10}\\
    \Row{100}\\
\end{tabular}
\end{document}

enter image description here

或者对下限阈值使用默认值,对上限阈值使用不同的值:

\documentclass{article}
\usepackage[low,high=1e3]{threshold}
\newcommand{\Row}[1]{#1 & \OldNum{#1} & \num{#1}}
\begin{document}
\begin{tabular}{l l l}
    Num & Old & New\\\hline\\[-0.7em]
    \Row{0.01}\\
    \Row{0.1}\\
    \Row{1}\\
    \Row{10}\\
    \Row{100}\\
\end{tabular}
\end{document}

enter image description here

相关内容