Print

Print


Building on the TeX.SE posts "Is there a \bool_case:Nnn?"
(http://tex.stackexchange.com/q/121074/2966) and Joseph Wright's
answer to "How do the \xx_case: functions work?"
(http://tex.stackexchange.com/q/121390/2966), here is \bool_case:nn,
proposed for addition:

\documentclass{minimal}
\usepackage{expl3, xparse}

\ExplSyntaxOn

\cs_new:Npn \bool_case:nn #1#2
  {
    \tex_romannumeral:D
      \__bool_case:w #1 \c_true_bool {#2} \q_recursion_stop
  }
\cs_new:Npn \__bool_case:w #1#2
  {
    \bool_if:nTF { #1 }
      { \__bool_case_end:nw {#2} }
      { \__bool_case:w }
  }
\cs_new_eq:NN \__bool_case_end:nw \__prg_case_end:nw

\NewDocumentCommand \testboolcase {}
  {
    \bool_set_false:N \l_tmpa_bool
    \bool_set_true:N \l_tmpb_bool

    \bool_case:nn
      {
        \l_tmpa_bool { false;~ can't~ happen }
        \l_tmpb_bool { true;~ should~ happen }
      }
      { else;~ can't~ happen }
  }

\NewDocumentCommand \testboolcaseelse {}
  {
    \bool_set_false:N \l_tmpa_bool
    \bool_set_true:N \l_tmpb_bool

    \bool_case:nn
      {
        \l_tmpa_bool { false;~ can't~ happen }
        { !\l_tmpb_bool } { false;~ can't~ happen }
      }
      { else;~ should~ happen }
  }

\ExplSyntaxOff

\begin{document}
\testboolcase

\testboolcaseelse
\end{document}