LuaTeX and Lua – Handling Percent Signs in luacode* Environment (LuaLaTeX)

lualuatex

Can someone explain what happens here:

\documentclass{article}
\usepackage{luacode}
\begin{document}

\begin{luacode*}
tex.print("xx\%a","yy\\%b","zz\\\%c")
\end{luacode*}

\end{document}

The result is xxyy%b zz%c. (The xx,yy,zz is clear.) The \%a is "ignored", the \\%b and the \\\%c results in the "same" %b (or c). What is the preferred way to get a % in the PDF output? Why is the \%a ignored? What does TeX see after returning from the lua call?

Best Answer

I assume that luacode in LaTeX works the same way as \startluacode...\stopluacode in ConTeXt. luacode expands its contents before passing control to lua. So,

  • xx\%a => xx%a (because \% = %)
  • yy\\%b => yy\%b (because \\ = \)
  • zz\\\%c => zz\%c (because \\ = \ and \% = %)

Thus, (as PaĆ­lo Ebermann) mentioned, lua writes the following to TeX input stream:

xx%a
yy\%b
zz\%c

which explains the output that you get. Perhaps the following test file will make this clear (Compare the console output from pdf output).

\def\%{?}
\def\?{!}
\starttext
\startluacode
tex.print("xx\%a","yy\\%b","zz\\\%c")
print("xx\%a","yy\\%b","zz\\\%c")
\stopluacode
\stoptext