0

The following tex document is compiled with LuaLaTeX engine.

\documentclass{article}
\usepackage{luakeytest}
\begin{document}
\myAddFunction[c=30,d=40]{10.1}{20}

\myAddFunction{1}{2} \end{document}

This output to

100.1

73

The second result is unexpected. I expect the result to be 10 as 1 + 2 + 3 + 4 = 10 and because the default key values of c and d are 3 and 4. So it appears that the command is not taking default key values and it is retaining values of c and d from the previous exectuion. Is there something wrong in the code? How could the code be corrected? Here is the code in `luakeytest.sty' file.

\ProvidesPackage{luakeytest}
\RequirePackage{xkeyval}
\RequirePackage{luacode}
\begin{luacode*}
function test(a,b,c,d)
local c = c or 3
local d = d or 4
  return a + b + c + d
end
\end{luacode*}
% ========= KEY DEFINITIONS =========
\define@key{someop}{c}{\def\mop@third{#1}}
\define@key{someop}{d}{\def\mop@fourth{#1}}
% ========= KEY DEFAULTS =========
\setkeys{someop}{c=3,d=4}%
% ========= Defining Command =========
\newcommand{\myAddFunction}[3][]{%
  \setkeys{someop}{#1}%
  \directlua{tex.sprint(test(#2,#3,\mop@third,\mop@fourth))}%
}%
\endinput
user61681
  • 1,749

1 Answers1

2

You need (for example)

\newcommand{\myAddFunction}[3][]{{%
  \setkeys{someop}{#1}%
  \directlua{tex.sprint(test(#2,#3,\mop@third,\mop@fourth))}%
}}%

so the key assignments are within a group.

David Carlisle
  • 757,742