3

I'd like to write a single file with a collection of e.g. questions, but I want to produce various outputs with a different subsets of all questions each.

So at the beginning of my file I want to state which subset should be printed and I just get the according output.

I found this useful question and came up with the following MWE:

\documentclass{article}

\usepackage{etoolbox}

%% toggles
\newtoggle{A}
\newtoggle{B}
\newtoggle{C}

%% chosen toggle
\toggletrue{C}

\newcommand{\question}[2]{%
\ifboolexpr { #1 } { #2 }{}
}

\begin{document}

\question { togl {A} or togl {B} } {
    Question is part of subset A or subset B!
    }{}%
\question { togl {A} or togl {C} } {
    Question is part of subset A or subset C!
    }{}%    

\end{document}

which gets a little clumsy, the more questions and subsets I got. So I actually just want to write

\question { A,B } {Question is part of subset A or subset B!}%

Can this be done?


There seems to be the possibilty to use any to expand the macro, but I can't get it work with the toggles, or at least it doesn't shorten the code.

2 Answers2

2

You can do it with xparse. You'll need to rename \newtoggle, \toggletrue and \togglefalse if you plan to use etoolbox.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
% emulate \newtoggle
\NewDocumentCommand{\newtoggle}{m}
 {
  \bool_new:c { l_twww_toggle_#1_bool }
 }
\NewDocumentCommand{\toggletrue}{m}
 {
  \bool_set_true:c { l_twww_toggle_#1_bool }
 }
\NewDocumentCommand{\togglefalse}{m}
 {
  \bool_set_false:c { l_twww_toggle_#1_bool }
 }
% evaluate booleans 
\NewDocumentCommand{\question}{mm}
 {
  \bool_if:xT { \c_false_bool \clist_map_function:nN { #1 } \twww_add_boolean:n } { #2 }
 }
\cs_generate_variant:Nn \bool_if:nT { x }
\cs_new:Nn \twww_add_boolean:n
 {
  || \use:c { l_twww_toggle_#1_bool }
 }
\ExplSyntaxOff


\newtoggle{A}
\newtoggle{B}
\newtoggle{C}

%% chosen toggle
\toggletrue{C}

\begin{document}

\question{A,B}{Question is part of subset A or subset B!}

\question{A,C}{Question is part of subset A or subset C!}

\question{A,B,C}{Question is part of subset A or subset B or subset C!}

\end{document}

enter image description here

egreg
  • 1,121,712
2

Short and sweet, done with recursion.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}
%% toggles
\newtoggle{A}
\newtoggle{B}
\newtoggle{C}
\newtoggle{D}
%% chosen toggle
\toggletrue{C}

\newcommand{\Question}[3]{%
 \def\toggletmp{F}%
 \Togl #1,\relax\relax%
 \if T\toggletmp #2\else#3\fi%
}
\def\Togl#1,#2\relax{\ifboolexpr {togl #1}%
  {\def\toggletmp{T}}%
  {\if \relax#2\relax\else\Togl#2\relax\fi}}
\begin{document}
\Question {A,B} {
    Question is part of subset A or subset B!}{Neither A nor B}\par
\Question {A,C} {
    Question is part of subset A or subset C!}{Neither A nor C}\par
\Question {C} {
    Question is part of subset C!}{Not C}\par
\Question {A,B,C,E} {
    Question is part of subset A or subset B,C,E!}{Neither A nor B, C, E}\par    
\Question {A,B,D} {
    Question is part of subset A or subset B,D!}{Neither A nor B, D}%
\end{document}

enter image description here