Testing for number
\documentclass{article}
\def\isnum#1{%
\if!\ifnum9<1#1!\else_\fi
\emph{#1}\else#1\fi}
\begin{document}
\isnum{dummy}
\isnum{123}
\end{document}
if #1
is a number we have \ifnum9<1xxx
which s true and therefore empty which leads to \if!!
which is also true and \emph{#1}
is the output. In the other case we have (#1 mybe 0a)
\ifnum9<10a
which is true and leaves a. Therefore we compare \if!a
which is wrong, the reason why now the else part the output is.
Here's a slightly flawed, but slightly more generic thing than you're asking for.
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\newcommand\ifnumber[1]{%
\begingroup
\edef\temp{#1}%
\expandafter\ifstrempty\expandafter{\temp}
{\endgroup\@secondoftwo}
{\expandafter\ifnumber@i\temp\@nnil}%
}
\def\ifnumber@i#1#2\@nnil{%
\if-#1%
\ifstrempty{#2}
{\def\temp{X}}
{\def\temp{#2}}%
\else
\def\temp{#1#2}%
\fi
\afterassignment\ifnumhelper
\count@0\temp\relax\@nnil
\endgroup
}
\def\numrelax{\relax}%
\def\ifnumhelper#1\@nnil{%
\def\temp{#1}%
\ifx\temp\numrelax
\aftergroup\@firstoftwo
\else
\aftergroup\@secondoftwo
\fi
}
\makeatother
\newcommand\testnumber[1]{#1: \ifnumber{#1}{Number}{Not a number}\par}
\begin{document}
\def\foo{-55}
\testnumber{1234}
\testnumber{\foo}
\testnumber{-}
\testnumber{}
\testnumber{1}
\testnumber{1234abc}
\testnumber{abc1234}
\end{document}
It's slightly complicated by checking if the first token in the expansion of the argument is a -
. Unfortunately, it does not work if the argument is a register. (It probably doesn't work in other cases too.)
But from the \ifnumber
macro, you should easily be about to build what you want.
\newcommand\domorestuffifnumber[1]{\ifnumber{#1}{\emph{#1}}{#1}}
A LuaTeX solution:
\documentclass{article}
\begin{document}
\directlua{ dofile("myluastuff.lua") }
\newcommand\domorestuffifnumber[1]{%
\directlua{
domorestuffifnumber("#1")
}}
\def\foo{-55}
\domorestuffifnumber{1234}
\domorestuffifnumber{\foo}
\domorestuffifnumber{-}
\domorestuffifnumber{}
\domorestuffifnumber{1}
\domorestuffifnumber{1234abc}
\domorestuffifnumber{abc1234}
\end{document}
and the myluastuff.lua
file:
function domorestuffifnumber( arg )
if tonumber(arg) then
tex.sprint("\\emph{" .. arg .. "}")
else
tex.sprint(arg or "")
end
end
I think the solution is quite readable.