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
Each environment/macro is different. Each one redefines special characters differently.
For example, in your code example
\luaexec{
num=6
for i=1,num do
for j=1,num do
ixj='$'..i..'\\times'..j..'='..i*j..'$';
tex.print(ixj)
if(j<num) then tex.sprint('&') else tex.sprint('\\\\') end
end
end
}
you use \\
. In latex \
is a special character. Latex processes the lua code block FIRST before lua gets chance to look at it. What this means is that if you have something like
\directlua{print('\n')}
Latex will parse that and see \
and try to do something with it. If \n
is some latex macro it will attempt to expand it. One way to prevent latex from messing up lua code is to tell it not to expand it, such as
\directlua{print('\noexpand\n')}
In which case latex will see the \noexpand
it not expand what is after it.
NOW, if we change the catcodes for \
and other special symbols we can stop latex from messing with our lua code. Sometimes this is good and sometimes this is bad.
For example
\newcommand{\mylatexmacro}{my text}
\directlua{print('\mylatexmacro')}
will print my text
since latex will expand \mylatexmacro
. BUT if we use
\newcommand{\mylatexmacro}{my text}
\luaexec{print('\mylatexmacro')}
will print ?latexmacro
where ? is whatever \m
is in lua.
I tend to keep all my lua code separate from the latex code because there are generally just too many issues involved when you start getting in to it. For very simple stuff I'll use use inline lua code but code all my functions in a separate lua file that is included into the latex file.
Essentially the issue is that lualatex does not preprocess the tex file but simply provides a lua interpreter hooked into latex. lua code in latex is really latex code that is passed to the lua interpreter. When latex parses the code block it can mess up the code where lua won't understand what it is. e.g.,
\directlua{print('\\')}
does not print a \
because latex sees the \\
and tries to make a new line there then that new code is passed to the lua parser, which ends up not making any sense as lua code.
Best Answer
As you've discovered, TeX macros are expanded in a
luacode
environment but not in aluacode*
environment.If you need to use a
luacode*
environment, you must check if\HCode
is defined outside theluacode*
environment, e.g., when calling the Lua function via\directlua
.