4

Various languages have constructs like

select;
   when <expression 1> then
      <action 1>;
   when <expression 2> then
      <action 2>;
   ...
   when <expression n> then
      <action n>;
   otherwise;
      <action false>;
   end;

or

if (<expression 1>) {
  <action 1>
  }
elsif (<expression 2>) {
  <action 2>
  }
...
elsif (<expression n>) {
  <action n>
  }
else {
  <action false>
  }

for evaluating a sequence of expressions and performing the first action for which the expression evaluates as true. I would have expected the expl3 kernel to include \case:n and \case:nTF for this purpose, but it does not. Is there any work in progress to add such a construct?

  • 3
    They are separated by variable type. There is \int_case for integers, \str_case for strings, \tl_case for token lists, and \dim_case for dimensions. – Phelype Oleinik Feb 8 at 19:28
  • 1
    You can then use \bool_if:nTF {<logical expression>} {<true code>} {<false code>} and nest them at will. – Phelype Oleinik Feb 8 at 19:46
  • 1
  • 1
    @StevenB.Segletes If I understood correctly, OP wants the same operation as \bool_if:nTF but that instead of if {<test>} { <true> } { if { <other test> } { <true> } { <nesting> } } he wants it all in one level: if { <test> } { <true> } elseif { <other test> } { <true> } elseif ... fi. The overall result would be the same, but the code wouldn't be (explicitly) nested. – Phelype Oleinik Feb 8 at 20:21
  • 1
    that's the wrong repo, but Frank already moved your issue. – Ulrike Fischer Feb 8 at 20:56
3

It's easy to make such a construct, I may not have quite the official naming scheme, but that could be arranged

enter image description here

\documentclass{article}

\usepackage{expl3}
\begin{document}


\ExplSyntaxOn
\def\zz#1\zzz{}\def\zzz{}

two ~true ~just ~do ~first ~
aaa ~ \bool_if:nT {\int_compare_p:n{3=1}} {3 ~ is ~ 1\zz}
      \bool_if:nT {\int_compare_p:n{3=2}} {3 ~ is ~ 2\zz}
      \bool_if:nT {\int_compare_p:n{3=3}} {3 ~ is ~ 3\zz}
      \bool_if:nT {\int_compare_p:n{3=4}} {3 ~ is ~ 4\zz}
      \bool_if:nT {\int_compare_p:n{1=1}} {1 ~ is ~ 1}
      \zzz \ bbb

\par

one ~true ~
aaa ~ \bool_if:nT {\int_compare_p:n{3=1}} {3 ~ is ~ 1\zz}
      \bool_if:nT {\int_compare_p:n{3=2}} {3 ~ is ~ 2\zz}
      \bool_if:nT {\int_compare_p:n{3=4}} {3 ~ is ~ 4\zz}
      \bool_if:nT {\int_compare_p:n{3=4}} {3 ~ is ~ 4\zz}
      \bool_if:nT {\int_compare_p:n{1=1}} {1 ~ is ~ 1}
      \zzz \ bbb

\par

none ~true ~
aaa ~ \bool_if:nT {\int_compare_p:n{3=1}} {3 ~ is ~ 1\zz}
      \bool_if:nT {\int_compare_p:n{3=2}} {3 ~ is ~ 2\zz}
      \bool_if:nT {\int_compare_p:n{3=4}} {3 ~ is ~ 4\zz}
      \bool_if:nT {\int_compare_p:n{3=4}} {3 ~ is ~ 4\zz}
      \bool_if:nT {\int_compare_p:n{1=4}} {1 ~ is ~ 4}
      \zzz \ bbb

\ExplSyntaxOff


\end{document}
  • the first has only one true too. – Ulrike Fischer Feb 8 at 21:57
  • You could change \zz to \prg_break: and \zzz to \prg_break_point:, as these are already provided by expl3. – Bruno Le Floch Feb 10 at 7:39
  • @BrunoLeFloch I probably should:-) – David Carlisle Feb 10 at 9:52

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.