I have some limitation in my understanding of how to deal with tex and lua variable.
I would like to pass the result of a Tex command in order to do some computation on it.
Inside the lua function, the variable is \case{2}
, when I try to do and tex.sprint on it in my lualatex file, it is 4. I'm looking for a way to have 4 inside my lua function in order to make some computation on it.
This is my minimal example.
% !TeX encoding = utf-8
% !TeX program = LuaLaTeX
\documentclass{article}
\usepackage{luacode}
\usepackage{xstring}
\begin{luacode}
compute = function(argone, argtwo)
tex.sprint(argone, argtow)
print("arg1", argone, "arg2", argtwo)
return argtwo
end
\end{luacode}
\newcommand\case[1]{\IfEqCase{#1}{%
{1}{2}%
{2}{4}%
{3}{6}%
{4}{8}%
}}
\newcommand\computefn[2]{%
\directlua{
compute(#1, \luastringN{#2})
}}
% -- First compilation
%compute(#1, #2)%
% -- Résult ! Argument of \xs_IfStringCase_ii has an extra \}.
% --<inserted text>
% -- \\par
% -- Second compilation
% -- Result : arg1 2 arg2 \case {2}
% -- I want to have the resulte of \case{2} in arg2
\newcommand\displayvals[1]{%
Value = #1
Case\#1 = \case{#1}
Function = \computefn{#1}{\case{#1}}
}
\begin{document}
\case{3}
\displayvals{2}
\end{document}
\case{1}
expanded to 2, it would just naturally pass 2 to Lua.