How to register callbacks in lualatex












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.










share|improve this question

























  • 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
















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.










share|improve this question

























  • 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














2












2








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.










share|improve this question
















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.







luatex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 5 at 17:28









Phelype Oleinik

24.1k54688




24.1k54688










asked Mar 5 at 17:26









Aleksandr KozlovskiyAleksandr Kozlovskiy

411




411













  • 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



















  • 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

















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





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




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





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





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










1 Answer
1






active

oldest

votes


















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}





share|improve this answer
























  • 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








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477901%2fhow-to-register-callbacks-in-lualatex%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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}





share|improve this answer
























  • 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
















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}





share|improve this answer
























  • 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














4












4








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}





share|improve this answer













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}






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 5 at 17:48









Ulrike FischerUlrike Fischer

195k8302688




195k8302688













  • 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



















  • 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

















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





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


















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477901%2fhow-to-register-callbacks-in-lualatex%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?