Changes

Jump to navigation Jump to search
1,028 bytes added ,  15:31, 14 December 2020
m
Improved examples
[ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf
inside your ConTeXt directory.
 
= Macro arguments (parameters) =
Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.
Next, for commands which use specific delimiters, such as the {{code|\MyGoTo}} command defined below, then one has to use the construction {{code|context.MyGoTo({"Myfirstargument"},{"Mysecondargument"})}}.
Finally another construction (which works also for the above cases) which is there more specifically for some Lua reserved words (such as {{code|goto, end, if}}, present also as TeX commands) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.
All this being said, it is better to give some examples below (try the code by putting and removing the Lua comment signs --)
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}
\define[2]\YourGoTo{\goto{#1}[#2]}
\starttextstartTEXpage[offset=5mm]
\startchapter[title={Ward},reference={ch:ward}]
\startluacode
context.index("Knuth")
-- context.index("Ward")
context["index"]("Ward")
-- context.YourGoTo("Index","ref:index")
-- context.YourGoTo(s,t)
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a rserved reserved word in Lua
\stopluacode
\stoptitle
\stoptextstopTEXpage</context> = Variables = For setting variables command{{cmd|setvariable}} is used. One can retrieve the value with {{cmd|getvariable}}. == From TeX to Lua == Setting variable on TeX end retrieving it at Lua side: <context source=yes mode=mkiv>\startTEXpage[offset=5mm] \setvariable{Namespace}{Key}{30} \startluacodelocal value = tokens.getters.macro(tokens.getters.macro("??variables") .. "Namespace:Key")value = 3*valuecontext("Type: " .. type(value))context.par()context("Result is " .. value)\stopluacode \stopTEXpage
</context>
 
 
== From Lua to TeX ==
 
Setting variable in Lua and retrieving it at TeX side:
 
<context source=yes mode=mkiv>
\startTEXpage[offset=5mm]
 
\startluacode
local value = 30
value = 3 * value
context("Type: " .. type(value))
context.par()
context.setvariable("Namespace", "Key", value)
\stopluacode
 
Result is \getvariable{Namespace}{Key}.
 
\stopTEXpage
</context>
 
 
[[Category:Programming and Databases]]
138

edits

Navigation menu