2

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?

1
  • 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.
    – plante
    Oct 13 at 5:45
4

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

Specifically this change was made in January 2020

https://github.com/latex3/latex2e/issues/203

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.