[NTG-context] passing variables/data between context and lua

Wolfgang Schuster wolfgang.schuster.lists at gmail.com
Fri Sep 27 14:58:26 CEST 2019


Martin Althoff schrieb am 27.09.2019 um 06:59:
> Hi, I am trying to pass variables/data between context and lua. Lua would be used for
> calculations on data coming from context.
>
> Unfortunately I can't figure out what to do. The code below doesn't work, but is there to
> illustrate my idea.
>
> Any help would be much appreciated. Thanks, Martin
>
>
> \define\question{44}
\defineexpandable\question{44}
> \starttext
>
> \startluacode
> local points = \question * 92
> -- do some calculations with points and return to to context
> answer=42
> \stopluacode
>
> % lots of Text in between
> The answer is \answer
>
> \stoptext
A solution which uses only existing commands in the document.

\startsetups [martin]
     \startlua
         local points = tonumber(\getvariable{martin}{question}) or 0 ;
               points = points * 92 ;
         local answer = 42 ;
         context.setvariable("martin","points",points) ;
         context.setvariable("martin","answer",answer) ;
     \stoplua
\stopsetups

\setvariable{martin}{set}{\directsetup{martin}}

\starttext

\setvariables [martin] [question=44]

Points: \getvariable{martin}{points}

The answer is \getvariable{martin}{answer}.

\stoptext

Wolfgang



More information about the ntg-context mailing list