Using lua to define a macro with one optional parameter in brackets
Hello ConTeXist. Within the one very complex problem I need to solve the following case. I want using lua code to define a macro with one optional parameter in brackets. I tried it using commands context.setgvalue, or find something on the wiki (http://wiki.contextgarden.net/System_Macros/Definitions_and_Assignments), but each time I broke a tooth on it. Can you, please, someone advise how something can be it done? Suffice some reference to solve a similar problem. Thanks Jaroslav Hajtmar Here is my minimal example: \starttext \def\lastname{\dosingleempty\dolastname} \def\dolastname[#1]{\iffirstargument #1\else nothing\fi} \lastname \lastname[Smith] % Not working % \ctxlua{ % context([[\def\firstname{\dosingleempty\dofirstname}]]) % context([[\def\dofirstname[#1]{\iffirstargument #1\else {nothing}\fi}]]) % } % % \firstname % % \firstname['Peter'] \stoptext
On 08 Jul 2015, at 11:05, Jaroslav Hajtmar
wrote: Hello ConTeXist.
Within the one very complex problem I need to solve the following case. I want using lua code to define a macro with one optional parameter in brackets.
Simplest solution I know is this: \startluacode interfaces.definecommand ('dolastname', { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context(opt_1) else context('nothing') end end }) interfaces.definecommand ('lastname', { macro = function () context.dosingleempty() context.dolastname() end }) \stopluacode Best wishes, Taco
Thanks Taco, your solution helped me ... Based on initial tests, it seems that it works correctly. I'll know more after thorough testing. Once again, thank you many times. Sincerely Jaroslav Hajtmar Dne 8. 7. 2015 v 12:07 Taco Hoekwater napsal(a):
On 08 Jul 2015, at 11:05, Jaroslav Hajtmar
wrote: Hello ConTeXist.
Within the one very complex problem I need to solve the following case. I want using lua code to define a macro with one optional parameter in brackets. Simplest solution I know is this:
\startluacode interfaces.definecommand ('dolastname', { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context(opt_1) else context('nothing') end end }) interfaces.definecommand ('lastname', { macro = function () context.dosingleempty() context.dolastname() end }) \stopluacode
Best wishes, Taco
Hi Taco. full deployment is unfortunately not working. Unfortunately I need to cycle create different macros whose names are variable. At this moment I can not deal with this: --- context.doxlsname() --- ????? Thanx Jaroslav Hajtmar here is another minimal example: \starttext \def\linepointer{5} \def\csvcell[#1,#2]{cell #1,#2} \directlua{ xlsname='A' interfaces.definecommand ('do'..xlsname, { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context('\\csvcell["'..xlsname..'",'..opt_1..']') else context('\\csvcell["'..xlsname..'",\\linepointer]') end end }) interfaces.definecommand (xlsname, { macro = function () context.dosingleempty() --- context.doxlsname() --- ????? end }) } \csvcell['Firstname',\linepointer] \A \A[5] \stoptext Dne 8. 7. 2015 v 12:07 Taco Hoekwater napsal(a):
\startluacode interfaces.definecommand ('dolastname', { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context(opt_1) else context('nothing') end end }) interfaces.definecommand ('lastname', { macro = function () context.dosingleempty() context.dolastname() end }) \stopluacode
On 08 Jul 2015, at 13:04, Jaroslav Hajtmar
wrote: Hi Taco. full deployment is unfortunately not working.
Unfortunately I need to cycle create different macros whose names are variable. At this moment I can not deal with this:
--- context.doxlsname() --- ?????
context[‘do’..xlsname]()
Thanx Jaroslav Hajtmar
here is another minimal example:
\starttext
\def\linepointer{5}
\def\csvcell[#1,#2]{cell #1,#2}
\directlua{ xlsname='A' interfaces.definecommand ('do'..xlsname, { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context('\\csvcell["'..xlsname..'",'..opt_1..']') else context('\\csvcell["'..xlsname..'",\\linepointer]') end end }) interfaces.definecommand (xlsname, { macro = function () context.dosingleempty() --- context.doxlsname() --- ????? end }) }
\csvcell['Firstname',\linepointer]
\A
\A[5]
\stoptext
Dne 8. 7. 2015 v 12:07 Taco Hoekwater napsal(a):
\startluacode interfaces.definecommand ('dolastname', { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context(opt_1) else context('nothing') end end }) interfaces.definecommand ('lastname', { macro = function () context.dosingleempty() context.dolastname() end }) \stopluacode
Thanx Taco for reply. the minimal example see bellow not crashed, but \A macro is not created. I don't Know in what could still be a problem. Example I am even more minimalize Thanx Jaroslav \starttext \directlua{ xlsname='A' interfaces.definecommand ('do'..xlsname, { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context(opt_1) else context('nothing') end end }) interfaces.definecommand (xlsname, { macro = function () context.dosingleempty() context['do'..xlsname]() end }) } % \A[5] % % % \A \stoptext Dne 8. 7. 2015 v 13:28 Taco Hoekwater napsal(a):
On 08 Jul 2015, at 13:04, Jaroslav Hajtmar
wrote: Hi Taco. full deployment is unfortunately not working.
Unfortunately I need to cycle create different macros whose names are variable. At this moment I can not deal with this:
--- context.doxlsname() --- ????? context[‘do’..xlsname]()
Thanx Jaroslav Hajtmar
here is another minimal example:
\starttext
\def\linepointer{5}
\def\csvcell[#1,#2]{cell #1,#2}
\directlua{ xlsname='A' interfaces.definecommand ('do'..xlsname, { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context('\\csvcell["'..xlsname..'",'..opt_1..']') else context('\\csvcell["'..xlsname..'",\\linepointer]') end end }) interfaces.definecommand (xlsname, { macro = function () context.dosingleempty() --- context.doxlsname() --- ????? end }) }
\csvcell['Firstname',\linepointer]
\A
\A[5]
\stoptext
Dne 8. 7. 2015 v 12:07 Taco Hoekwater napsal(a):
\startluacode interfaces.definecommand ('dolastname', { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context(opt_1) else context('nothing') end end }) interfaces.definecommand ('lastname', { macro = function () context.dosingleempty() context.dolastname() end }) \stopluacode
On 08 Jul 2015, at 13:58, Jaroslav Hajtmar
wrote: Thanx Taco for reply.
the minimal example see bellow not crashed, but \A macro is not created. I don't Know in what could still be a problem. Example I am even more minimalize
It works for me after changing \directlua {} to \startluacode … \stopluacode Best wishes, Taco
Thanks Taco very much. At this point, it really works in simple situations, but in the event that this solution will incorporate into the existing system, then I have big problems with expansion (values are not updated in macros). I need more solutions using something like context.setgvalue, or at least according to the following example. If they could incorporate the remaining two definitions ie \def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1} into luacode (probably encounter a problem with #), so it might work. Can not think of anyone, how it could be done differently? Thanks Jaroslav Hajtmar Here is minimal example: \ctxlua{ xlsname='A' content='content when parameter is missing' context([[\definecomplexorsimple\]]..xlsname) %context([[\def\simple]]..xlsname..[[{]]..content..[[}]]) %context([[\def\complex]]..xlsname..'[#1]'..[[{Neco: #1}]]) } \def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1} \type{\A}: \A \type{\A[5]}: \A[5] \stoptext Dne 8. 7. 2015 v 14:11 Taco Hoekwater napsal(a):
On 08 Jul 2015, at 13:58, Jaroslav Hajtmar
wrote: Thanx Taco for reply.
the minimal example see bellow not crashed, but \A macro is not created. I don't Know in what could still be a problem. Example I am even more minimalize It works for me after changing \directlua {} to \startluacode … \stopluacode
Best wishes, Taco
On 7/8/2015 4:11 PM, Jaroslav Hajtmar wrote:
Thanks Taco very much.
At this point, it really works in simple situations, but in the event that this solution will incorporate into the existing system, then I have big problems with expansion (values are not updated in macros). I need more solutions using something like context.setgvalue, or at least according to the following example. If they could incorporate the remaining two definitions ie \def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
into luacode (probably encounter a problem with #), so it might work. Can not think of anyone, how it could be done differently? Thanks Jaroslav Hajtmar
Here is minimal example:
\ctxlua{ xlsname='A' content='content when parameter is missing' context([[\definecomplexorsimple\]]..xlsname) %context([[\def\simple]]..xlsname..[[{]]..content..[[}]]) %context([[\def\complex]]..xlsname..'[#1]'..[[{Neco: #1}]]) }
\def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
why don't you define these commands at the tex level Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Hi Hans. Only for explanation of my (maybe) crazy request: I can not at this level TeX macros to create, because it creates automatically after opening the CSV file. Although I tried to start creating something with \ csname \ endcsname etc. but my experiments with this method were never successful. In addition with TeX probably I can not work as well as with Lua (for things which in this case I need ie. at low level). Given that I have a CSV file loaded in global variables and header information at my disposal, I Lua everything you need me to do what I need. The problem is that the macros created in Lua I sometimes do not work as well when I create TeX (during the cycle will automatically fall short values). I have a particularly bad experience in this regard with the construction, he sent Taco. Simplified version of "interfaces.definecommand" construction does not in my way of processing the results of what I expected. Module which at the moment I work (in collaboration with Pablo Rodriguez) at the moment is very complex and extensive, and already it slowly begin to lose myself :-), so I try to get any information from the conference. Thank you and also to other counselors too at the conference for their cooperation in solve my problems. Jaroslav Hajtmar Dne 8. 7. 2015 v 20:54 Hans Hagen napsal(a):
On 7/8/2015 4:11 PM, Jaroslav Hajtmar wrote:
Thanks Taco very much.
At this point, it really works in simple situations, but in the event that this solution will incorporate into the existing system, then I have big problems with expansion (values are not updated in macros). I need more solutions using something like context.setgvalue, or at least according to the following example. If they could incorporate the remaining two definitions ie \def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
into luacode (probably encounter a problem with #), so it might work. Can not think of anyone, how it could be done differently? Thanks Jaroslav Hajtmar
Here is minimal example:
\ctxlua{ xlsname='A' content='content when parameter is missing' context([[\definecomplexorsimple\]]..xlsname) %context([[\def\simple]]..xlsname..[[{]]..content..[[}]]) %context([[\def\complex]]..xlsname..'[#1]'..[[{Neco: #1}]]) }
\def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
why don't you define these commands at the tex level
Hans
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________
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://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
Hi,
On 09 Jul 2015, at 07:22, Jaroslav Hajtmar
wrote: The problem is that the macros created in Lua I sometimes do not work as well when I create TeX (during the cycle will automatically fall short values). I have a particularly bad experience in this regard with the construction, he sent Taco.
Simplified version of "interfaces.definecommand" construction does not in my way of processing the results of what I expected.
Perhaps interfaces.definecommand could be extended with a way to do global definitions? Best wishes, Taco
On 7/9/2015 7:22 AM, Jaroslav Hajtmar wrote:
Hi Hans. Only for explanation of my (maybe) crazy request: I can not at this level TeX macros to create, because it creates automatically after opening the CSV file. Although I tried to start creating something with \ csname \ endcsname etc. but my experiments with this method were never successful. In addition with TeX probably I can not work as well as with Lua (for things which in this case I need ie. at low level). Given that I have a CSV file loaded in global variables and header information at my disposal, I Lua everything you need me to do what I need. The problem is that the macros created in Lua I sometimes do not work as well when I create TeX (during the cycle will automatically fall short values). I have a particularly bad experience in this regard with the construction, he sent Taco.
Simplified version of "interfaces.definecommand" construction does not in my way of processing the results of what I expected.
interfaces.setmacro("name","value")
Module which at the moment I work (in collaboration with Pablo Rodriguez) at the moment is very complex and extensive, and already it slowly begin to lose myself :-), so I try to get any information from the conference. Thank you and also to other counselors too at the conference for their cooperation in solve my problems.
Jaroslav Hajtmar
Dne 8. 7. 2015 v 20:54 Hans Hagen napsal(a):
On 7/8/2015 4:11 PM, Jaroslav Hajtmar wrote:
Thanks Taco very much.
At this point, it really works in simple situations, but in the event that this solution will incorporate into the existing system, then I have big problems with expansion (values are not updated in macros). I need more solutions using something like context.setgvalue, or at least according to the following example. If they could incorporate the remaining two definitions ie \def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
into luacode (probably encounter a problem with #), so it might work. Can not think of anyone, how it could be done differently? Thanks Jaroslav Hajtmar
Here is minimal example:
\ctxlua{ xlsname='A' content='content when parameter is missing' context([[\definecomplexorsimple\]]..xlsname) %context([[\def\simple]]..xlsname..[[{]]..content..[[}]]) %context([[\def\complex]]..xlsname..'[#1]'..[[{Neco: #1}]]) }
\def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
why don't you define these commands at the tex level
Hans
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________
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://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________
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://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Thanx to Hans and Taco too, ad Taco) ala Perhaps interfaces.definecommand could be extended with a way to do global definitions? Indeed it seems to me. Changes in lua code does not affect the results in ConTeXt. ad Hans) interfaces.setmacro("name","value") Thanx, II tried to use it, but somehow I can not realize what I wanted. Not available somewhere a minimal example? I tried to scan codes in the folder with the installation of ConTeXt, but I never encountered a working example of where you could bounce. Thanks Jaroslav Hajtmar Dne 9. 7. 2015 v 11:07 Hans Hagen napsal(a):
On 7/9/2015 7:22 AM, Jaroslav Hajtmar wrote:
Hi Hans. Only for explanation of my (maybe) crazy request: I can not at this level TeX macros to create, because it creates automatically after opening the CSV file. Although I tried to start creating something with \ csname \ endcsname etc. but my experiments with this method were never successful. In addition with TeX probably I can not work as well as with Lua (for things which in this case I need ie. at low level). Given that I have a CSV file loaded in global variables and header information at my disposal, I Lua everything you need me to do what I need. The problem is that the macros created in Lua I sometimes do not work as well when I create TeX (during the cycle will automatically fall short values). I have a particularly bad experience in this regard with the construction, he sent Taco.
Simplified version of "interfaces.definecommand" construction does not in my way of processing the results of what I expected.
interfaces.setmacro("name","value")
Module which at the moment I work (in collaboration with Pablo Rodriguez) at the moment is very complex and extensive, and already it slowly begin to lose myself :-), so I try to get any information from the conference. Thank you and also to other counselors too at the conference for their cooperation in solve my problems.
Jaroslav Hajtmar
Dne 8. 7. 2015 v 20:54 Hans Hagen napsal(a):
On 7/8/2015 4:11 PM, Jaroslav Hajtmar wrote:
Thanks Taco very much.
At this point, it really works in simple situations, but in the event that this solution will incorporate into the existing system, then I have big problems with expansion (values are not updated in macros). I need more solutions using something like context.setgvalue, or at least according to the following example. If they could incorporate the remaining two definitions ie \def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
into luacode (probably encounter a problem with #), so it might work. Can not think of anyone, how it could be done differently? Thanks Jaroslav Hajtmar
Here is minimal example:
\ctxlua{ xlsname='A' content='content when parameter is missing' context([[\definecomplexorsimple\]]..xlsname) %context([[\def\simple]]..xlsname..[[{]]..content..[[}]]) %context([[\def\complex]]..xlsname..'[#1]'..[[{Neco: #1}]]) }
\def\simpleA {content when parameter is missing} \def\complexA[#1]{parameter #1}
why don't you define these commands at the tex level
Hans
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________
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://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________
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://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
On 7/9/2015 5:05 PM, Jaroslav Hajtmar wrote:
Thanx to Hans and Taco too,
ad Taco) ala Perhaps interfaces.definecommand could be extended with a way to do global definitions? Indeed it seems to me. Changes in lua code does not affect the results in ConTeXt.
ad Hans) interfaces.setmacro("name","value")
Thanx, II tried to use it, but somehow I can not realize what I wanted. Not available somewhere a minimal example? I tried to scan codes in the folder with the installation of ConTeXt, but I never encountered a working example of where you could bounce. Thanks Jaroslav Hajtmar
\starttext \startluacode interfaces.setmacro("foo","bar") \stopluacode \foo \start \startluacode interfaces.setmacro("foo","foo","global") \stopluacode \stop \foo \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Thanks Hans. At this moment, it works fine. For me it is quite essential novelty and it is very much handy for me. It seems that works only in the standalone version because version in TeXlive 2014 version it tells an error message (but does not matter on it). I want to even ask how you can write a macro with one or more parameters? Now the parameter in square brackets displayed together with the contents of the macro. You can do this at all? It would get any minimal example? Thanks again for your support. Jaroslav Hajtmar Dne 9. 7. 2015 v 17:24 Hans Hagen napsal(a):
On 7/9/2015 5:05 PM, Jaroslav Hajtmar wrote:
Thanx to Hans and Taco too,
ad Taco) ala Perhaps interfaces.definecommand could be extended with a way to do global definitions? Indeed it seems to me. Changes in lua code does not affect the results in ConTeXt.
ad Hans) interfaces.setmacro("name","value")
Thanx, II tried to use it, but somehow I can not realize what I wanted. Not available somewhere a minimal example? I tried to scan codes in the folder with the installation of ConTeXt, but I never encountered a working example of where you could bounce. Thanks Jaroslav Hajtmar
\starttext
\startluacode interfaces.setmacro("foo","bar") \stopluacode
\foo
\start \startluacode interfaces.setmacro("foo","foo","global") \stopluacode \stop
\foo
\stoptext
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________
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://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
participants (3)
-
Hans Hagen
-
Jaroslav Hajtmar
-
Taco Hoekwater