# Issue with xparse and lualatex

I have the following code

\documentclass{article}
\usepackage{luacode,xparse,amsmath}

\begin{luacode*}

local matrix = require "matrix"
local complex = require "complex"

m=change(m)
n=change(n)
end

function change(m1)
if type(m1) ~="string" then return  m1 end
m1 = string.gsub(m1,"\\","},{" )
m1 = string.gsub(m1,"\begin%{matrix%}","{{" )
m1 = string.gsub(m1,"begin%{matrix%}","{" )
m1 = string.gsub(m1,"%&","," )
m1 = string.gsub(m1,"end%{matrix%}","}}" )
m1 = string.gsub(m1 , "%s+", "")
m1 = string.gsub(m1,",{}","" )
m1 = string.gsub(m1,"},{{","{{" )
m1 = string.gsub(m1,"%(","{")
m1 = string.gsub(m1,"%)","}")
m1 = string.gsub(m1,"%[","{")
m1 = string.gsub(m1,"%]","}")
m1 = string.gsub(m1,";",",")
if string.find(m1,"i") ~= nil then
m1 = matrix(m1):replace(complex)
return m1
else
return m1
end
end

function matrix.display( mtx, format, dig )
mtx = change(mtx)
local format = format or "matrix"
local dig = dig or 4
local str = "\\begin{"..format.."}"

for i = 1,#mtx do
str = str.."\t"..complex.round(complex(mtx[i][1]),dig)
for j = 2,#mtx[1] do
str = str.." & "..complex.round(complex(mtx[i][j]),dig)
end
-- close line
if i == #mtx then
str = str
else
str = str .. " \\\\"
end
end
return str .. "\\end{"..format.."} "
end

\end{luacode*}
\def\n{{{2,4,6},{8,10,12},{14,16,20}}}

\begin{document}

$\matrixadd{\n}{\n}$
%$\matrixadd{\matrixadd{\n}{\n}}{\n}$

\end{document}


Here I am using matrix.lua and complex.lua from the following links.

https://github.com/davidm/lua-matrix/blob/master/lua/matrix.lua

https://github.com/davidm/lua-matrix/blob/master/lua/complex.lua

These files are to be put in the same folder as that of tex file. The code is to be compiled with lualatex.

This code works fine. However when I uncomment the last line, it gives error. So there is trouble with nesting of commands. It gives the error invalid escape sequence near '\m'. So \matrixadd command works well when called alone. However it gives the error when nested. There are few possibilities. The error may be with my lua code. The error may be with lualatex or with xparse. Could someone point out the issue. Any help will be appreciated. Thanks.

• Your lua function add() does return tex.sprint(...). This is not useful, because tex.sprint() returns nil! Contrary to lua functions, (La)TeX macros don't have the concept of return value. A macro expands to some tokens, can gobble tokens, define macros, assign registers or TeX internal parameters, but it can't return a value as in lua. Expansion is close because it can be recursive, but expansion-only contexts present severe limitations to what can be done. You have to couple expansion with execution to do many things (increment loop counter, set flag...). – frougon Jul 27 '19 at 8:10
• All this to say that your inner \matrixadd doesn't return a matrix that the outer \matrixadd can see. It would probably be easier to chain calls like this if they were all in lua space. – frougon Jul 27 '19 at 8:11
• Off-topic: Instead of if string.find(m1,"i") ~= nil then, choose the simpler if string.find(m1,"i") then – Mico Jul 27 '19 at 8:14
• @frougon actually that's not true in luatex, \directlua is expandable so you can do arbitrary lua execution, which includes if you want, setting an arbitrary number of tex counter assignments, in a single expansion step. It's a whole new world.... :-) – David Carlisle Jul 27 '19 at 8:36
• @frougon because it was wrong. This question is (I guess) not a duplicate of that but an attempt to address the comments under my answer where I pointed out that if you want to nest then the input and output syntax have to match. There I used lua table syntax, here the OP is attempting to pass strings withlatex tablular syntax and then parse on the fly to a Lua table (which could be made to work but seems vastly inefficient.) – David Carlisle Jul 27 '19 at 8:41