# How to integrate xkeyval keys with Lua code

I am currently maintaining a template that uses directlua to compute a value based on a number of parameters defined as command keys with keyval. As it stands, it compiles correctly. However, when I try to adapt it, I obtain cryptic errors that I'm finding very hard to troubleshoot.

The template is a very complex one, what makes it difficult to create a coherent MWE, but here's my attempt:

\documentclass{report}
\usepackage{xkeyval}
\usepackage{luacode}

\define@cmdkey{X}{y}[Z]{}
\begin{document}
\setkeys*{X}{y="p"}
\directlua{
if \cmdKV@X@y == "P" then
var=1
end
}
\end{document}


I have tried some variants with the package luacode and importing the script from a different file using dofile and require, getting invariably an error that points somewhere at the end of the file. I suspect the Lua code doesn't like the @character, but I have not found any mention about that (or how to get around it).

So my question is: how can I evaluate command key-values using a Lua script?

UPDATE 10.05.18: As mentioned before, the original template is complex enough to make it difficult for me to extract a perfectly accurate MWE. This one, however, should be a bit better than the original one:

\documentclass{report}
\usepackage{xkeyval}
\usepackage{luacode}

\makeatletter
\define@cmdkey{X}{y}[Z]{}

\newcommand{\mycommand}[1]{
\setkeys*{X}{#1}

\directlua{
if \cmdKV@X@y == "P" then
var=1
else
var=2
end
token.set_macro("amacro", var)
}
\amacro
}
\makeatother

\begin{document}
\mycommand{y=P}

\end{document}


If I replace \cmdKV@X@y with a more normal command (i.e., something like \mymacro), it works and evaluates correctly. However, as it stands, it acts as if something is missing and the stack trace points elsewhere (down in the document).

• missing \makeatletter - \makeatother pairs
• wrong usage of the key management (family and key name in definition do not match their use)

The following code is a working example. Change the uppercase to a lowercase letter and you'll see a TeX page.

\documentclass{report}
\usepackage{xkeyval}
\usepackage{luacode}

\makeatletter
\define@cmdkey{ECVSS}{X}[Z]{}
\makeatother
\begin{document}
\makeatletter
\setkeys*{ECVSS}{X="P"}
\directlua{
if \cmdKV@ECVSS@X == "p" then
var=1
tex.print("1")
end
}
\makeatother
\end{document}

• Thank you. The inconsistencies of the key management were a product of hasty copy-paste. I adapted the example to be more accurate. I'll try the makeat*commands and see how does it go. – guest_user May 9 '18 at 17:57
• @guest_user Did it solve your issue? – TeXnician May 10 '18 at 6:57
• Thank you for following up, @TeXnician. Unfortunately, it didn't. Let me try to adjust the example a bit more. – guest_user May 10 '18 at 10:45
• I just wanted to share that I succeeded on solving the problem. Although the answer didn't address my problem directly (the whole template is a monster!), it is correct and answers the question. Thank you, @TeXnician – guest_user May 11 '18 at 12:56