ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Cross-reference expansion with ConTeXt and processing with LuaTeX
@ 2019-09-04 17:37 Oliver von Criegern
  2019-09-05 12:41 ` Hans Hagen
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver von Criegern @ 2019-09-04 17:37 UTC (permalink / raw)
  To: ntg-context


[-- Attachment #1.1: Type: text/plain, Size: 1537 bytes --]

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?

Best regards,
Oliver


||


[-- Attachment #1.2: Type: text/html, Size: 5089 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Cross-reference expansion with ConTeXt and processing with LuaTeX
  2019-09-04 17:37 Cross-reference expansion with ConTeXt and processing with LuaTeX Oliver von Criegern
@ 2019-09-05 12:41 ` Hans Hagen
  2019-09-06 14:26   ` Oliver von Criegern
  0 siblings, 1 reply; 3+ messages in thread
From: Hans Hagen @ 2019-09-05 12:41 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Oliver von Criegern

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
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Cross-reference expansion with ConTeXt and processing with LuaTeX
  2019-09-05 12:41 ` Hans Hagen
@ 2019-09-06 14:26   ` Oliver von Criegern
  0 siblings, 0 replies; 3+ messages in thread
From: Oliver von Criegern @ 2019-09-06 14:26 UTC (permalink / raw)
  To: Hans Hagen, mailing list for ConTeXt users

Many thanks for your response, but unfortunately it does not solve my 
problem. In your solution, var in TestVarB is the identifier of the 
reference, which is "1", but I need the value of the reference, that is, 
the number of the line where the \someline command is being called, 
which is "31" or so. I want to access the value in Lua to do some 
calculations on it before returning the result to TeX.

Best regards,
Oliver

Am 05.09.19 um 14:41 schrieb Hans Hagen:
> On 9/4/2019 7:37 PM, Oliver von Criegern wrote:
>> 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
> -----------------------------------------------------------------

-- 
Dr. Oliver von Criegern
Referat für IT und Digital Humanities
Bayerische Akademie der Wissenschaften
Alfons-Goppel-Str. 11
80539 München
Tel.: 089-23031-1310

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-09-06 14:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-04 17:37 Cross-reference expansion with ConTeXt and processing with LuaTeX Oliver von Criegern
2019-09-05 12:41 ` Hans Hagen
2019-09-06 14:26   ` Oliver von Criegern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).