\documentclass{article}
\usepackage{amsmath}
\usepackage{luacode}
\begin{luacode*}
-- https://www.lua.org/pil/9.3.html
function permgen (a, permgen(a, n)
local n = n or #a
if n == 0 then
coroutine.yield(a)
else
for i=1,n do
a[n], a[i] = a[i], a[n]
permgen(a, n - 1)
a[n], a[i] = a[i], a[n]
end
end
end
function perm (a)
perm(a)
local co = coroutine.create(function() permgen(a) end)
return function()
local code, res = coroutine.resume(co)
return res
end
end
function print_permutation(a)
local start = table.concat(a,"&") .. "\\\\"
for p in perm(a) do
tex.sprint("\\begin{pmatrix}")
tex.sprint(start)
tex.sprint(table.concat(p,"&"))
tex.sprint("\\end{pmatrix}")
end
end
\end{luacode*}
\begin{document}
$\directlua{print_permutation{1,2,3}}$
\end{document}
[![alt text][1]][1]
[1]: https://texwelt.de/wissen/upfiles/test_402.png