
私は 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
[問題]
従来のクラスオプションは機能しません。
質問:\documentclass[someoption]{myclass}
このようなコンテキストでpgfoptsと従来のクラスオプションを組み合わせて、定義済みの動作とカスタマイズ可能な動作の両方の柔軟性を提供する方法\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$}}}
しかし、David がコメントで示唆しているように、別の key=value システムを使用する方が良いかもしれません。以下では、カーネル コマンド\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
値を持たないキーを定義できるため、および.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 (クラシックではない) オプションとして宣言します。