In TeX, a 'write' node is inserted with, say:
\write1{\string\doit{\the\lastypos}}
With pure luatex, a node could be created with:
local n = node.new(8, 1)
n.stream = 1
n.data = <token-list>
According to the manual, the <token-list> is a table representing the token list to be written (with a list of triplets). I couldn't find any documentation about how this list is built. I discovered a string is accepted, but it gets converted to a string'ed list of chars (much like \meaning), so \the\lastypos is written verbatim, not evaluated.
I've found a workaround, shown in the following piece of code:
\setbox0\hbox{\write1{\the\lastxpos}}
\directlua{
for _,d in ipairs(tex.box[0].head.data) do
texio.write(' ** ' .. d[1] .. '/' .. d[2] .. '/' .. d[3])
end
}
I define a box with a \write and then inspect the node. In the real code, instead of printing it I pass it to n.data and primitives work as expected (with some problems in user defined macros).
My question is: how to generate in lua the token list to feed the data field? [Edit. Please, note my questions is not about \lastypos, but about building an arbitrary token list for the data field. Remember also that, because of the asynchronous nature of TeX, page numbers and the like are not known when the 'write' node is created, only when the 'write' is actually output.]
Here is a latex file to make some experiments, with a lua file named extra.lua:
\documentclass{article}
\def\donothing#1{}
\directlua{
require'extra'
}
\setbox0\hbox{\write1{\string\donothing{\the\lastypos}}}
\begin{document}
\directlua{
for _,d in ipairs(tex.box[0].head.data) do
texio.write(' +++ ' .. d[1] .. '/' .. d[2] .. '/' .. d[3])
end
}
\copy0
\copy0
\copy0
\end{document}
The lua file:
local n = node.new(8, 1)
n.stream = 1
n.data = 'abcd#&\\the\\lastxpos'
for _,d in ipairs(n.data) do
texio.write(' *** ' .. d[1] .. '/' .. d[2] .. '/' .. d[3])
end
tex.lastyposto avoid needing to tex-expand\the\lastyposand put the value straight in the whatsit. – David Carlisle Mar 07 '19 at 15:19\lastyposis just an example. There is no\saveposin the examples after all. – Javier Bezos Mar 07 '19 at 15:33lastyposwhen the whatsit is created, not when it's output, after asave_posnode. – Javier Bezos Mar 07 '19 at 15:48\lateluainstead of\directluathen? – Ulrike Fischer Mar 07 '19 at 17:20