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?

  • 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
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

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.