Passing macro parameters to lua
Hello, good people-- I've encountered what appears to be a bug in either the TeX->Lua interface or the documentation thereof (I have the latest stable ConTeXt standalone from contextgarden.net). I'm trying to create a macro that will insert different text depending on whether one of the arguments is empty or not. My initial test implementation (following the wiki section entitled "Passing arguments and buffers: ConTeXt commands that hook into Lua") looked like this: \startluacode userdata = userdata or {} function userdata.empty_or_not(str) if str == "" or str == nil then context("{\\sc Empty}") else context(str) end end \stopluacode \def\emptyOrNot#1{% \ctxlua{userdata.empty_or_not(#1)}% } \starttext \emptyOrNot{Amazing Text!} \emptyOrNot{} \stoptext This did not work. I determined that even when I passed a non-empty string, the Lua interpreter detected it as nil. It turns out the fix was simple--I had to quote the string, as follows. \def\emptyOrNot#1{% \ctxlua{userdata.empty_or_not("#1")}% } It took me a while to figure that out, though, since the wiki example does not show quotes. -- Matt Gushee
On 8/5/2013 10:53 PM, Matt Gushee wrote:
Hello, good people--
I've encountered what appears to be a bug in either the TeX->Lua interface or the documentation thereof (I have the latest stable ConTeXt standalone from contextgarden.net).
I'm trying to create a macro that will insert different text depending on whether one of the arguments is empty or not. My initial test implementation (following the wiki section entitled "Passing arguments and buffers: ConTeXt commands that hook into Lua") looked like this:
\startluacode userdata = userdata or {}
function userdata.empty_or_not(str) if str == "" or str == nil then context("{\\sc Empty}") else context(str) end end \stopluacode
\def\emptyOrNot#1{% \ctxlua{userdata.empty_or_not(#1)}% }
\starttext
\emptyOrNot{Amazing Text!}
\emptyOrNot{}
\stoptext
This did not work. I determined that even when I passed a non-empty string, the Lua interpreter detected it as nil. It turns out the fix was simple--I had to quote the string, as follows.
\def\emptyOrNot#1{% \ctxlua{userdata.empty_or_not("#1")}% }
It took me a while to figure that out, though, since the wiki example does not show quotes.
indeed you need quotes ----------------------------------------------------------------- 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 -----------------------------------------------------------------
Matt Gushee wrote:
It took me a while to figure that out, though, since the wiki example does not show quotes.
Hans Hagen wrote:
indeed you need quotes
Yep. I've updated the wiki. http://wiki.contextgarden.net/index.php?title=Programming_in_LuaTeX&action=historysubmit&diff=21966&oldid=13667 Thanks, Matt! Cheers, Sietse
participants (3)
-
Hans Hagen
-
Matt Gushee
-
Sietse Brouwer