%20seg%C3%BAn%20el%20estado%20de%20la%20macro%20%5Cif.png)
Esta pregunta es básicamente una continuación de dos preguntas anteriores que publiqué hace un año, pero en realidad es una consecuencia de intentar encontrar una solución a esta pregunta:
Utilice conmutadores en biblatex `printbibliography`
Quiero usar un comando con una configuración clave-valor con una \if
declaración (pseudocódigo)
\somekeycommand[\ifdisplay somekey=A \else someotherkey=B]
de modo que se establezca alguna clave específica de acuerdo con el estado de la variable \ifdisplay
.
Desafortunadamente, ninguno de los distintos enfoques funciona, ni siquiera el \begingroup\edef\x{\endgroup\noexpand....}\x
truco tiene éxito.
Tenga en cuenta que yonoquiere usar
\ifdisplay
\somekeycommand[somekey=A]
\else
\somekeycommand[someotherkey=B]
\fi
Lo real \somekeycommand
es un código de un paquete, la macro usa\setkeys
Aquí está el MWE fallido
\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}
Aquí están mis preguntas más antiguas:
Respuesta1
Tenga en cuenta que esto \setkeys
no amplía su segundo argumento. No estoy seguro de lo que buscas, pero esto 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}