Is there a way to pass an arrays like {\sin{x},0,0,1} to a lua function. Numbers work perfectly fine but it seems to fail when using \sin, \cos, \tan, \sum, \int,...
Commands with arguments (e.g. \sin{x}) cause 2 errors
Argument of \@gobble has an extra }.Argument of \reserved@b has an extra }.
and the passed array will be empty.
Commands without arguments (e.g. \pm) don't cause an error, but the passed array will also be empty. The same behaviour happens when there are spaces in the argument.
This is my code:
\begin{luacode}
function buildMatrix(x,y,arr)
for i=1, #arr do
tex.sprint(arr[i])
if math.fmod(i,x)==0 then
tex.sprint("\\\\")
else
tex.sprint("&")
end
end
end
\end{luacode}
\newcommand{\mat}[3]{%
\ensuremath{%
\def\args{{#3}}
\begin{pmatrix}
\directlua{
buildMatrix(#2,#1,\args)
}
\end{pmatrix}
}
}
% This is a 2D Identity matrix
\mat{2}{2}{1,0,0,1}
% This fails + 2 errors
\mat{2}{2}{\sin{x},0,0,1}
% This also fails
\mat{2}{2}{1 1,0,0,1}
\mat{2}{2}{\pm,0,0,1}
Any idea how to fix this? Also the 'readarray' package has the same issue.
Here is a test file with surrounding structure: https://gist.github.com/michihupf/1f51e8a09871ae389f04b5a168990655

\unexpandedand ``\luaescapestring` will do what you need