# LuaLaTeX code should generate an error, yet doesn't

I'm trying to learn a bit more about LuaLaTeX and came across this introduction. The author claims that the following code does not work:

% this code will not work
\directlua{
tex.print("\\begin{tabular}{lll}")
tex.print("1 & a & Test A \\\\")
tex.print("2 & b & Test B \\\\")
tex.print("\\end{tabular}")
}


I have, however, tried this on my local machine, and compiling using latexmk -pdflatex test.tex works just fine. Furthermore, the table generated by LaTeX looks identical to the one generated via Lua. This is my code:

\documentclass{article}

\begin{document}

Table generated by Lua:
\directlua{
tex.print("\\begin{tabular}{lll}")
tex.print("1 & a & Test A \\\\")
tex.print("2 & b & Test B \\\\")
tex.print("\\end{tabular}")
}

Table generated by \LaTeX
\begin{tabular}{lll}
1 & a & Test A \\
2 & b & Test B \\
\end{tabular}
\end{document}


Am I doing something wrong? Why is my code not generating an error?

• This is because \\  is etex \protected (robust), so it is not expanded before passing to LuaTeX. You can observe the same thing with \immediate\write16{\\ <- not expanded} which shows up verbatim. Oct 13 at 5:45

The page that you reference would have been correct when written but in recent years more latex commands have been made robust and in the case of \\  that is done with e-tex \protected (not the LaTeX \DeclareRobustCommand mechanism) as such it stays as \\  in \directlua