我在这里问的问题是这个问题的后续用于动态更改 pgfkeys 包选项并定义自定义选项集的宏。
我习惯使用以下技术来指定包选项:
\pgfkeys{
/mypackage/.cd,
firstcolor/.store in = \mypackage@firstcolor,
firstcolor = blue,
secondcolor/.store in = \mypackage@secondcolor,
secondcolor = red,
}
\ProcessPgfPackageOptions{/mypackage}
在回答原始问题时(关联),提供了以下方法:
\pgfkeys{/mypackage/.is family}
\def\mypackage@set#1{\pgfkeys{/mypackage,#1}}
\mypackage@set{
% storage
first-color/.store in=\mypackage@firstcolor,
second-color/.store in=\mypackage@secondcolor,
% default value
first-color=blue,
second-color=red,
mypackage@init/.style/.expanded={
first-color=\mypackage@firstcolor,
second-color=\mypackage@secondcolor
}
}
\ProcessPgfPackageOptions{/mypackage}
\NewDocumentCommand\setcolors{m}{\mypackage@set{#1}}
这使得可以/轻松地将文档中的选项更改为新值,以便我可以输入:
\usepackage[firstcolor = blue]{mypackage}
首先,
\setcolors{firstcolor = red}
稍后。但是我想知道如果我想添加一个zerothcolor
只能在最初设置而不能在稍后设置的,我该怎么做?
问题:换句话说,如何添加一个zerothcolor
选项以便可以\usepackage[zerothcolor = green]{mypackage}
工作,但是\setcolors{zerothcolor = green} does not?
注意:这当然是一个例子,让我了解一般原则,并使其在现实场景中发挥作用,而不仅仅是设置颜色
答案1
如果您使用 LaTeX 内置的 key=value 选项机制,这将是微不足道的,因为它们可以获得有效性,指定密钥仅在包加载时使用,而不是稍后使用。
像这样:
\begin{filecontents}{\jobname.sty}
% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{\@currname}[Demo]
\DeclareKeys[my]
{
first-color .store = \mypackage@firstcolor
,second-color .store = \mypackage@secondcolor
,zeroth-color .store = \mypackage@zerothcolor
,zeroth-color .usage = load
}
\ProcessKeyOptions[my]
\NewDocumentCommand\showvalues{}
{%
0th: \mypackage@zerothcolor,
1st: \mypackage@firstcolor,
2nd: \mypackage@secondcolor
\par
}
\NewDocumentCommand\setcolors{m}{\SetKeys[my]{#1}}
\end{filecontents}
% main.tex
\documentclass[]{article}
\usepackage[zeroth-color=green]{\jobname}
\setcolors{first-color=red, second-color=blue, zeroth-color=yellow} % ERROR
\begin{document}
\showvalues
\end{document}
抛出错误
! LaTeX Error: Key "zeroth-color" may only be used during loading of package
(LaTeX) "my".
我们pgfkeys
可以在处理包选项后重新定义键来模拟这种行为:
\begin{filecontents}{\jobname.sty}
% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{\@currname}[Demo]
\RequirePackage{pgfkeys,pgfopts}
\NewDocumentCommand\setcolors{m}{\pgfqkeys{/mypackage}{#1}}
\setcolors
{
.is family,
% storage
first-color/.store in = \mypackage@firstcolor,
second-color/.store in = \mypackage@secondcolor,
zeroth-color/.store in = \mypackage@zerothcolor,
% default value
first-color=blue,
second-color=red,
mypackage@init/.style/.expanded={
first-color=\mypackage@firstcolor,
second-color=\mypackage@secondcolor
}
}
\ProcessPgfPackageOptions{/mypackage}
\setcolors
{
zeroth-color/.code = \PackageError{mypackage}
{%
Key "zeroth-color" may only be used during loading\MessageBreak
of package "mypackage"%
}%
{}
}
\NewDocumentCommand\showvalues{}
{%
0th: \mypackage@zerothcolor,
1st: \mypackage@firstcolor,
2nd: \mypackage@secondcolor
\par
}
\end{filecontents}
% main.tex
\documentclass[]{article}
\usepackage[zeroth-color=green]{\jobname}
\setcolors{first-color=red, second-color=blue, zeroth-color=yellow} % ERROR
\begin{document}
\showvalues
\end{document}
引发类似的错误:
! Package mypackage Error: Key "zeroth-color" may only be used during loading
(mypackage) of package "mypackage".