Can someone help me understand the order in which LuaTeX documents are being processed?
From what I've understood, the order is the following:
- the code is being read, top to bottom
- when the compiler finds the start of some Lua code it swallows it, expands any TeX commands it can find and passes the result to a Lua interpreter
the Lua interpreter interprets the code and puts its output back into the document
is the output being parsed by again? Is it just put into the document as is?
To illustrate my question:
\newcommand{\compare}[2]{
#1
\directlua{
testNumber=#2
if #1<testNumber then
tex.sprint("is bigger")
else
tex.sprint("is smaller")
end
tex.sprint(" than "..testNumber..".")
}
}
When \compare{1}{5}
is called the Lua interpreter is being fed:
testNumber=5
if 1<testNumber then
tex.sprint("is bigger")
else
tex.sprint("is smaller")
end
tex.sprint(" than "..testNumber..".")
And the output would be pasted into the document in the place where the directlua command was called.
Now suppose I add another command:
\newcommand{\iterate}[2]{
\directlua{
for i=1,#1,1 do
tex.print(\\compare{i}{#2})
end
}
}
and call it via \iterate{5}{5}
From my limited understanding, this should be fed to the Lua interpreter as follows:
for i=1,10,1 do
tex.print(\compare{i}{5})
end
and the output would be:
\compare{1}{5}
\compare{2}{5}
\compare{3}{5}
\compare{4}{5}
\compare{5}{5}
Is this output being evaluated again? I can't seem to get my code to run, hence I was thinking that I've maybe misunderstood something.
Can this process be understood in terms of a preprocessor-processor relationship such as with, say, a PHP application which outputs JS code which itself is being interpreted by the client?