6

This question is basically a follow-up of two older questions I posted a year ago, but actually is a consequence of trying to find a solution to this question:

Use toggles in biblatex `printbibliography`

I want to use a command with a key-value setup with an \if statement (pseudocode)

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

such that some specific key is set according to the state of the variable \ifdisplay.

Unfortunately none of the various approaches work, not even the \begingroup\edef\x{\endgroup\noexpand....}\x - trick is successful.

Please note, that I don't want to use

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

The actual \somekeycommand is some code from a package, the macro uses \setkeys

Here is the failing 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}

enter image description here

Here are the older questions of mine:

  • 2
    \setkeys doesn't expand its second argument. I don't really understand what you're after. – egreg Feb 15 '15 at 22:12
  • 1
    @egreg: Isn't it possible at all? I wonder if it's possible to set either keyA or keyB (the key-value itself is irrelevant for now) depending whether \ifdisplay is evaluated to be true or to be false. The keys are different – user31729 Feb 15 '15 at 22:22
7

Note that \setkeys doesn't expand its second argument. I'm not sure what you're after, but this works.

\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}

enter image description here

  • 1
    I think this is what I want to achieve. And I had something similar, but it failed with some complains about \endgroup. – user31729 Feb 15 '15 at 22:27
  • 2
    Could you answer tex.stackexchange.com/questions/228293/… too? ;-) – user31729 Feb 15 '15 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy