passing variables/data between context and lua
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} \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
\starttext \define\question{44} \startluacode -- see "ConTeXt Lua Documents" manual, §7.4 Looking inside local question = tokens.getters.macro("question") -- it's up to you to verify that \question contains a number -- better using the userdata table for your data userdata.points = tonumber( question ) * 92 -- do some calculations with points and return to to context userdata.answer=42 \stopluacode % lots of Text in between The answer is \ctxlua{ context(userdata.answer) }. Points are \ctxlua{ context(userdata.points) }. \stoptext Best wishes, Massi Il 27/09/19 06:59, Martin Althoff ha scritto:
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}
\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
___________________________________________________________________________________ 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 ___________________________________________________________________________________
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
Am 2019-09-27 um 14:58 schrieb Wolfgang Schuster
: \define\question{44} \defineexpandable\question{44}
Would it make sense to use a counter or some other TeX construct? Is it better to use \setvariable(s) for simple values than any \def? Should I use a \newdimen for every custom measure that I want to use with \dimexpr, e.g. \def\MyWidth{10mm} or better \newdimen\MyWidth \MyWidth=10mm ? Greetlings, Hraban --- https://www.fiee.net http://wiki.contextgarden.net https://www.dreiviertelhaus.de GPG Key ID 1C9B22FD
Henning Hraban Ramm schrieb am 28.09.2019 um 08:53:
Am 2019-09-27 um 14:58 schrieb Wolfgang Schuster
: \define\question{44} \defineexpandable\question{44} Would it make sense to use a counter or some other TeX construct?
Is it better to use \setvariable(s) for simple values than any \def? Module: \def etc.
Document/Style: \setvariable(s) because \getvariable works also for unknown keys.
Should I use a \newdimen for every custom measure that I want to use with \dimexpr, e.g.
\def\MyWidth{10mm} or better \newdimen\MyWidth \MyWidth=10mm ? \definemeasure [MyWIdth] [10mm]
\setupsomething[width=\measure{Mywidth}] Wolfgang
Hi Hraban, I can't answer your question counter or \define. However in my specific situation I would not use the number as a counter but just as a value. Actually my situation is an exam in which the total points are 44. From these I want to calculate percentages. And, as I find it easier to write calculations (electro physics and maths) in Lua, I then want to pass the results out to Context. Greetings, Martin
\define\question{44} \defineexpandable\question{44}
Would it make sense to use a counter or some other TeX construct?
Is it better to use \setvariable(s) for simple values than any \def?
Should I use a \newdimen for every custom measure that I want to use with \dimexpr, e.g.
\def\MyWidth{10mm} or better \newdimen\MyWidth \MyWidth=10mm ?
Thanks for the quick answers. Both suggestions work for me. @Massi Thanks for the pointer into the cld-mkiv.pdf @Wolfgang: I have to admit that I don't completely comprehend your solution, though it works. Searching around for the commands you use, I came across an older posting you answered too: https://mailman.ntg.nl/pipermail/ntg-context/2012/064326.html That helps too. However I can find little documentation on the \setvariable/s commands. I'll play around with the solution to understand it better.
\defineexpandable\question{44}
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
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
Hello Wolfgang, what exactly does the command "\setvariable{martin}{set}{\directsetup{martin}}"? I don't understand it. Has it something to do with "Command/datasetvariable"? Especially what does "\directsetup"? Rudolf
Rudolf Bahr schrieb am 06.10.2019 um 07:11:
Hello Wolfgang, what exactly does the command "\setvariable{martin}{set}{\directsetup{martin}}"? Let's start with a simple example where I set a values with the \setvariables command.
\setvariables [metadata] [author=Rudolf Bahr, date=\currentdate] \starttext \getvariable{metadata}{author}\par \getvariable{metadata}{date} \stoptext The main point of the command is that you can choose whatever name you want for the keys to store the values. Instead a using a list of values to store in a key-value-method you can also pass values to single keys with the \setvariable command, e.g. \setvariable{metatdata}{author}{Rudolf Bahr}. While it is true that you're free to choose the name for the keys there are two keys with a special meaning, these two keys are "reset" and "set". The value of the "reset" key is used before the values in \setvariables are stored while "set" is used after the values are stored.
I don't understand it. Has it something to do with "Command/datasetvariable"? No, this is a different mechanism to save values between different ConTeXt runs which allows one to access values before they are set. Especially what does "\directsetup"? When you access the content of a setups-environment you can use the \setups command (with a trailing s) to pass a list of setups or the \setup command, both commands allow you to use braces or brackets as delimiters for the argument. The \directsetup skips the check for the argument form and allows only braces as delimiter.
\startsetups[example-1] \startparagraph First example! \stopparagraph \stopsetups \startsetups[example-2] \startparagraph Second example! \stopparagraph \stopsetups \starttext \setups[example-1,example-2] \blank \setups{example-1,example-2} \blank \setup[example-1] \setup[example-2] \blank \setup{example-1} \setup{example-2} \blank \directsetup{example-1} \directsetup{example-2} \stoptext Wolfgang
On Sun, Oct 06, 2019 at 10:44:12PM +0200, Wolfgang Schuster wrote:
While it is true that you're free to choose the name for the keys there are two keys with a special meaning, these two keys are "reset" and "set". The value of the "reset" key is used before the values in \setvariables are stored while "set" is used after the values are stored.
Hello Wolfgang, thank you very much for your detailed answer and your two examples! Concerning the two keys with a special meaning, "reset" and "set", remains a question to me. For what are they good? Does "reset" clear a data area, namespace perhaps, before new data are stored there? If they are keys, what is there value or are they commands? Best regards, Rudolf
On Tue, 8 Oct 2019, Rudolf Bahr wrote:
On Sun, Oct 06, 2019 at 10:44:12PM +0200, Wolfgang Schuster wrote:
While it is true that you're free to choose the name for the keys there are two keys with a special meaning, these two keys are "reset" and "set". The value of the "reset" key is used before the values in \setvariables are stored while "set" is used after the values are stored.
Hello Wolfgang,
thank you very much for your detailed answer and your two examples!
Concerning the two keys with a special meaning, "reset" and "set", remains a question to me. For what are they good? Does "reset" clear a data area, namespace perhaps, before new data are stored there? If they are keys, what is there value or are they commands?
Let's start with a simple example: \setvariables [dummy] [ set={AAA}, reset={BBB}, ] \starttext Hello World! \setvariables[dummy][key=value] \stoptext When the \setvariables[dummy][key=value] is called, it first executes the value of `reset` key. So the output of the above is ``` Hello World! BBBAAA ``` Now suppose you want to do some calculations based on the value of passed key. Let's take a variation of the original question, and define a macro where we can say \setvariables[exam][points=5] \setvariables[exam][points=10] and we can a macro \totalpoints to show the number of points. (There are probably better ways to do this than setvariables ...) Here is one way to define it: \def\totalpoints{0} \setvariables [exam] [ points=0, % This is important. set={\edef\totalpoints{\the\numexpr(\totalpoints + \getvariable{exam}{points})\relax}}, ] \starttext \setvariables[exam][points=5] \setvariables[exam][points=10] The exam has \totalpoints\ points. \stoptext So, everytime \setvariables is call, the value of the `set` key is executed. If I set the value of the `set` key to code that updates the value of `\totalpoints` macro, then I get the desired behavior. Writing macros as value of a key can be cumbersome to read. So, we can use setups: \def\totalpoints{0} \startsetups updatepoints \edef\totalpoints{\the\numexpr(\totalpoints + \getvariable{exam}{points})\relax} \stopsetups \setvariables [exam] [ points=0, set=\directsetup{updatepoints}, ] \starttext \setvariables[exam][points=5] \setvariables[exam][points=10] The exam has \totalpoints\ points. \stoptext That's it. Aditya
On Tue, Oct 08, 2019 at 08:55:30AM -0400, Aditya Mahajan wrote:
So, everytime \setvariables is call, the value of the `set` key is executed.
Hello Aditya, this is the key sentence to understand better what "reset" and "set" are good for! I had to read your explanation thrice :-) and run your examples which are really good! Now I have an idea how to use "\setvariables" in a context project. Thank you very much! Rudolf
participants (6)
-
Aditya Mahajan
-
Henning Hraban Ramm
-
Martin Althoff
-
mf
-
Rudolf Bahr
-
Wolfgang Schuster