如何在 lualatex 中传递值数组?

如何在 lualatex 中传递值数组?

这是矩阵乘法的代码。

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function MatMul( m1, m2 )
    if #m1[1] ~= #m2 then       
        return nil
    end

    local res = {}

    for i = 1, #m1 do
        res[i] = {}
        for j = 1, #m2[1] do
            res[i][j] = 0
            for k = 1, #m2 do
                res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
            end
        end
    end

    for i = 1, #res do
        for j = 1, #res[1] do
            tex.sprint( res[i][j] )
            tex.sprint("  ")
        end
        tex.sprint("\n")
    end
end

\newcommand\matrixmult[2]{\directlua{MatMul(#1,#2)}}

\begin{document}

\matrixmult{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}

\end{document}

我认为这会引发错误,因为传递的参数不正确。参数在 中以数组或表格形式出现lua。我在 中测试了代码lua,它运行良好。那么是否可以在 中传递值数组LuaLaTeX

答案1

您缺少 \end{luacode}。并且\n不是 tex 命令。例如,\\par它可以正常工作:

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function MatMul( m1, m2 )
    if #m1[1] ~= #m2 then
        return nil
    end

    local res = {}

    for i = 1, #m1 do
        res[i] = {}
        for j = 1, #m2[1] do
            res[i][j] = 0
            for k = 1, #m2 do
                res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
            end
        end
    end

    for i = 1, #res do
    for j = 1, #res[1] do
        tex.sprint( res[i][j] )
        tex.sprint("  ")
    end
    tex.sprint("\\par")
end
end
\end{luacode}
\newcommand\matrixmult[2]{\directlua{MatMul(#1,#2)}}

\begin{document}
\matrixmult{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}
\end{document}

在此处输入图片描述

答案2

如果您希望能够生成具有给定环境(例如matrix,等)的矩阵,则快速解决方案pmatrix是:bmatrix

\documentclass{article}
\usepackage{luacode}
\usepackage{amsmath}
\begin{luacode}
function MatMul( m1, m2, matrixtype)
  if #m1[1] ~= #m2 then
    return nil
  end

  local res = {}

  for i = 1, #m1 do
    res[i] = {}
    for j = 1, #m2[1] do
      res[i][j] = 0
      for k = 1, #m2 do
        res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
      end
    end
  end

  tex.sprint("\\begin{" .. matrixtype .. "}")
    for i = 1, #res do
      for j = 1, #res[1] do
        tex.sprint( res[i][j] )
        if j < #res[1] then
          tex.sprint(" & ")
        end
      end
      tex.sprint("\\\\")
    end
    tex.sprint("\\end{" .. matrixtype .. "}")
  end
\end{luacode}
\newcommand\matrixmult[3][matrix]{\directlua{MatMul(#2,#3,"#1")}} % Input the default matrix-environment here ("matrix")

\begin{document}

Display math, with default \verb|matrix|-environment:
\begin{equation}
\matrixmult{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}
\end{equation}

Matrix with parentheses:
$\matrixmult[pmatrix]{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}$

Matrix with braces:
$\matrixmult[Bmatrix]{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}$

Matrix with brackets:
$\matrixmult[bmatrix]{{ { 1, 2, 4 }, { 4, 5, 6 } }}{{ { 1, 2 }, { 3, 4 }, { 5, 6 } }}$

\end{document}

输出

相关内容