我正在为我的所有个人命令制作一个自定义包,并尝试将一些关键字参数传递给该包,如第 4.4 节所述分类指南。我的用例是我选修数学和物理课程,因此我经常需要更改符号(例如,在数学中,通常用横线表示复共轭,而在物理学中,更常见的符号是星号)。我制作了这个小模板,它几乎可以按照我想要的方式工作:
% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]
\RequirePackage{xstring}
\DeclareKeys[demo]
{
notation.store = \@demo@notation,
notation.usage = load
}
\SetKeys[demo]{notation=physics} % Default to physics notation
\ProcessKeyOptions[demo]
\IfStrEqCase{\@demo@notation}{
{physics}{}
{math}{}
}[
\PackageError{demo-pkg}{Invalid notation value: \@demo@notation}
{Choose value of: physics, math}
]
\RequirePackage{xparse}
\NewDocumentCommand{\Conjugate}{ m }{
\IfStrEqCase{\@demo@notation}{
{physics}{{#1}^{\ast}}
{math}{\overline{#1}}
}
}
% main.tex
\documentclass{article}
\usepackage[notation=math]{demo-pkg}
\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}
当给它一个有效值(如physics
或 )时math
,它会按预期工作。但是,如果我给它一个无效值(如 )engineer
,我会收到错误,但这不是我指定的错误:
Runaway argument?
\PackageError {demo-pkg}{Invalid notation value: \@demo@notation } {\ETC.
! File ended while scanning use of \xs_testcase.
<inserted text>
\par
l.4
I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.
从这个错误中,我看不到无效值是什么,也看不到错误消息或帮助。为什么会发生这种情况?当获取无效参数时,我该如何正确抛出错误?
答案1
在比较的情况之间有一个空格,您不喜欢\IfStrEqCase
它。xstring
\begin{filecontents*}[overwrite]{demo-pkg.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]
\RequirePackage{xstring}
\DeclareKeys[demo]
{
notation.store = \@demo@notation,
notation.usage = load
}
\SetKeys[demo]{notation=physics} % Default to physics notation
\ProcessKeyOptions[demo]
\IfStrEqCase{\@demo@notation}{
{physics}{}%
{math}{}%
}[
\PackageError{demo-pkg}{Invalid notation value: \@demo@notation}
{Choose value of: physics, math}
]
\RequirePackage{xparse}
\NewDocumentCommand{\Conjugate}{ m }{%
\IfStrEqCase{\@demo@notation}{%
{physics}{{#1}^{\ast}}%
{math}{\overline{#1}}%
}%
}
\end{filecontents*}
\documentclass{article}
\usepackage[notation=aa]{demo-pkg}
\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}
您可以使用选择键来简化代码(它也会更有效率,因为每次使用时不会进行比较\Conjugate
):
\begin{filecontents*}[overwrite]{demo-pkg.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]
\DeclareKeys[demo]
{
notation.choice:,
notation/physics.code = \DeclareDocumentCommand\Conjugate{ m }{{##1}^{\ast}},
notation/math.code = \DeclareCommandCopy\Conjugate\overline,
notation/unknown.code =
\PackageError{demo-pkg}{Invalid notation value: #1}
{Choose value of: physics, math},
notation.initial:n = physics,
notation.usage = load
}
\ProcessKeyOptions[demo]
\end{filecontents*}
\documentclass{article}
\usepackage[notation=math]{demo-pkg}
\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}
答案2
您可以使用比 中记录的更多的关键属性clsguide
,其他的属性记录在interface3.pdf
第 27.1 节中,其中之一是.choices:nn
,第 27.3 节给出了其使用示例。
错误消息不会与您在代码中指定的消息完全相同,但我希望它足够好。
% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]
\RequirePackage{xstring}
\DeclareKeys[demo]
{
notation .choices:nn =
{physics,math}
{\ExpandArgs{Nc}\let\@demo@notation{l_keys_choice_tl}}
,notation.usage = load
}
\SetKeys[demo]{notation=physics} % Default to physics notation
\ProcessKeyOptions[demo]
% \RequirePackage{xparse} % not needed with recent kernel versions
\NewDocumentCommand{\Conjugate}{ m }{%
\IfStrEqCase{\@demo@notation}{%
{physics}{{#1}^{\ast}}%
{math}{\overline{#1}}%
}%
}
与@UdiFogiel 类似,这里提出了略有不同的值的用法,并且还将您的包转换为使用 L3 编程语言而不是xstring
。
% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{demo-pkg} {2024-01-06} {0.0} {Demo}
% assuming that you always need only two different notations, the following
% changes it to a boolean variable
\bool_new:N \l__demopkg_notation_physics_bool
\DeclareKeys[demo]
{
notation .choice:
,notation / physics .code:n =
\bool_set_true:N \l__demopkg_notation_physics_bool
,notation / maths .code:n =
\bool_set_false:N \l__demopkg_notation_physics_bool
,notation .usage:n = load
,notation .initial:n = physics
}
\ProcessKeyOptions[demo]
% since `load` doesn't allow changes after the package got loaded, we can just
% hard code the implementations here, instead of deciding on each call
\bool_if:NTF \l__demopkg_notation_physics_bool
{ \NewDocumentCommand \Conjugate { m } { #1 \sp { \ast } } }
{ \NewDocumentCommand \Conjugate { m } { \overline {#1} } }
\begin{advertisement}
您还可以使用expkv
-family 软件包进行选项解析(我是其作者)。有些东西可能更容易expkv
,其他东西则更难(或者不是内置的)。否则,这更多地取决于您喜欢哪种语法。
% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[2024-01-06 v0.0 Demo]
\RequirePackage{expkv-def,expkv-opt}
\ekvdefinekeys{demo}
{
choice notation =
{
% expkv allows much freedom regarding the protected prefix because it
% can work by expansion only (if it's not processing package
% options...), therefore we need to set the protected prefix ourselves
% here if we want to play nice.
protected physics = \let\@demo@physicsTF\@firstoftwo
,protected maths = \let\@demo@physicsTF\@secondoftwo
}
,initial notation = physics
}
\ekvoProcessGlobalOptions{demo} % options provided to \documentclass
\ekvoProcessLocalOptions{demo} % options provided while loading this package
% future options will throw an option conflict error, since we only have a
% single load-time only option this is to be preferred over manually handling
% this. Else we could use `\ekvoProcessFutureOptions` (or combine all three
% calls by using `\ekvoProcessOptions` instead), but we'd need to redefine all
% the keys we don't want to be usable on a second time usage, because there is
% nothing like the `.usage`-property in `expkv`. This could look like the
% following:
% \newcommand\@demo@aux[1]
% {%
% \protected\long\ekvdef{demo-pkg}{#1}
% {%
% \PackageError{demo-pkg}
% {Option `#1' only usable on first package load}{}%
% }%
% }
% \ekvcsvloop\@demo@aux{notation}% if you
% \let\@demo@aux\@demo@undefined
% since `load` doesn't allow changes after the package got loaded, we can just
% hard code the implementations here, instead of deciding on each call
\@demo@physicsTF
{\NewDocumentCommand \Conjugate { m } {#1^{\ast}}}
{\NewDocumentCommand \Conjugate { m } {\overline{#1}}}
\end{advertisement}