# keyvalue with kvoptions and switch structure

I am using kvoptions for a key-value syntax. However I need a switch syntax for the evaluation. Since boolexpr is problamatic (see here) I use now the other solution of this question.

However the switch is not working as expected (always results in the same value).

This is the code that I use:

\documentclass[]{scrbook}

\makeatletter
\RequirePackage{kvoptions-patch}
\RequirePackage{kvoptions}  % options
\RequirePackage{pdftexcmds} % string comparison

\SetupKeyvalOptions{family=demo,prefix=demo@}

% parallel, stacked
\DeclareStringOption[stacked]{style}

\ProcessKeyvalOptions{demo}

\newcommand{\PrintDemoUsingKeys}{%
\ifnum\pdf@strcmp{\demo@style}{parallel}=0%
parallel%
\else\ifnum\pdf@strcmp{\demo@style}{stacked}=0%
stacked%
\else%
\PackageError{templatedemo}{%
\MessageBreak%
value >\tplbugs@style< unkown \MessageBreak%
}{}%
\fi\fi%
}%

% Print code and result using the key-value syntax
\newcommand{\PrintDemo}[2][]{%
%  #1  is the optional keyval argument
%  #2  is a mandatory argument
\begingroup%
\setkeys{demo}{#1}%
%  Do  stuff  with  #2
\PrintDemoUsingKeys%
\endgroup%
}

\makeatother

\begin{document}
\PrintDemo{style=parallel}

\PrintDemo{style=stacked}
\end{document}

-
Your key setting is inside a group, so \PrintDemoUsingKeys will never see any change. –  Joseph Wright Jan 20 '12 at 22:42
@JosephWright: that is right, but even that does not solve the problem. –  Matthias Pospiech Jan 20 '12 at 22:55

There are two errors in your macros: you say

\setkeys{demo}{#1}


and #1 is the optional argument to \PrintDemo, which is by default empty, according to the definition. So nothing is evaluated, of course, if the call is \PrintDemo{whatever}.

But also calling

\PrintDemo[style=parallel]{whatever}


you'll get a surprise: because of the \begingroup and \endgroup pair, the value of \demo@style will be reverted to the default stacked just after executing \endgroup, so before \PrintDemoUsingKeys is expanded. Thus it should be

\newcommand{\PrintDemo}[2][]{%
%  #1  is the optional keyval argument
%  #2  is a mandatory argument
\setkeys{demo}{#1}%
%  Do  stuff  with  #2
\PrintDemoUsingKeys
}


called as

\PrintDemo[style=parallel]{whatever}


If you want to call this as \PrintDemo{style=parallel}, just change the definition into

\newcommand{\PrintDemo}[1]{%
\setkeys{demo}{#1}%
\PrintDemoUsingKeys
}

-
Ok, but I want to call it using \PrintDemo{style=parallel}. I copied the code for the optional argument, but can not remember where it is from. –  Matthias Pospiech Jan 20 '12 at 22:59
@MatthiasPospiech Don't define an optional argument, then. –  egreg Jan 20 '12 at 23:04
The code is from tug.org/TUGboat/tb30-1/tb94wright-keyval.pdf –  Matthias Pospiech Jan 20 '12 at 23:06
It works with your code, nevertheless I would like to discuss how the solution of the documentation is supposed to work. –  Matthias Pospiech Jan 20 '12 at 23:11
@MatthiasPospiech The authors explains clearly that they want to limit the action of setting the keys to that particular instance. If you want to do that, then put \PrintDemoUsingKeys inside the \begingroup-\endgroup pair. –  egreg Jan 20 '12 at 23:19