Difference between revisions of "CLD passing variables"

From Wiki
Jump to navigation Jump to search
m
(4 intermediate revisions by one other user not shown)
Line 11: Line 11:
 
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"})}}.
 
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}}) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.
+
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 by removing the Lua comment signs --)  
+
All this being said, it is better to give some examples below (try the code by putting and removing the Lua comment signs --)  
  
 
<context source=yes mode=mkiv>
 
<context source=yes mode=mkiv>
Line 32: Line 32:
 
-- context.MyGoTo({s},{t})
 
-- context.MyGoTo({s},{t})
 
context["goto"]("Index",{"ref:index"})
 
context["goto"]("Index",{"ref:index"})
 +
-- context["goto"](s,{t})
 
-- context.YourGoTo("Index","ref:index")
 
-- context.YourGoTo("Index","ref:index")
 +
-- context.YourGoTo(s,t)
 
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a reserved word in Lua
 
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a reserved word in Lua
 
\stopluacode
 
\stopluacode
Line 49: Line 51:
 
\stoptext
 
\stoptext
 
</context>
 
</context>
 +
 +
[[Category:Programming and Databases]]

Revision as of 13:48, 8 June 2020

CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in [ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf inside your ConTeXt directory.

Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.

First, if \mycommand is defined on the TeX end and takes one parameter, as in \mycommand{Myvariable}, then in Lua one can say context.mycommand("Myvariable").

The same applies to all the commands which take one or more parameters, provided that in their definition no specific delimiters is used. If \mycommand takes two arguments, then saying context.mycommand("Myfirstargument","Mysecondargument") in the Lua end, results in \mycommand{Myfirstargument}{Mysecondargument} on the TeX end.

Next, for commands which use specific delimiters, such as the \MyGoTo command defined below, then one has to use the construction 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 goto, end, if, present also as TeX commands) is to use the construction context["goto"]("some argument",{"ref:somewhere"}) in order to obtain \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 --)

\setuppapersize[A6][A6]
\setupinteraction[state=start]
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}
\define[2]\YourGoTo{\goto{#1}[#2]}
\starttext

\startchapter[title={Ward},reference={ch:ward}]

\startluacode
	context.index("Knuth")
--	context.index("Ward")
	context["index"]("Ward")
	context("Read Knuth and see also the ")
	local s,t = "Index","ref:index"
--	context.MyGoTo({s},{t})
	context["goto"]("Index",{"ref:index"})
--	context["goto"](s,{t})
--	context.YourGoTo("Index","ref:index")
--	context.YourGoTo(s,t)
--	context.goto("Index",{"ref:index"}) -- this does not work because goto is a reserved word in Lua
\stopluacode

\input ward.tex

\stopchapter

\starttitle[title={Index}]
\startluacode
	context.pagereference({"ref:index"})
\stopluacode
\placeindex
\stoptitle

\stoptext