3

Please help me how to register callbacks in lualatex. For example, I need to determine when the page of my document was started or ended and the print number of that page*2. I found in the luatex documentation only how to catch when the end of the page, but it does not work for me. Here is my code:

\documentclass{article}
\usepackage{luacode}
\usepackage{polyglossia}
\setmainlanguage[babelshorthands=true]{russian}
\setmainfont{Times New Roman}
\pagestyle{empty}
\thispagestyle{empty}
\begin{luacode}
function f()
tex.print("page "..\thepage*2)
end
id,error=callback.register("finish_pdfpage",f)
tex.sprint(error," ",id)
\end{luacode}
\begin{document}
test
\newpage
new test
\end{document}

But when I compile this document in lualatex, I get the following errors:

C:/Program Files/MiKTeX 2.9/tex/latex/base/ltluatex.lua:109:
Module luatexbase Error: Attempt to use callback.register() directly
(luatexbase)             on input line 14

stack traceback:
        [C]: in function 'error'
        C:/Program Files/MiKTeX 2.9/tex/latex/base/ltluatex.lua:109: in function 'modu
le_error'
        C:/Program Files/MiKTeX 2.9/tex/latex/base/ltluatex.lua:116: in function 'luat
exbase_error'
        C:/Program Files/MiKTeX 2.9/tex/latex/base/ltluatex.lua:320: in function 'regi
ster'
        [\directlua]:4: in main chunk.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }

l.14 \end{luacode}
?

Please also help me to catch the event when a new page is started, e.g. to print the number of this page*2 or the simple number of this page. Thank everybody for help.

  • the ltluatex code used by latex (based on an earlier luatexbase package) implements an allocation scheme for callbacks so different packages can access the callbacks without over-writing each other, but to make that work the primitive callback registration has to be hidden. – David Carlisle Mar 05 '19 at 18:34
  • 3
    you can use texdoc ltluatex to see the documentation latex lua interface. You register a callback with luatexbase.add_to_callback as shown in Ulrike's answer. – David Carlisle Mar 05 '19 at 20:21
  • I tryed use texdoc ltluatex,but it give me some useful information about callbacks,but not so mutch about the tex functions and attributes of tex tables such as: tex.attribute tex.count tex.dimen tex.skip tex.toks etc,. It seems,that i must guess al this attributes. – Aleksandr Kozlovskiy Mar 06 '19 at 14:13
  • For the tex.* just use the raw LuaTeX interface documentation (texdoc luatex). – user202729 Feb 02 '22 at 10:54

1 Answers1

5

You need to use luatexbase.add_to_callback. But you shouldn't try to print something in finish_pdfpage. Here an example that writes to the log:

\documentclass{article}
\usepackage{luacode}
\usepackage{polyglossia}
\setmainlanguage[babelshorthands=true]{russian}
\setmainfont{Times New Roman}
\pagestyle{empty}
\thispagestyle{empty}
\begin{luacode}
function f()
texio.write_nl("HELLO: page "..tex.count["c@page"]*2)
end
id,error=luatexbase.add_to_callback("finish_pdfpage",f,"finish")
\end{luacode}
\begin{document}
test
\newpage
new test
\end{document}
Ulrike Fischer
  • 327,261
  • Ok,now it print number of page,but it seems for me,that it print at the second page. How i can catch end of the page and print information about it and how i can catch a start of new page and print information about it and,if it possible,how i can get content only for this pages? It also not understandeble for ne,how i can get the name of callback,which call my function,because in future i want to use one function with several callbacks. Thanks very mutch everybody for the help. – Aleksandr Kozlovskiy Mar 06 '19 at 14:54