[luatex] Changing catcodes in \directlua

Paul Isambert zappathustra at free.fr
Tue Jul 6 08:34:15 CEST 2010


Things are expanded in a \directlua statement, like in a \write or 
\special. And what is not exanded, e.g. primitives like \begingroup, is 
interpreted as Lua code, where quite obviously a string like 
"\begingroup" doesn't make sense. The simplest solution to achieve what 
you want is:

\directlua{tex.print("\luaescapestring{$\noexpand\alpha$}")}

which turns into correct Lua code: first because \alpha isn't expanded, 
second because \luaescapestring modifies its argument so that you get 
what you want. Forgetting it leads to Lua reading

tex.print("$\alpha$")

which you might find sensible but to Lua it means ``print the dollar 
character, then a bell, then the `lpha'$ string.'' I say ``a bell'' 
because \a is an escape sequence that refers to this character, which 
let's admit isn't the most often used nowadays. On the other hand the 
sequence with \luaescapestring returns this to Lua:

tex.print("$\\alpha$")

which means ``print a dollar, a backslash `alpha', and a dollar'' and 
that's correctly interpreted by TeX. (Note that I'm using LuaTeX v.0.6, 
hopefully it'll change nothing.)

Now a note for our much admired LuaTeX developpers: experimenting with 
the engine to answer this post I found that

\directlua{tex.print("a\noexpand\alpha b")}

returns ``aXlpha b'' where ``X'' is whatever TeX renders the bell \a 
with (an uppercase upsilon for me). But what surprises me is the space 
before the ``b''. Where does it come from? I'd think ``\alpha'' when it 
was still a command would have gobbled it...?

Best,
Paul





Steve Hafner a écrit :
> I'm using luatex beta-0.40.6 in texlive2009.
>
> If file.tex contains the the following two lines
>
> \directlua{\begingroup\catcode`\\=12 tex.print("$\alpha$")\endgroup}
> \bye
>
> then
>
> luatex file.tex
>
> gives
>
> ! Argument of \\ has an extra }.
>
> But I expected that the argument of \directlua would expand to
>
> tex.print("$\alpha$")
>
> and that $\alpha$ would be sent to the tex stream.
>
> So how do I change catcodes in \directlua?
>   



More information about the luatex mailing list