[Tex/LaTex] math conditionals using (if – else – fi) command


I saw these command when i searching sth. I wonder if we can increase numbers if conditions.

it's like if{0} = a, if{1}=b, if{2}=c, if{3}=d etc.

\if{#1 != 0}
Some text, because it's nonzero
Some other text, because it's zero


Best Answer

You can do much better: a \newcasecommand that can deal with arbitrary labels, not only numbers.


\NewDocumentCommand{\newcasecommand}{ m m O{No~default~value} }
  \cs_new_protected:Npn #1 ##1
    \str_case:nnF { ##1 } { #2 } { #3 }

 {0}{The number is zero}
 {1}{The number is one}
 {x}{We have x}







The syntax is

  {<string-a>}{<text for case a>}
  {<string-b>}{<text for case b>}
 }[<text for no match>]

The final argument is optional; the default is printing an informative text, which could be turned into a warning.

enter image description here