On 28/05/20, 00:42, context@vivaldi.net wrote:
Hello,
is it possible to check whether a macro exists (or - is it a non-\undefined control sequence) with Lua? Suppose:
---- \starttext \def\MyMacro{Ahoj} \def\MyMac#1#2{Something}
\startluacode IsDefined = function(ctl_seq) print("CS " .. ctl_seq .. (tex.IsCS(ctl_seq) -- Or what to come here? "is defined" or "is unknown") .. ".") end
-- So the function should print to the console:
IsDefined("MyMacro") --> "CS MyMacro is defined." IsDefined("MyMac") --> "CS MyMac is defined." IsDefined("bf") --> "CS bf is defined." IsDefined("dummy") --> "CS dummy is unknown."
\stopluacode \stoptext ----
Is it possible?
Yes, that is what the token library is there for. \starttext \def\MyMacro{Ahoj} \def\MyMac#1#2{Something} \startluacode local str = { [true] = "defined", [false] = "undefined" } for _, macro in ipairs({ "MyMacro", "MyMac", "bf", "dummy" }) do context(macro .. " is " .. str[token.is_defined(macro)] .. "\\par") end \stopluacode \stoptext Cheers, Henri
Best regards,
Lukas ___________________________________________________________________________________ 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 ___________________________________________________________________________________