I have the following code


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

function add(m,n)
  return tex.sprint(matrix.display(matrix.add(m,n)))

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=loadstring("return "..m1)()
     m1 = matrix(m1):replace(complex)
     return m1
     m1=loadstring("return "..m1)()
     return m1

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)
      -- close line
      if i == #mtx then
         str = str
         str = str .. " \\\\"
  return str .. "\\end{"..format.."} "
\NewDocumentCommand{\matrixadd}{m m}{\directlua{add('#1','#2')}}




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



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, 2019 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, 2019 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, 2019 at 8:14
  • 2
    @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.... :-) Jul 27, 2019 at 8:36
  • 2
    @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.) Jul 27, 2019 at 8:41


You must log in to answer this question.

Browse other questions tagged .