パッケージのキー値オプションの設定についてヘルプが必要です

パッケージのキー値オプションの設定についてヘルプが必要です

私は自分のパッケージにkey-svalueオプションを使用したいので、pgfoptsで解決策を試しました。1)例えば次のようにパッケージをロードすることは可能でしょう。

\usepackage[colors=1]{mypack}

2) プリアンブルだけでなく、ユーザーコードのどこかで色を変更するには、次のようにします。

\MPset{colors=2}

3) どの色がアクティブであるかをテストします。

私の実際のパッケージには、言語とメッセージに関するオプションがあと 2 つあるので、pgfopts はやりすぎかもしれません。key-val パッケージで動作するソリューションであれば、喜んで受け入れます。

\ProvidesPackage{mypack}
\RequirePackage{%
xcolor,
pgfopts
}

\def\MPset#1{\pgfqkeys{/MP}{ /#1}}
\MPset{colors/.initial=2}
\MPset{colors/.is choice}
\MPset{colors/1/.code={\def\test{\textcolor{black}{TEST}}}}
\MPset{colors/2/.code={\def\test{\textcolor{red}{TEST}}}}
\MPset{colors/3/.code={\def\test{\textcolor{blue}{TEST}}}}

\ProcessPgfPackageOptions{/MP}
\endinput% mypack.sty

このテスト文書ではmypackを使用します

\documentclass{article}
%\usepackage[colors=1]{mypack}
\usepackage{mypack}
\MPset{colors=2}
\begin{document}
  :\pgfkeysvalueof{colors}:
  \test
\end{document}
\endinput

もし私がmypackをロードしたら

\usepackage[colors=1]{mypack}

このエラーが発生します

! Package pgfkeys Error: I do not know the key '/MP/colors', to which you
  passed '1', and I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.16 \ProcessPgfPackageOptions{/MP}

オプションなしでmypackをロードするとコンパイルされますが、

\pgfkeysvalueof{colors}

空です。コメントを外すと

\test

希望する色のテキストは取得できましたが、どの色が有効であるかをテストすることはできません

答え1

コードの修正バージョンは次のとおりです。

\documentclass{article}
%------------------------------
\usepackage{filecontents}
\begin{filecontents*}{mypack.sty}
\ProvidesPackage{mypack}
\RequirePackage{%
xcolor,
pgfopts
}

\pgfkeys{/MP/.is family}
\def\MPset#1{\pgfkeys{/MP,#1}}
\MPset{
  colors/.is choice,
  colors/1/.code={\def\test{\textcolor{black}{TEST}}},
  colors/2/.code={\def\test{\textcolor{red}{TEST}}},
  colors/3/.code={\def\test{\textcolor{blue}{TEST}}},
  % default value
  colors=2,
}

\ProcessPgfPackageOptions{/MP}
\endinput% mypack.sty
\end{filecontents*}
%------------------------------

\usepackage[colors=3]{mypack}
%\MPset{colors=2}
\begin{document}
  \test
\end{document}

以下は修正版です(試してみる) がお客様の要件に合致します。

  • マクロは TeX-if\MPspecialを使用してMP@uses@italic斜体またはカラーを選択します。
  • キーはTeX-if を true または false にuse italic設定します。MP@uses@italic
  • キーdefine current colorは現在の色を定義し(xcolor 経由)、その現在の名前を に保存します\MP@current@color
  • キーcolorsは 3 つの選択肢 (黒、青、赤) から選択し、 を定義しますMP current color。赤と青の色はuse italicfalse に設定され、黒の色はuse italictrue に設定されます。
\documentclass{article}
%------------------------------
\usepackage{filecontents}
\begin{filecontents*}{mypack.sty}
\ProvidesPackage{mypack}
\RequirePackage{xcolor}
\RequirePackage{pgfopts}

\newif\ifMP@uses@italic
\pgfkeys{/MP/.is family}
\def\MPset#1{\pgfkeys{/MP,#1}}
\MPset{
  % some special macros use italic istead of black
  use italic/.is if=MP@uses@italic,
  use italic=false, % default value
  % define the current color
  define current color/.code={\def\MPcurrent@color@name{#1}\colorlet{current MP color}{#1}},
  % 
  colors/.is choice,
  colors/black/.style={use italic=true,define current color=black},
  colors/red/.style={use italic=false,define current color=red},
  colors/blue/.style={use italic=false,define current color=blue},
  % default value
  colors=red,
}

\def\MPspecial#1{\ifMP@uses@italic\textit{#1}\else\textcolor{current MP color}{#1}\fi}
\def\test{\textcolor{current MP color}{TEST (in \MPcurrent@color@name)}}

\ProcessPgfPackageOptions{/MP}
\endinput% mypack.sty
\end{filecontents*}
%------------------------------
\pagestyle{empty}
\usepackage[colors=red]{mypack}
\begin{document}
\test{} \MPspecial{My text}

\MPset{colors=black}
\test{} \MPspecial{My text}

\MPset{colors=blue}
\test{} \MPspecial{My text}

\end{document}

ここに画像の説明を入力してください

関連情報