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.

4
  • Will there always be only 2 choices, e.g." A or B", for example, or might there be "A or B or C" or even more? Apr 19 '16 at 17:04
  • 1
    There will be more, something between 1 and 10 choices. Apr 19 '16 at 17:14
  • Do you stick to this \ifboolexpr stuff. I think, it could be easier with expl3 to extend this
    – user31729
    Apr 19 '16 at 17:21
  • etoolbox has the means to handle csv lists so it should be doable. (I'm not at home right now so I can't test at the moment, though)
    – cgnieder
    Apr 19 '16 at 17:43
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

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.