1

Here is the code. It makes use of the file matrix.lua available at the following link.

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

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
local matrix = require "matrix"
function add(m,n)
return tex.sprint(matrix.latex(matrix.add(m,n)))
end
function subtract(m,n)
return tex.sprint(matrix.latex(matrix.sub(m,n)))
end
\end{luacode*}
\newcommand{\matrixadd}[2]{\directlua{add(#1,#2)}}
\newcommand{\matrixsubtract}[2]{\directlua{subtract(#1,#2)}}
\def\m{{{1,2,3},{4,5,6},{7,8,9}}}
\def\n{{{2,4,6},{8,10,12},{14,16,20}}}
\matrixadd{\m}{\n}\\
\matrixsubtract{\m}{\n}\\
\matrixsubtract{\matrixadd{\m}{\n}}{\m}
\end{document}

These functions work well when called alone. However nesting as in the last line of code does not work. I also know the reason that I am using the function matrix.latex. I think this is the only way to get print output. Is there any way around to get this done?

Note: There is some problem in the matrix.latex function of the file matrix.lua. It prints some unnecessary characters at beginning of each row and in the last entry of the last row of the matrix. I already fixed it. But for this question, it can be neglected.

2

If you change your add function to

function add(m,n)
print(matrix.latex(matrix.add(m,n)))
return tex.sprint(matrix.latex(matrix.add(m,n)))
end

you will see the problem, you are outputting the wrong string.

the terminal printing will show

$\left( \begin{array}{ccc}
        3 & 6 & 9 \\
        12 & 15 & 18 \\
        21 & 24 & 29
\end{array} \right)$

but that is not suitable for your nested calls: you should be outputting the string

     {
    {3  , 6 , 9},
    {12 ,15 , 18},
    {21 , 24 , 29}
    }

Something like this, except the matrix library latex print is adding omega for some reason

enter image description here

The basic idea is to generate the Lua input syntax {{1,2,3},{4,5,6}} at all stages except the final call when you generate a latex array.

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
local matrix = require "matrix"
function add(m,n)
tex.sprint(matrixprint(matrix.add(m,n)))
end
function subtract(m,n)
tex.sprint(matrixprint(matrix.sub(m,n)))
end
function latex(m)
tex.sprint(matrix.latex(m))
end
function matrixprint (m)
local s=""
if(type(m) == 'table') then
s = s .. "{" 
for ii,kk in ipairs(m) do
 if ii ~= 1 then
  s = s .. ","
 end
 s = s .. matrixprint(kk)
end
 s = s .. "}"
else
 s= tostring(m)
end
return s
end
\end{luacode*}

\newcommand\latexmatrix[1]{\directlua{latex(#1)}}

\newcommand{\matrixadd}[2]{\directlua{add(#1,#2)}}
\newcommand{\matrixsubtract}[2]{\directlua{subtract(#1,#2)}}
\def\m{{{1,2,3},{4,5,6},{7,8,9}}}
\def\n{{{2,4,6},{8,10,12},{14,16,20}}}
\latexmatrix{\m}
+
\latexmatrix{\n}
=
\latexmatrix{\matrixadd{\m}{\n}}

\latexmatrix{\m}
-
\latexmatrix{\n}
=
\latexmatrix{\matrixsubtract{\m}{\n}}

\latexmatrix{\matrixadd{\m}{\n}}
-
\latexmatrix{\m}
=
\latexmatrix{\matrixsubtract{\matrixadd{\m}{\n}}{\m}}

\end{document}
  • 1
    Regarding the omega: the library is obviously giving back a \n, you get an omega too with \fontencoding{OT1}\fontfamily{cmr}\selectfont \directlua{tex.sprint("\string\n abc")}. With unicode-math there is no problem. – Ulrike Fischer Jul 25 at 20:12
  • Can if check be added to add and subtract functions itself so that there is need to type \latexmatrix everywhere? – user61681 Jul 26 at 5:50
  • @user61681 you could change the syntax eg add an extra argument or a star form so the functions output the latex markup but it's clearer to separate the functions I think – David Carlisle Jul 26 at 8:13
  • @DavidCarlisle Can the code at least be modified so that it doesn't print anything with \matrixadd add \matrixsubtract. I mean it should get printed only with \latexmatrix. – user61681 Jul 26 at 16:05
  • @user61681 that conflicts with your stated requirement: you could hold all the data in Lua and not print anything to tex, but you said you wanted to save it in tex so your lua functions have to pass back something to tex and that has to be legal lua input as the nested calls are the point of the question. You have defined your input to be lua table syntax, so that means you have to output lua table syntax. So while there are lots of possible interfaces I'm not sure how I can change this. – David Carlisle Jul 26 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.