Using the following macro to redefine catcodes for lua blocks to make programming lua inside tex files more natural has a fault:
\def\lua{%
\bgroup
\catcode`\\=12
\catcode`\{=12
\catcode`\}=12
\catcode`\^^M=12
\catcode`\#=12
\catcode`\~=12
\catcode`\%=12
\catcode`\_=12
\catcode`\@=12
\doluacode
}
\bgroup
\catcode`\|=0
\catcode`\^^M=12 %
\catcode`\\=12 %
|long|gdef|doluacode#1^^M#2\endlua{|directlua{#2}|egroup}%
|egroup
Because it redefines the codes it also redefines the codes from tex.print output. The following code will fail
\lua
tex.print("\\mymacro")
\endlua
because \
is interpreted with a catcode of 12.
Is there any way to get tex.print to revert back to using the original tex catcodes so printing tex stuff will work WITHOUT having to redefine each character by hand(which obviously doesn't work when you are generating tex code programmatically in lua).
That is, I would like to simply run the above code with LITTLE modification and have it work correctly and not have to do stuff like tex.print(0, "\") tex.print("mymacro") which ,AGAIN, does not work for programmatically generated tex.
The only way to do this that I have of is to create a new print function that scans the string and converts the cat codes but hopefully there is a better way?
tex.print
? See the LuaTeX manual, page 116 (v0.70.1). – Joseph Wright♦ Jul 20 '12 at 19:46luacode
package? – Caramdir Jul 20 '12 at 21:32