Bug in Lualatex: not printing characters from calculation
There seems to be an issue in how tex.sprint
interprets complex numbers. As a solution, you can convert it to a string before printing:
\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
local matrix = require "matrix"
local complex = require "complex"
function cmatrix(n)
return matrix(n):replace(complex)
end
function det(m)
tex.sprint(tostring(matrix.det(cmatrix(m))))
end
\end{luacode*}
\newcommand{\matrixdet}[1]{\directlua{det(#1)}}
\matrixdet{{{1,2,3},{4,5,6},{"7+8i",8,10}}}
\end{document}
Unlike the Lua print
function, which implicitly applies tostring
to its argument, tex.sprint
is defined to print each entry of a table separately if its argument is a table. The complex number is a two item table with the real and imaginary part with a custom tostring
function that adds the i
.
So print(matrix.det(cmatrix(m)))
invokes tostring
and prints -3-24i
but tex.sprint
applies the normal numeric tostring
to each element of the table separately so prints -3
then -24
. If you explicitly apply tostring
before calling tex.sprint
then the function specified for the complex number table will be used, resulting in "-3-24i"
again.