Another option is to use the luacode
package and its luacode*
environment, which doesn't expand as well:
\documentclass{article}
\usepackage{array,longtable}
\usepackage{luacode}
\begin{luacode*}
function mymulticol()
tex.sprint([[\multicolumn{3}{l}{1}&\bf 2]])
end
\end{luacode*}
\begin{document}
\begin{longtable}{p{8cm}rrr}
\textbf{1}&2é&3&4\\ \hline\\
\multicolumn{3}{l}{1}&\bf 2\\
\multicolumn{3}{l}{1}&\bf 2\\
\directlua{mymulticol()}
\end{longtable}
\end{document}
In general you should only have simple function calls in \directlua
, complex code is harder to maintain due to expansion issues and the mixture of Lua and TeX code. For longer pieces of Lua code, it is best to place them in a dedicated file (possibly as a LuaTeXBase module).
To explain why such issues arise, consider that \directlua
does two things: first it expands its argument like writing to a file would, second it interprets the expansion as Lua code. Both steps have to succeed and produce meaningful results. As a simple example, we try to print \TeX
(giving the TeX logo) from Lua. \directlua{tex.sprint(\TeX)}
expands the \TeX
command, leading to garbage on the Lua side. \directlua{tex.sprint(\noexpand\TeX)}
prevents the expansion so that Lua sees tex.sprint(\TeX)
, which is still invalid Lua code because Lua expects a string. The next refinement \directlua{tex.sprint("\noexpand\TeX")}
at least causes no compilation errors, but still does not give the desired result: Lua sees tex.sprint("\TeX")
and tries to interpret the backslash escape (in this case the backslash is removed because \T
is not a valid escape sequence). Only if we suppress backslash escape interpretation with \directlua{tex.sprint([[\noexpand\TeX]])}
the code works as expected. Note that in this case we had to suppress both TeX expansion and Lua's escape character to get the desired result.
\directlua{tex.print(...)}
is expanded like in an\edef
. Adding\noexpand
before all macros removes the first errorIncomplete \iffalse
but then givesLuaTeX error <\directlua >:1: unexpected symbol near '\'
. I don't know LuaTeX enough to help further. – Martin Scharrer♦ Apr 17 '11 at 19:08