Problem
I need to define a command that grabs all numerical digits following it and leaves the rest alone. Eventually I came up with this:
First Solution
\RequirePackage{xstring} % for \IfInteger
\newcommand*{\num}{%
\def\@tmpnum{}%
\@num%
}
\newcommand*{\@num}[1]{%
\IfInteger{#1}{%
\edef\@tmpnum{\@tmpnum#1}\@num%
}{%
[I saw the number \@tmpnum!]#1%
}%
}
The \@num macro scans ahead by recursively calling itself. Every digit it encounters is appended to \@tmpnum. The first non-digit it sees ends the sequence and is put back.
It works as intended in simple cases:
\num1234...
outputs:
[I saw the number 1234!]...
How to Handle \par - Second Solution
It soon occurred to me that \IfInteger can't handle a \par, so... second solution:
\newcommand{\@num}[1]{%
\def\@end{%
[I saw the number \@tmpnum!]#1%
}%
\ifdefequal{#1}{\par}\@end{%
\IfInteger{#1}{%
\edef\@tmpnum{\@tmpnum#1}\@num%
}\@end%
}%
}
How to Handle a Closing Brace?
This one I can't figure out. What if I want to accept something like this:
\emph{\num42}
Whatever I try, I get some error about having too many closing braces. I guess I'm putting it back when it was already scanned. But that doesn't really help me.
- Can I test for a closing brace? Is there some other way to handle this?
- Are other such problems waiting for me?

