I did an attempt a while ago with LuaJIT 1, and it did not seem to be that hard. A few small build tweaks would be needed, but nothing major.
The main reason why there is no LuaJIT in LuaTeX currently is that we have a few patches to Lua now that are not in LuaJIT (and are hard to add) like an extended coroutine subsystem. In lua5.2, our patches to the Lua core will become obsolete, so once we switch to that Lua version, LuaJIT becomes interesting again and I will certainly try to have LuaJIT support by default on the supported platforms.
That should be before LuaTeX 1.0, but not in next few months.
But, don't expect much of it. Even in ConTeXt, most Lua processing is iterating over C data structures using our embedded Lua support libraries. JITting the little bit of Lua code that wraps that together will not help a lot, a few percent at best is my current guess (which is also why incorporating LuaJIT is low priority)
When you write
\newcommand{\iterate}[2]{
\directlua{
for i=1,#1,1 do
tex.print(\\compare{i}{#2})
end
}
}
The Lua process sees
for i=1,#1,1 do
tex.print(<the result of \compare>)
end
What you need to do is something like that:
\newcommand\iterate[2]{%
\luaexec{
for i=1,#1 do
tex.print(string.format("\\compare{\%d}{#2}",i))
end
}
}
\iterate{3}{2}
(luaexec is from the luacode package) This prints "\compare{1}{2}
\compare{2}{2}
\compare{3}{2}
" into TeX's input string and that gets evaluated. This works because \\
generates a backslash and it is separate from the word "compare". It's a hack and I don't recommend that. Please see How to do a 'printline' in LuaTeX for more info.
This is an experiment on "reevaluating":
For example: if you write
\documentclass[a4paper]{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
tex.sprint("%hello")
\end{luacode*}
\end{document}
TeX will see %hello
and interprets it as a comment and thus no output file will be generated.
When you write this instead (see the -2 as the first argument to tex.print())
\documentclass[a4paper]{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
tex.sprint(-2,"%hello")
\end{luacode*}
\end{document}
tex sees %hello, but the % has a "safe" category code. If the first argument to tex.print is a number, it will be taken as a catcode table. What I want to show: TeX reads the result of the \directlua{}
call.
Best Answer
If you are using LaTeX (LuaLaTeX), I'd strongly advice against using
\directlua{}
for more than a simple call to execute another Lua file.\directlua{}
is in no way safe. For example:--
), your code gets Q="!%"§I. This is because in\directlua{}
everything is read on one line.%
sign, for example intex.print()
you... well, try yourself . Good luck.~
surprising results are guaranteed.tex.print()
with a macro, such as\par
, you have do crazy stuff to not let TeX see the macro before Lua sees the macro.Now what to do? Use the environment
luacode*
from the luacode package:If you can't use the luacode package (for example when you use plain TeX), use
dofile()
orrequire()
to load a package and put your code there. TeX can't find and interpret the code there, only Lua. And then you can writewithout trouble.