In an external file, I've put these 3 lines in a .lua file:

function texTest(name)
    x = tex.print("Hello " .. name)

and when I call it from .tex file, it gets interpreted properly. Out of curiosity, I set a breakpoint at line 2 and called it from within the .lua script to inspect what is in it BUT as soon as it hits the breakpoint, it throws the following exception:

Exception has occurred: test.lua:5: attempt to index a nil value (global 'tex')

I'm using VS Code with lua and lua debug extensions. Is there any way to see what is in it?

  • 3
    tex.print is part of Lua in LuaTeX but not vanilla Lua ... – Joseph Wright Jun 1 at 15:05
  • @JosephWright, so there is no way to dig into this variable? – Emon Haque Jun 1 at 15:06
  • 5
    the tex module is a lua table so you can iterate over it and find it has entries such as print, but the function itself is c code and not viewable from Lua – David Carlisle Jun 1 at 15:26
  • @DavidCarlisle, is tex the only module we can use in luatex or there are more? – Emon Haque Jun 1 at 16:38
  • 2
    @EmonHaque All the normal Lua tables also exists in LuaTeX, plus all the tables documented in the LuaTeX reference. Beside tex the most important ones are lua, token, node, pdf, kpse and font, but read the documentation for an (almost) full list. – Marcel Krüger Jun 1 at 17:47

As Joseph Wright wrote in his comment, tex.print is a LuaTeX function which is not part of regular Lua, so you can not see it outside of LuaTeX. So let's ask LuaTeX what it is: Lua comes with the debug library, which contains debug.getinfo(). This can be used to get the source of any Lua function f via debug.getinfo(f).source. Wrapping this in a (plain) LuaTeX wrapper gives:

      texio.write_nl('The source of tex.print is ' .. debug.getinfo(tex.print).source)

If we save this e.g. as luadebug.tex and execute it on the command line via luatex luadebug.tex we get:

This is LuaTeX, Version 1.10.0 (TeX Live 2019) 
 restricted system commands enabled.
The source of tex.print is =[C])
warning  (pdf backend): no pages of output.
Transcript written on luadebug.log.

So the source is ... =[C]

What does that mean? It means that tex.print is not implemented through Lua, it is a C function which can be called from Lua. So to find the implementation, you have to look at the C source of LuaTeX, specifically at ltexlib.c.

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.