char to integer cast in latex

The difference here is that \pdfelapsedtime is read only integer, while \benchmarkcount is a read-write register. In both cases, the contents are 'internal integers', and therefore you need \the or \number to insert the values into the input stream. However, TeX will allow you to assign to \benchmarkcount

\benchmarkcount <optional => <integer denotation> <optional space>

so when you use it as you have done, TeX goes looking for a number and fails to find one. On the other hand, you cannot assign \pdfelapsedtime. That means that you simply can't use it as you have, and so TeX complains. Thus while the difference can be explained, the only way you can get the same error is to create a new \pdfelapsedtime which is a TeX count. The problem there is that the value is dynamic, and so the usual copy-and-rename approaches will fail.


I guess that the best approximation you can get is something that's allowed only in the context of a <number> and not "by itself".

With your definition

\newcommand{\elapsedtime}{%
\directlua{
    tex.print((os.clock()-x)*65536)
}}

you push into the token stream a sequence of digits, not an "abstract number". I propose something like

\protected\def\elapsedtime{\numexpr\directlua{
  tex.print(math.floor(os.clock()*65536+0.5))}\relax}

This would not be equivalent to \pdfelapsedtime in two respects:

  1. the error message in case \elapsedtime doesn't appear when LuaTeX is searching for a number is ! You can't use `\numexpr' in vertical mode.

  2. such a wrong usage of \elapsedtime would leave the number in the token stream (\pdfelapsedtime in wrong places wouldn't).

To the contrary, \the\elapsedtime will do what's expected.

Should I want to add a \resetelapsedtime, I would do in \elapsedtime a "conditional subtraction", that is I'd do -x only if x has been given a value.

Tags:

Pdftex

Luatex