How can I create a command \if with 1 argument, namely #1,


that returns symbol1, when #1 is 1, ..., and returns symboln, when #1 is n, and returns nothing, when #1 isn't 1,...,n?

For example, I'd like to use the command


when I'm defining a larger command.

  • It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting.
    – Thorsten
    May 7, 2011 at 17:08
  • @Thorsten: OK, won't greet unnecessarily anymore. However, I don't know what "sign your questions" means, I didn't sign anything.
    – Leo
    May 7, 2011 at 17:28
  • Well I just took one of our text building blocks from meta.tex.stackexchange.com/q/430/3240
    – Thorsten
    May 7, 2011 at 17:30

2 Answers 2


For simple integer comparisons, TeX provides \ifcase:

  % Case 0
  % Case 1
  % Optional

So you can define a wrapper such as


Here, you start from the 0 case and work upward: often you'll see \ifcase#1\relax\or with no 0 case at all. You can use as many \or statements as you like, depending on how many numbers you need. The \else is optional, and is used if you want 'otherwise do this' functionality.

By the way, don't define \if: this is a TeX primitive which you should not change!

  • 1
    hmm, I don't really understand what's going on. You can assume that #1 is always an integer and assume that symbol1...symboln are latex symbols, such as "\mapsto", "\rightarrow", and so on. I was expecting \newcommand{\if}{...} as your answer. Could you give me an example of how to use the command?
    – Leo
    May 7, 2011 at 16:57
  • Oh and another comment, this command "if" that I'm asking for, is primarily meant to be used when defining larger commands, so I don't have to copy paste them, since they are going to be almost identical.
    – Leo
    May 7, 2011 at 17:00
  • @Leon: Does the edit help?
    – Joseph Wright
    May 7, 2011 at 17:08
  • Yes, it's working! For others that might read this, my example from the original question would be witten \ifcase #1 \or \mapsto \or \mapsfrom \or \rightarrow \else \fi. For example \ifcase 2 \or \mapsto \or \mapsfrom \or \rightarrow \else \fi returns \mapsfrom. Thank you, @Joseph Wright.
    – Leo
    May 7, 2011 at 17:26

Using \ifcase as Joseph Wright suggested is the best way most the time.

An alternative which might have benefits for some applications would be to define a set of macros which names dependent on the number. Using \csname ...\endcsname or \@namedef macro names can contain numbers or these can be converted to a Roman number using \romannumeral. Then \@nameuse can be used to use these macros. The etoolbox package also provides the identical user-level macros \csdef and \csuse.



% or alternatively:

% Define cases:
\defcase{0}{code for case 0}
\defcase{1}{code for case 1}
\defcase{2}{code for case 2}

% Usage
% \myswitch{some integer expression}


The use of the eTeX primitive \numexpr makes the whole thing a little more robust to fancy user input.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .