
我正在嘗試將 pgfopts 與更經典的選項混合起來來建立自訂類別。這個想法是,經典選項將在這裡提供預先定義的行為,而 pgfopts 選項將在這裡提供更多可自訂的行為。
例如,假設我的班級有一個帶有預定義“紅色”選項的顏色選項,但也提供了 pdfopt 方法來手動設定更多自訂顏色。我的意思是:
\documentclass{myclass}
將是預設行為\documentclass[red]{myclass}
將提供通常的預定義顏色主題\documentclass[maincolor = green]{myclass}
允許使用者提供自己的顏色
我嘗試了以下方法:
% CLASS
% Preamble
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesClass{myclass}[2022/10/11]
\LoadClassWithOptions{article}
% Packages
\RequirePackage{pgfopts}
\RequirePackage{color}
% Pgfoptions
\pgfkeys{
/myclass/.cd,
maincolor/.store in = \maincolor,
maincolor = blue,
}
\ProcessPgfOptions{/myclass}
% Class options
\DeclareOption{red}{\pgfkeyssetvalue{maincolor}{red}}
\ProcessOptions
% Test command
\newcommand{\printcolor}{\textcolor{\maincolor}{\Huge{$\bullet$}}}
產生以下輸出:
\documentclass{myclass}\begin{document}\printcolor\end{document}
=>
blue
[好的]\documentclass[maincolor = green]{myclass}\begin{document}\printcolor\end{document}
=>
green
[好的]\documentclass[maincolor = red]{myclass}\begin{document}\printcolor\end{document}
=>
red
[好的]\documentclass[red]{myclass}\begin{document}\printcolor\end{document}
=>
blue
[問題]
傳統的課程選項不起作用。
問題:如何在這種上下文中混合 pgfopts 和傳統類別選項,以便提供預定義行為\documentclass[someoption]{myclass}
和可自訂行為的靈活性\documentclass[somekey = someoption]{myclass}
答案1
如果你想堅持,pgfopts
你可以使用一個.style
鍵來red
設定你的maincolor
,例如使用以下類別代碼:
% CLASS
% Preamble
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesClass{myclass}[2022/10/11]
\LoadClassWithOptions{article}
% Packages
\RequirePackage{pgfopts}
\RequirePackage{color}
% Pgfoptions
\pgfkeys{
/myclass/.cd,
maincolor/.store in = \maincolor,
maincolor = blue,
red/.style={maincolor=red},
red/.value forbidden
}
\ProcessPgfOptions{/myclass}
% Test command
\newcommand{\printcolor}{\textcolor{\maincolor}{\Huge{$\bullet$}}}
但正如大衛在評論中建議的那樣,使用不同的鍵=值系統可能是更好的主意。以下使用內核命令\DeclareKeys
、\SetKeys
和\ProcessKeyOptions
:
% CLASS
% Preamble
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesClass{myclass}[2022/10/11]
\LoadClassWithOptions{article}
% Packages
\RequirePackage{color}
\DeclareKeys
{
maincolor .store = \maincolor
,red .meta:n = {maincolor=red}
,red .value_forbidden:n = true
}
\SetKeys{maincolor=blue}
\ProcessKeyOptions
% Test command
\newcommand{\printcolor}{\textcolor{\maincolor}{\Huge{$\bullet$}}}
但我個人更喜歡expkv-opt
(好吧,我寫的...)。您expkv
可以定義不採用值的鍵,這比and.value forbidden
支持的方法具有更強的區分性(儘管在大多數情況下這不是很相關,但理論上您可以為有或沒有的鍵定義完全不同的行為這樣的值)。pgfopts
DeclareKeys
% CLASS
% Preamble
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesClass{myclass}[2022/10/11]
\LoadClassWithOptions{article}
% Packages
\RequirePackage{expkv-opt,expkv-def}
\RequirePackage{color}
\ekvdefinekeys{myclass}
{
store maincolor = \maincolor
,initial maincolor = blue
,nmeta red = {maincolor=red}
}
\ekvoProcessGlobalOptions{myclass}% this does the right thing currently and in the future
%\ekvoProcessLocalOptions{myclass}% due to changes in the kernel this doesn't do everything correct in a class file currently -- it might result in a wrongfully thrown "unused global option" error
% Test command
\newcommand{\printcolor}{\textcolor{\maincolor}{\Huge{$\bullet$}}}
答案2
你可以使用
\pgfkeys{
/myclass/.cd,
maincolor/.store in = \maincolor,
maincolor = blue,
red/.code = \def\maincolor{red},
red/.value forbidden
}
因此聲明red
為 pgf(非經典)選項。