\ifマクロの状態に応じて特定のキー(および値)を設定する

\ifマクロの状態に応じて特定のキー(および値)を設定する

この質問は基本的に、私が 1 年前に投稿した 2 つの古い質問のフォローアップですが、実際にはこの質問に対する解決策を見つけようとした結果です。

biblatex の `printbibliography` でトグルを使用する

\ifステートメント(疑似コード)でキー値設定のコマンドを使用したい

\somekeycommand[\ifdisplay somekey=A \else someotherkey=B]

変数の状態に応じて特定のキーが設定されます\ifdisplay

残念ながら、さまざまなアプローチはどれも機能せず、\begingroup\edef\x{\endgroup\noexpand....}\x- トリックさえも成功しません。

ご注意ください。しない使いたい

\ifdisplay
\somekeycommand[somekey=A]
\else
\somekeycommand[someotherkey=B]
\fi

実際の\somekeycommandコードはパッケージのコードで、マクロは\setkeys

失敗したMWEはこちら

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{xkeyval}

\makeatletter
\define@key{myfam}{somekey}{%
  \def\somekeyvalue{#1}%
}

\define@key{myfam}{someotherkey}{%
  \def\somekeyothervalue{#1}%
}

\makeatother

\NewDocumentCommand{\somekeycommand}{+O{}}{%
  \begingroup
  \setkeys{myfam}{#1}%
  \ifdef{\somekeyvalue}{%
    Key was \somekeyvalue%
  }{%
  }%
  \endgroup
}%

\newif\ifdisplay
\displayfalse

\begin{document}


\somekeycommand[somekey=A]



\edef\x{%
  \expandafter\noexpand\csname ifdisplay\endcsname% 
  somekey=A%
  \noexpand\else%
  someotherkey=B%
  \noexpand\fi%
}%


\somekeycommand[\x] % fails 

\somekeycommand\expandafter[\x] % fails --> expands to [ \expanded value ]

\somekeycommand[\expandafter\begingroup\edef\x{%
  \ifdisplay
 somekey=A%
\else%
someotherkey=B%
\fi%
}\x] %% -> fails, prints someotherkey=B,

\expandafter\somekeycommand[\begingroup\edef\x{%
  \noexpand\ifdisplay
  somekey=A%
  \noexpand\else%
  someotherkey=B%
  \noexpand\fi%
}\x] %% -> fails, prints someotherkey=B,



\end{document}

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

以下は私の以前の質問です:

答え1

2 番目の引数を展開しないことに注意してください\setkeys。何を求めているのかわかりませんが、これは機能します。

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{xkeyval}

\makeatletter
\define@key{myfam}{somekey}{%
  \def\somekeyvalue{#1}%
}

\define@key{myfam}{someotherkey}{%
  \def\somekeyothervalue{#1}%
}

\makeatother

\NewDocumentCommand{\somekeycommand}{+O{}}{%
  \begingroup
  \setkeys{myfam}{#1}%
  \ifdef{\somekeyvalue}
    {Key was \somekeyvalue}
    {\ifdef{\somekeyothervalue}{Other key was \somekeyothervalue}{}}%
  \endgroup
}%

\newif\ifdisplay
\displayfalse

\begin{document}


\begingroup\edef\x{\endgroup\noexpand
  \somekeycommand\expandafter[\ifdisplay somekey=A\else someotherkey=B\fi]%
}\x

\displaytrue

\begingroup\edef\x{\endgroup\noexpand
  \somekeycommand\expandafter[\ifdisplay somekey=A\else someotherkey=B\fi]%
}\x


\end{document}

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

関連情報