[NTG-context] Cross-reference expansion with ConTeXt and processing with LuaTeX

Hans Hagen j.hagen at xs4all.nl
Thu Sep 5 14:41:42 CEST 2019


On 9/4/2019 7:37 PM, Oliver von Criegern wrote:
> Dear list members,
> 
> I want to process cross-references in ConTeXt/LuaTeX, that is, to do 
> some calculations in Lua with the page and line numbers returned by the 
> \at and \inlinerange commands.
> 
> But this does not work, as these commands are obviously not being 
> expanded before they are passed to Lua.
> 
> Here is a minimal example:
> 
> 
> |\setupreferencing[state=start]||\definenumber[test]\setnumber[test][5]\startluacodefunction 
> TestVar(var)print('VARIABLE: ')print(var)tex.print(var)end 
> \stopluacode\def\TestLua#1{\ctxlua{TestVar([==[#1]==])}}% Cf. 
> https://www.contextgarden.net/Programming_in_LuaTeX\startbuffer[testtext]\dorecurse{100}{Words, 
> words, words. }\stopbuffer\starttext\startlinenumberingCounter: 
> \TestLua{\rawcountervalue[test]}\par% The counter is expanded before 
> being passed to Lua.Lineref: \TestLua{\inlinerange[1]}\par% The line 
> reference is not expanded before being passed to 
> Lua.\getbuffer[testtext]\someline[1]LABEL 1 \stoplinenumbering\stoptext|||
> 
> 
> \TestLua just passes the argument to the Lua command TestVar, which 
> prints it to stdout and returns it to TeX. The pdf result looks good at 
> first glance, showing the correct values as expected, but in stdout we read:
> 
> |VARIABLE: 5 VARIABLE: \inlinerange[1] |||
> 
> The line reference obviously has not been expanded before but only after 
> having been passed to the Lua command. Accordingly, when trying to 
> perform mathematical calculations on it in Lua, there will be an error.
> 
> So how can I pass the value of my reference to Lua?

\setupreferencing[state=start]

\definenumber[test]
\setnumber[test][5]

\startluacode
function TestVarA(var)
     print('VARIABLE A: ',var)
     context(var)
end

function TestVarB(var)
     print('VARIABLE B: ',var)
     context.inlinerange { var }
end
\stopluacode

\def\TestLuaA#1{\ctxlua{TestVarA(#1)}}
\def\TestLuaB#1{\ctxlua{TestVarB(#1)}}

\startbuffer[testtext]
     \dorecurse{100}{Words, words, words. }
\stopbuffer

\starttext

\startlinenumbering

Counter: \TestLuaA{\rawcountervalue[test]}\par
Lineref: \TestLuaB{1}                     \par

\getbuffer[testtext]

\someline[1]LABEL 1

\stoplinenumbering


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the ntg-context mailing list