[Tex/LaTex] generalize \iftoggle in etoolbox

conditionalsetoolbox

I am using nested loop conditional \iftoggle of etoolbox, is there any other generalized conditional? Usually it works as:

\newtoggle{name}
\settoggle{name}{true}
\iftoggle{name}{do if name is true}{do if name is false}

Now, I would like something like:

\newtoggle{name} % with 5 options, for example
\settoggle{name}{option3} % all the other option are false
\iftoggle{name}{do if option1 is true}{do if option2 is true}{do if option3 is true}{do if option4 is true}{do if option5 is true}

I know there is a solution with nested conditional, but maybe there is other direct solution. Any idea?

Best Answer

Here is a key-value interface, which, as Joseph Wright has said, appears to be the way to get a solution to your problem.

\documentclass{article}
\usepackage{keyreader}
\makeatletter
\def\msg#1{\ifkrdindef\else Option chosen: #1\endgraf\fi}
\krddefinekeys{gitano}{%
  choice/name/option1/{
    option1.do=\msg{1},
    option2.do=\msg{2},
    option3.do=\msg{3},
    option4.do=\msg{4},
    option5.do=\msg{5}
  }
  ;
}
% Example command, to reformat the list, process the keys, and print the results:
\newcommand\gitanooptions[1][]{%
  \krdifblank{#1}{%
    \krdsetkeys{gitano}{name=option1}%
  }{%
    \def\tempa##1=##2=##3\@nil{\unexpanded{##2}}%
    \edef\tempa{\tempa#1==\@nil}%
    \ifx\tempa\@empty
      \krdsetkeys{gitano}{name=option1}%
    \else
      \def\tempb{}%
      \@for\tempa:=\tempa\do{%
        \edef\tempb{%
          \unexpanded\expandafter{\tempb}%
          \ifx\tempb\@empty\else,\fi
          name=\unexpanded\expandafter{\tempa}%
        }%
      }%
      \krdexpanded{\noexpand\krdsetkeys{gitano}{\tempb}}%
    \fi
  }%
}
\makeatother

% Examples:
\begin{document}
\gitanooptions[name={option1,option2}]
\gitanooptions[name]
\gitanooptions[name=option3]
\gitanooptions[name=option4]
\gitanooptions[name={option2,option5}]
\gitanooptions
\end{document}
Related Question