3

I'm using TeX Live 2020 (with LuaLaTeX 1.12.0) and Lua 5.3.


When I compile the following .tex file using lualatex --shell-escape the content of the resulting .pdf is a.

\documentclass{article}
\usepackage{luacode}

\begin{luacode}
T = 
{
    { "a", 5 },
    { "b", 2 }
}    
\end{luacode}

\begin{document}
\luaexec{tex.print(T[1])}
\end{document}

Further, T[1]=="a" and T[2]==5, T[3] does not output a value and does not throw an error in LuaLaTeX. T[1][1] and T[1][2] also do not do anything, but T[2][1] throws

attempt to index a number value (field '?')


Running similar code directly in Lua, namely this code:

T = 
{
    { "a", 5 },
    { "b", 2 }
}

print(T[1])

I get the expected output, namely the adress of the table:

table: 0x559417e736b0


What is happening here, and what am I doing wrong? I am very confused.

1 Answer 1

4

Looks like a 'feature' of luacode: if I either run everything into one line or use \directlua then all is well. Specifically, if you trace the TeX, the outer set of braces are being lost so Lua is seeing

T =

    { "a", 5 },
    { "b", 2 }

which gives the result you see.

That suggests a bug in luacode where a brace group is dropped, probably a delimited argument. In general, I'd avoid luacode and either use \directlua with 'safe' Lua snippets or put them in a separate file and use require(). (The Lua file could be part of the TeX source using `filecontents.)

1
  • 1
    Thank you, you're right! Should have read the documentation. It is indeed a feature of the luacode environment, but there is luacode* which makes the original code work. Sep 17, 2021 at 13:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .