2

Help me please,how i can register callbacks in lualatex. For example it need to me catch,when the page of my document started or ended and print number of this page*2. I found in luatex documentation only how to catch,when the end of the page,but it not works 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 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}
?

Also help me please,how i can catch event,when new page started,to print,for example,number of this page*2 or 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 5 at 18:34
  • 1
    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 5 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 6 at 14:13
4

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}
  • 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 6 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.