# char to integer cast in latex

I am wondering what an integer in LaTeX is. I take this from pdfTeX release notes (2005-08-01)

• \pdfelapsedtime is a read-only integer that (initially) returns the amount of time passed since the start of this run. This amount is given in scaled seconds': the value 65536 counts as one second. If more time has passed than 32767 seconds, (2^31)-1 will be returned.
• \pdfresettimer updates the internal timer, such that subsequent calls to \pdfelapsedtime will restart from 0.

so I guess the return value from \pdfelapsedtime is an integer. I have to use \the\pdfelapsedtime to cast it to string value (please correct my words to the LaTeX conventions, I hope everybody knows what I mean). To further elucidate my problem, please look at the following MWE:

\documentclass{minimal}

\newcount\benchmarkcount

\begin{document}
\pdfelapsedtime
\benchmarkcount

\end{document}


The first error is ! You can't use '\pdfelapsedtime' in vertical mode. while the second one gives ! Missing number, treated as zero. So an integer is not the same as a counter either.

Is there anything -- let me call it \foo -- that casts a string to become an integer, e.g. \foo\the\pdfelapsedtime.

Or, that would solve my problem as well, is there an analog to \directlua{tex.print(...)} that gives an integer back?

background: I am working on the answer to this question and try to make the lualatex \pdfelapsedtime macro as identical to the pdflatex one as possible.

-
The term I'd use here is 'insert into the input stream'. The same concept applies to, for example, a token register (toks). – Joseph Wright Nov 27 '11 at 17:44

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

\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.

-
Why add a space after \relax? I see no case where it is needed, and plenty of cases where it shouldn't be there. – Bruno Le Floch Nov 27 '11 at 23:10
You're right, I was misleaded by \the. – egreg Nov 27 '11 at 23:13
@egreg: Could you explain, why you would do the conditional subtraction? To avoid the initialization? It would behave even more different from the original \pdfresettimer. – Andy Nov 28 '11 at 16:26
If you subtract x unconditionally, you need to initialize the variable; doing it when loading the package that defines \elapsedtime would make the count start from that moment, while \pdfelapsedtime counts from the start of the job. – egreg Nov 28 '11 at 16:35

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.

-
Your probably want to read either The TeXbook or TeX by Topic on TeX's idea of integer denations and internal integers. – Joseph Wright Nov 27 '11 at 17:43
Doesn't that mean, that I cannot exactly reproduce \pdfelapsedtime in lualatex user space, since I would have to write something that is supposed to be read-only? – Andy Nov 27 '11 at 17:51
@Andy I think so (I'm not LuaTeX expert). If you look at Heiko Oberdiek's pdftexcmds package, he's quite explicit that reproducing primitives is not possible using macros, etc. – Joseph Wright Nov 27 '11 at 17:55