# Error during using callback start_page_number in lualatex

I updated miktex and all packages and now i have an error,when i try use callback start_page_number in my document. The error appeared even in very small document,for example in this document:

\documentclass{article}
\usepackage{luacode}
\usepackage{polyglossia}
\setmainlanguage[babelshorthands=true]{russian}
\setmainfont{Times New Roman}
\pagestyle{empty}
\thispagestyle{empty}
\begin{luacode}
function f0()
tex.print("page "..tex.count["c@page"])
end
\end{luacode}
\begin{document}
test
\end{document}


When i compile it,i get errors:

{C:/Users/ksash/AppData/Local/MiKTeX/2.9/pdftex/config/pdftex.map}] (./lua3.aux
! Missing number, treated as zero.
p
l.3 \select@language {russian}

?
! Missing = inserted for \ifnum.
p
l.3 \select@language {russian}

?
! Missing number, treated as zero.
p
l.3 \select@language {russian}

?
! Missing number, treated as zero.
p
l.3 \select@language {russian}

?
! Missing number, treated as zero.
\__bool_p_0:
l.3 \select@language {russian}


Help me please fix this errors (i use russian language,because i want to use it in documents,which contains cyrillic chars). Help me please fix this errors. Thanks everybody for the help.

start_page_number "replaces the code that prints the [ and the page number at the begin of \shipout.". This code sends information to the terminal and the log. It doesn't make much sense to replace it by code that tries to write something in the document. With texio.write_nl e.g. it works fine:

\documentclass{article}
\usepackage{luacode}
\usepackage{polyglossia}
\setmainlanguage{russian}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\pagestyle{empty}
\thispagestyle{empty}
\begin{luacode}
function f0()
texio.write_nl("page "..tex.count["c@page"])
end
\end{luacode}
\begin{document}
test
\end{document}


You are trying to use tex.print in a callback. That will almost never work in the way you expect. To show the issue in this particular case, compile the following modification of your document:

\documentclass{article}
\usepackage{luacode}
\pagestyle{empty}
\thispagestyle{empty}
\begin{luacode}
function f0()
tex.print("page "..tex.count["c@page"])
end
\end{luacode}
\begin{document}
test
\clearpage
X\directlua{tex.print''}X
\end{document}


On the second page, this will print

What happened? After \clearpage LuaTeX finished up the first page and called your callback. This added something to the Lua TeX output buffer and returned. Now the next time \directlua tries to print something, it will be added to the same buffer which will be sent to TeX after the evaluation of \directlua. So the text printed with tex.print during a callback is printed during a random following \directlua use. In you example, polyglossia tries to use LuaTeX to access some number and was confused about page 1 being returned instead.

This demonstrates: Never use tex.print in a callback. The actual behaviour might also change with any update, to quote the manual: (referring to the whole tex.*print family of functions)

The result of using these functions from inside callbacks is undefined at the moment.

Now you could add tokens into the TeX input stream from a callback using the token.put_next interface, but this is very likely not what you are trying to do: It would insert the tokens at a basically random position because the callback is called when \shipout is done, so they would probably end up in the code of the output routine. This routine does not expect random code insertions, so it will either result in another error or add the tokens to the main vertical list. In the last case, the page number would end up in some "random" location after the actual page it refers to because of the asynchronous nature of the TeX output routine.

So how to fix it depends on what you are trying to archive: If you want to print something to the console, see Ulrike Fischer's answer. If you want to print a page number in your TeX document or some other information in the page footer, see for example this question. LuaTeX callbacks operate on a much lower level.