# ConTeXt: Cross reference expansion and processing with Lua

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.

A minimal example:

\setupreferencing[state=start]

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

\startluacode
function 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
\startlinenumbering

Counter: \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 can read:

VARIABLE:
5
VARIABLE:
\inlinerange [1]


So 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?

• The definition is \unexpanded\def\inlinerange[#1], i.e. it is not expandable. – Henri Menke Sep 5 '19 at 6:51
• What is it that you are actually trying to do? Accessing cross-references from the Lua level does not make much sense, because they are not defined in the first run. – Henri Menke Sep 6 '19 at 1:54
• Actually, I have to deal with many cross references (showing column and line number) in a two column layout. These references have to be formatted and sorted differently depending on whether the reference is in the same or in an other column as the target. I also did not yet find out how to get the column number of the target (see also this question) and had the idea, as there is a fixed number of lines in each column, to calculate the column number from the line number by division. – OvC Sep 6 '19 at 13:53