Using \directlua
and tex.print
, it is possible to send strings of characters back and forth between TeX and Lua. Let's take an example: starting with a macro \A
, I pass its replacement text to Lua and back, defining \B
\edef\A{ab\string c}
\directlua{tex.print("\noexpand\\def\noexpand\\B{\unexpanded\expandafter{\A}}")}
\show\A
\show\B
\ifx\A\B
\message{Identical :)!}
\else
\message{Different :(.}
\fi
\bye
The two macros appear identical, but they aren't: the weird/special combination of category codes (letter and other) is lost before reaching Lua, and tokenization on the return trip is done using the category code régime in effect when \directlua
is performed.
Can the \directlua
line be replaced by some other Lua code which would be able to define \B
to be identical to \A
, including category codes? Of course,
\directlua{\unexpanded{tex.print("\\edef\\B{\\unexpanded\\expandafter{\\A}}")}}
would work (by delaying the expansion of \A
until after the passage through LuaTeX), but my goal would be to perform some complicated operations on the token lists on the Lua side.
\directlua
is executed'. (The tokens still have category codes, just not the ones you expected!) – Joseph Wright♦ Feb 21 '12 at 17:36\A
a little, hopefully that makes things clearer). Btw, the French word is régime (we also had that typo in the LaTeX3 doc at some point). – Bruno Le Floch Feb 21 '12 at 17:43