How can I retrieve \thepage (or detect a page change) from Lua in LuaTeX?
You can almost never accurately reference the page counter from within the main document, only during the output routine where the page being shipped out is accurately known. Luatex does not change the basic model of the way TeX pages are output so the same applies in luatex.
In your lua loop you can use
mcount=mycount+1
tex.print('\\includegraphics{something}\\label{graphic-' .. mycount .. '}\\par')
Then every image will have a \label
associated to it and on the next run you can pick up \pageref{graphic-5}
(from Lua or TeX) which will tell you which page the 5th graphic ended up on.
a full example:
\documentclass{article}
\directlua{j=0}
\newcommand\printluapage{%
\directlua{
for i = 1, 60 do
j=j+1
tex.print('\string\\pageref{foo' .. j .. '}\string\\label{foo' .. j .. '}')
tex.print([[\par]]) end}%
}
\begin{document}
\printluapage
New command
\printluapage
New command
\printluapage
\end{document}
This is a very basic solution which just reads the value of the LaTeX counter page in Lua (printing is for testing, but the tex.count statement can be used at other places as well).
\documentclass{article}
\newcommand\printluapage{%
\directlua{tex.print(tex.count['c@page'])}
}
\begin{document}
\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage\printluapage\clearpage
\end{document}
There are two complications here.
Firstly, every call to \directlua
runs immediately, so if you want TeX's usual processing to happen, you need to relinquish control to TeX (and use coroutines or something). So when you have
\directlua{for i = 1, 60 do tex.print(tex.count['c@page'])
tex.print([[\par]]) end}
}
as in the question, this simply puts 60 instances of 42
(or whatever the page number is) and \par
on TeX's input stack, for TeX to read next after the entire \directlua
call is done. (It doesn't result in TeX processing each \par
and the rest of typesetting immediately, even before the loop ends.) Your exact problem is discussed, with solutions, at this question: Concurrently interleaving execution of Lua and TeX in LuaTeX.
Secondly, TeX simply appends items to its vertical lists until a page break becomes inevitable (e.g. there's no possible way to put all the so-far collected material on a single page), so the value of the page counter at the time the material is added isn't necessarily the page that it will end up on (it may end up on the next page for example). The page number is only finally determined when boxes are being shipped out. There are some tricks for dealing with this fact (e.g. have your Lua code run during shipout?), but I don't know these tricks; maybe someone else will answer. :-)
You can get some approximate results by using quantities like \pagetotal
(or from Lua, tex.pagetotal
) which shows how much material has been added to the current page. This is subject to caveats: the first point above about letting TeX do its thing (splitting your Lua code across different calls to \directlua
), and also you may need to deal with shrinkable and stretchable glue, and account for them.