xparse 和 lualatex 的问题

xparse 和 lualatex 的问题

我有以下代码

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

\begin{luacode*}
local matrix = require "matrix"
local complex = require "complex"

function add(m,n)
  m=change(m)
  n=change(n)
  return tex.sprint(matrix.display(matrix.add(m,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=loadstring("return "..m1)()
     m1 = matrix(m1):replace(complex)
     return m1
  else
     m1=loadstring("return "..m1)()
     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*}
\NewDocumentCommand{\matrixadd}{m m}{\directlua{add('#1','#2')}}
\def\n{{{2,4,6},{8,10,12},{14,16,20}}}

\begin{document}

\[\matrixadd{\n}{\n}\]
%\[\matrixadd{\matrixadd{\n}{\n}}{\n}\]

\end{document}

这里我使用的matrix.luacomplex.lua以下链接。

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

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

这些文件要和tex文件放在同一个文件夹中,用lualatex进行编译。

此代码运行正常。但是,当我取消注释最后一行时,它会出现错误。因此,命令嵌套存在问题。它会出现错误invalid escape sequence near '\m'。因此,\matrixadd命令在单独调用时运行良好。但是,当嵌套时会出现错误。可能性很少。错误可能出在我的 lua 代码上。错误可能出在 lualatex 或 xparse 上。有人能指出这个问题吗?任何帮助都将不胜感激。谢谢。

相关内容