[luatex] Inline Lua scripts

Reinhard Kotucha reinhard.kotucha at web.de
Fri Sep 20 00:11:27 CEST 2013

On 2013-09-18 at 16:03:21 +0200, Dirk Laurie wrote:

 > AFAICS, LuaLaTeX offers no
 > \begin{lua}
 >   Lua code to be read verbatim
 > \end{lua}
 > There is \directlua, which however allows TeX macros inside the code,
 > reorganizes whitespace and in general requires one to keep a cool
 > TeX head inside the Lua code.
 > So the workaround seems to be
 > \directlua{dofile"filename"}
 > which requires separate named files for such scripts.
 > Am I missing something?

I must admit that it's not clear to me what exactly you want to
achieve.  I often use separate files for Lua code because I get proper
syntax highlighting and indentation in Emacs then.

However, if you want to have the Lua code inline, use the luacode
package.  The luacode* environment allows you to write the code
conveniently without worrying about characters which have a special
meaning in TeX.

If you want to pass information from TeX to Lua, put only function
definitions into the luacode* environment and call these functions
using \directlua or \luadirect.  Since the latter expand TeX macros
you can pass any information from TeX to the Lua functions this way.

Using \filecontents probably allows you to put the expansion of TeX
macros into Lua function bodies instead of passing them as arguments.
But I suppose that you need the information from TeX when the Lua
functions are executed anyway.

Look at the file


It's kept extremely simple deliberately and doesn't pass information
from TeX to Lua.  But if you change
  -function mytable ()
  +function mytable (foo)

in the luacode* environment and


you can pass the TeX control sequence \foo as an argument to the Lua
function mytable().

Not tested, though.  But I'm convinced that external files are rarely
needed, if at all.  I also doubt that they make life easier.


Reinhard Kotucha                                      Phone: +49-511-3373112
Marschnerstr. 25
D-30167 Hannover                              mailto:reinhard.kotucha at web.de
Microsoft isn't the answer. Microsoft is the question, and the answer is NO.

More information about the luatex mailing list