4

Is there a way to implement a switch-like macro in LaTeX or do I have to nest ifs?

Something that would look like:

 \ifcase#1
   \case a{}
   \or\case b{}
   \or\case c{}
 \fi

If not, could you please point me to a solution. Preferably, something that could run on vanilla-LaTeX?

lockstep
  • 250,273
Minustar
  • 1,052
  • Yes. See http://tex.stackexchange.com/questions/17676/conditional-cases-expression – Przemysław Scherwentke Jun 25 '14 at 13:30
  • 1
    Another possibility: Define macros \csname mycase@a\endcsname etc. and then just call \csname mycase@#1\endcsname. – Stephan Lehmke Jun 25 '14 at 13:40
  • @StephanLehmke that's Martin's answer in the question linked in the first comment (but my answer here is more specific to single characters as requested in the question:-) – David Carlisle Jun 25 '14 at 13:51
  • @DavidCarlisle Only in the most general sense, as Martin's solution is very much geared towards numerals (and numexpressions). It might not be obvious to everybody how to transfer it to arbitrary characters. Btw, I read the question to refer to arbitrary (selections of) characters, not a,b,c specifically. – Stephan Lehmke Jun 25 '14 at 14:26

1 Answers1

6
\def\foo#1{%
 \ifcase\numexpr`#1 - `a\relax
       case a
   \or case b
   \or case c
 \fi}

then \foo{b} probably expands to case b

David Carlisle
  • 757,742