Is the argument a number ?#

’s own lexical analysis doesn’t offer the macro programmer terribly much support : while category codes will distinguish letters (or what currently thinks of as letters) from everything else, there’s no support for analysing numbers.

The simple-minded solution is to compare numeric characters with the characters of the argument, one by one, by a sequence of direct tests, and to declare the argument « not a number » if any character fails all comparisons :


which one would then use in a tail-recursing macro to gobble an argument. One could do slightly better by assuming (pretty safely) that the digits’ character codes are consecutive :

\ifnum''#1<''0 \isanumfalse
\else\ifnum''#1>''9 \isanumfalse

again used in tail-recursion. However, these forms aren’t very satisfactory : getting the recursion « right » is troublesome (it has a tendency to gobble spaces in the argument), and in any case itself has mechanisms for reading numbers, and it would be nice to use them.

Donald Arseneau’s cite package offers the following test for an argument being a strictly positive integer :

  \ifcat//\ifnum0<0#1 //\else A\fi

which can be adapted to a test for a non-negative integer thus :

  \ifcat//\ifnum9<1#1 //\else A\fi

or a test for any integer :

  \ifcat//\ifnum9<1\gobbleminus#1 //\else A\fi

but this surely stretches the technique further than is reasonable.

If we don’t care about the sign, we can use to remove the entire number (sign and all) from the input stream, and then look at what’s left :

\def\testnum#1{\afterassignment\testresult\count255=0#1 \end}

(which technique is due to David Kastrup; the trick for avoiding the errors, noted in an earlier version of this answer, was suggested by Andreas Matthias). In a later thread on the same topic, Michael Downes offered :

  \begingroup \lccode''\-=''\0 \lccod+=''\0
    \lccode''\1=''\0 \lccode''\2=''\0 \lccode''\3=''\0
    \lccode''\4=''\0 \lccode''\5=''\0 \lccode''\6=''\0
    \lccode''\7=''\0 \lccode''\8=''\0 \lccode''\9=''\0

which relies on \romannumeral producing an empty result if its argument is zero. Sadly, this technique has the unfortunate property that it accepts simple expressions such as 1+2-3; this could be solved by an initial \gobbleminus-like construction.

All the complete functions above are designed to be used in conditionals written « naturally » — for example :

\if\IsInteger{⟨subject of test⟩}%
  ⟨deal with integer⟩%
  ⟨deal with non-integer⟩%

The memoir class has an internal command of its own, \checkifinteger{num}, that sets the conditional command \ifinteger according to whether the argument was an integer.

Of course, all this kerfuffle would be (essentially) void if there was a simple means of « catching errors. Imagining an error-catching primitive \ifnoerror, one might write :

% carries on if no error
% here if there was an error

thus using ’s own integer-parsing code to do the check. It’s a pity that such a mechanism was never defined (it could be that it’s impossible to program within !).