%20espec%C3%ADfica%20dependendo%20do%20estado%20da%20macro%20%5Cif.png)
Esta questão é basicamente uma continuação de duas questões mais antigas que publiquei há um ano, mas na verdade é uma consequência da tentativa de encontrar uma solução para esta questão:
Use alternadores no biblatex `printbibliography`
Quero usar um comando com configuração de valor-chave com uma \if
instrução (pseudocódigo)
\somekeycommand[\ifdisplay somekey=A \else someotherkey=B]
de modo que alguma chave específica seja definida de acordo com o estado da variável \ifdisplay
.
Infelizmente, nenhuma das várias abordagens funciona, nem mesmo o \begingroup\edef\x{\endgroup\noexpand....}\x
truque é bem-sucedido.
Por favor, note que eunãoquero usar
\ifdisplay
\somekeycommand[somekey=A]
\else
\somekeycommand[someotherkey=B]
\fi
O real \somekeycommand
é algum código de um pacote, a macro usa\setkeys
Aqui está o MWE com falha
\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}
Aqui estão as minhas perguntas mais antigas:
Responder1
Observe que isso \setkeys
não expande seu segundo argumento. Não tenho certeza do que você está procurando, mas isso funciona.
\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}