Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to make a simple conditional in a macro. If #1 is "1", "2", "3", "4", "5", or "6", then print Text: #1, otherwise, do nothing, even if it is empty. I tried this code:


I cannot even get make it able to check if 1 is present. How can I check #1 is equal to "1", "2", "3", "4", "5", or "6"?

share|improve this question
Is #1 definitely an integer, or does the answer need to cover the case where it's not? Can it be an integer expression (such as 10 - 6) and if so does that need to count? – Joseph Wright Apr 22 '12 at 11:24
Only \macro{1}, \macro{2}, \macro{3}, \macro{4}, \macro{5}, and \macro{6} should count as a match. Something like \macro{15} should not count as any match. Usually, the macro is empty, like \macro{}, I guess is why my code is getting confused. – Village Apr 22 '12 at 11:29
Should \macro{\really\wild\input} be taken into account? – egreg Apr 22 '12 at 12:49

3 Answers 3

up vote 12 down vote accepted

In your comments you indicated that #1 can be empty


would be an error in that case but a simple change to


will make an empty argument be Ok (and count as zero). beware that \macro{ 1} would then be an error as \ifnum0 1=1 does not work as 0 1 is not a valid number. It's possible to insert extra macros to remove spurious spaces if that is needed.

share|improve this answer
From the way the question is phrased, I'd interpret the comparison as essentially string-based (it has to be exactly as given). You could easily get rid of the spaces with \numexpr, or as you say using a macro-based approach. My worry with the \numexpr solution is that something like 1 + 1 would then be valid, but the question suggests it's not. – Joseph Wright Apr 22 '12 at 12:02

For a limited range of integers, with the other possibilities as blank or integer input, I would go with

\catcode`\@=11 %
  \ifnum 0#1 > 0 %
    \ifnum 0#1 < 7 %

used as

share|improve this answer

I hope that I understood your question correctly:

This is \mynumber{0} \mynumber{4} \mynumber{7}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.