4

I'm looking to make automatised table using lualatex (I just want to make some conversion and be able to modify the input easily).

But I can't find how the code would be parsed and compile by lualatex.

The correct sequence is:

all lua section and after latex

or

lua section executed when latex parsed them (so between some latex compilation)

or something else?

  • 2
    I think you are after the technical detail of execution of \directlua (the primitive for running Lua chunks in LuaTeX). If so: the TeX run goes along as normal until it comes to \directlua. The argument to that is then expanded using normal TeX rules and passed to Lua. Lua then executes and returns whatever it does, and the TeX run continues. Does that cover it? – Joseph Wright Jul 1 '14 at 8:01
  • @JosephWright : Pretty well. So considering I use \directlua, If I have the following statement: 1- LaTeX code, 2 - Lua Code , 3 - LaTeX code, luaLaTeX will do: LaTeX compilation for 1 and 2, lua interpretation for 2 and LaTeX compilation for 3? Or after the lua interpretation LaTeX compilation is done an other time for 2? – Romain Picot Jul 1 '14 at 9:45
6

Irrespective of the 'document level' interface you use, at the level of primitives using Lua means that the \directlua primitive is being used. The logic here is easy enough to follow provided you bear in mind this is 'Lua-in-TeX'.

When LuaTeX parses a file it works in the same way as 'classical' TeX. Thus each token is read and expanded, executed or typeset as appropriate. When LuaTeX finds a \directlua primitive, it first grabs the argument in a TeX manner:

\directlua{<argument goes here>}

It then carries out expansion of the argument, much as \edef would do. The only real point to note is that \directlua is expandable (if you don't understand what this means, it's probably not going to impact on you).

Lua then receives the result of the TeX expansion and executes the code. TeX 'waits' for the Lua run to finish, then picks up with the first token that was returned by the Lua process. For example

\directlua{tex.print("Foo")}

will insert Foo into the TeX input stream. The tokens returned by Lua are tokenized using the current category codes active, so here I've inserted the letters F and o (twice) into what TeX sees.

As you can see, the two processes interweave. It's therefore possible to pick up some information from TeX, process it and return it to TeX for further manipulation. A trivial example

\documentclass{article}
\def\luauppercase#1{%
  \directlua{tex.print(string.upper("\luatexluaescapestring{#1}"))}
}
\begin{document}

\luauppercase{foo}

\end{document}

uses Lua to do expandable upper casing of input.


Note: there is a second primitive for executing Lua code, \latelua. Unless you are worrying about attaching things to the output routine, this is not relevant!

  • I've stuck to the primitive here: I hope it's OK. The detail is the same with higher-level interfaces, of course. – Joseph Wright Jul 1 '14 at 13:16
  • Primitive is fine ;-) . All information I wanna know (in particular this part: "As you can see, the two processes interweave. It's therefore possible to pick up some information from TeX, process it and return it to TeX for further manipulation." ) can be found in your answer it's a really good one. Thanks – Romain Picot Jul 1 '14 at 13:37

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.