Paasing two arguments from Lua to Context
![](https://secure.gravatar.com/avatar/5faea8b4fa0adb143383811dc94f0d22.jpg?s=120&d=mm&r=g)
Hi all, After Hans showed me an example of using Lua code in ConTeXt, I am learning this stuff in the manual cld-mkiv.pdf, which is in [ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf From that manual I understand that if a macro command \mycommand is defined so that it takes one argument, such as in \mycommand{Myargument} then in a Lua code one may use it as in context.mycommand({"Myargument"}) However, if \mycommand takes two arguments (or more), it is not clear to me how these arguments can be passed from Lua to Context. Indeed there is always the possibility to say (for a macro taking two arguments) context("\\mycommand{FirstArgument}{SecondArgument}") as in the example below with \goto. But I don’t know how to use context.goto… The reason for which I need such a construction is that I want to add some links and buttons in my document which point to locations which are determined inside the Lua code. Does any one have an idea to do this? Thanks in advance and best regards: OK %% begin test-lua-reference.tex \setupinteraction[state=start] \starttext \startchapter[title={Knuth},reference={ch:knuth}] % a simple way of adding a link to some part of the document... Read the following and also what \goto{Ward says}[ch:ward].\index{Ward} See also the \goto{Index}[ref:index]. \input knuth.tex \stopchapter \startchapter[title={Ward},reference={ch:ward}] % a complicated way of doing the same as above... \startluacode context("Read the following and also what ") context("\\goto{Knuth says}[ch:knuth]") context.index("Knuth") context(". See also the ") local s,t = "Index","ref:index" -- context("\\goto{s}[t]") -- this does not work -- context.goto({"Index"},{"ref:index"}) -- this does not work context("\\goto{Index}[ref:index]") \stopluacode \input ward.tex \stopchapter \starttitle[title={Index}] % one could say \pagereference[ref:index], but I need a construction such as this: \startluacode local s,t = "ref:","index" context.pagereference({s..t}) \stopluacode \placeindex \stoptitle \stoptext %% end test-lua-reference.tex
![](https://secure.gravatar.com/avatar/49e63acb01f2ca80efce7eed08310ce8.jpg?s=120&d=mm&r=g)
On 2/13/2018 4:54 PM, Otared Kavian wrote:
Hi all,
After Hans showed me an example of using Lua code in ConTeXt, I am learning this stuff in the manual cld-mkiv.pdf, which is in [ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf
From that manual I understand that if a macro command \mycommand is defined so that it takes one argument, such as in \mycommand{Myargument} then in a Lua code one may use it as in context.mycommand({"Myargument"})
However, if \mycommand takes two arguments (or more), it is not clear to me how these arguments can be passed from Lua to Context. Indeed there is always the possibility to say (for a macro taking two arguments)
context("\\mycommand{FirstArgument}{SecondArgument}")
as in the example below with \goto. But I don’t know how to use context.goto… The reason for which I need such a construction is that I want to add some links and buttons in my document which point to locations which are determined inside the Lua code.
Does any one have an idea to do this?
...
\startluacode context("Read the following and also what ") context("\\goto{Knuth says}[ch:knuth]") context.index("Knuth") context(". See also the ") local s,t = "Index","ref:index" -- context("\\goto{s}[t]") -- this does not work -- context.goto({"Index"},{"ref:index"}) -- this does not work context("\\goto{Index}[ref:index]") \stopluacode
context.goto( "Index", { "ref:index" }) \def\foo#1#2{....} context.foo("a","b") \def\foo[#1][#2]{....} context.foo( { "a" }, { "b" } ) etc ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
![](https://secure.gravatar.com/avatar/5faea8b4fa0adb143383811dc94f0d22.jpg?s=120&d=mm&r=g)
On 13 Feb 2018, at 17:38, Hans Hagen
wrote: […]
context.goto( "Index", { "ref:index" })
\def\foo#1#2{....}
context.foo("a","b")
\def\foo[#1][#2]{....}
context.foo( { "a" }, { "b" } )
etc
Hi Hans, Thanks for your attention, your quick response and the hints, especially regarding the definition of a command with brackets. Indeed, as you may see with the following modified example, context.goto("Index",{"ref:index"}) generates an error, maybe because of a delimiter issue with the definition of \goto. However, after defining a \MyGoTo command with delimiters being explicitely brackets, then context.MyGoTo({"Index"},{"ref:index"}) works fine, as well as local s,t = "Index","ref:index" context.MyGoTo({s},{t}) so that my problem is solved… Best regards: OK %% begin test-lua-reference.tex \setupinteraction[state=start] \def\MyGoTo[#1][#2]{\goto{#1}[#2]} \starttext \startchapter[title={Ward},reference={ch:ward}] \startluacode context.index("Knuth") 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"}) -- this does not work \stopluacode \input ward.tex \stopchapter \starttitle[title={Index}] \startluacode local s,t = "ref:","index" context.pagereference({"ref:index"}) \stopluacode \placeindex \stoptitle \stoptext %% end test-lua-reference.tex
![](https://secure.gravatar.com/avatar/49e63acb01f2ca80efce7eed08310ce8.jpg?s=120&d=mm&r=g)
On 2/13/2018 10:16 PM, Otared Kavian wrote:
On 13 Feb 2018, at 17:38, Hans Hagen
wrote: […]
context.goto( "Index", { "ref:index" })
\def\foo#1#2{....}
context.foo("a","b")
\def\foo[#1][#2]{....}
context.foo( { "a" }, { "b" } )
etc
Hi Hans,
Thanks for your attention, your quick response and the hints, especially regarding the definition of a command with brackets.
Indeed, as you may see with the following modified example, context.goto("Index",{"ref:index"}) generates an error, maybe because of a delimiter issue with the definition of \goto. However, after defining a \MyGoTo command with delimiters being explicitely brackets, then context.MyGoTo({"Index"},{"ref:index"}) works fine, as well as local s,t = "Index","ref:index" context.MyGoTo({s},{t}) so that my problem is solved…
Best regards: OK %% begin test-lua-reference.tex \setupinteraction[state=start] \def\MyGoTo[#1][#2]{\goto{#1}[#2]} \starttext
\startchapter[title={Ward},reference={ch:ward}]
\startluacode context.index("Knuth") 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"}) -- this does not work \stopluacode
you need to use context["goto"]("Index",{"ref:index"}) -- this does not work because "goto" is a reserved lua word you can't say foo.if or foo.end either
\input ward.tex
\stopchapter
\starttitle[title={Index}] \startluacode local s,t = "ref:","index" context.pagereference({"ref:index"}) \stopluacode \placeindex \stoptitle
\stoptext %% end test-lua-reference.tex ___________________________________________________________________________________ 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 ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (2)
-
Hans Hagen
-
Otared Kavian