[luatex] Changing catcodes in \directlua

Steve Hafner steve.b.hafner at gmail.com
Tue Jul 6 16:05:20 CEST 2010

On Mon, Jul 5, 2010 at 11:34 PM, Paul Isambert <zappathustra at free.fr> wrote:
> 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.)

Yes, everything works as you say. Thanks for the schooling. However, I
don't want to have to add anything  to the string that I pass to Lua.
I guess what I'm looking for is ConTeXt's \startluacode ...
\stopluacode, but in plain-TeX.  So for my second attempt I took some
code from  tex/context/base/luat-ini.mkiv and pieced the following

%The above was
% \def\obeylualines
%   {\obeylines      \let\obeyedline   \outputnewlinechar
%    \obeyspaces  \let\obeyedspace\space }

 \def\obeyluatokens % todo: make this a proper catcode table, use let's
   {\catcode`\%=12 \catcode`\#=12
    \catcode`\_=12 \catcode`\^=12
    \catcode`\&=12 \catcode`\|=12
    \catcode`\{=12 \catcode`\}=12
    \catcode`\~=12 \catcode`\$=12


%The above was
%  {\normalexpanded{\endgroup\noexpand\directlua\zerocount{#1}}}


% this was

tex.print([[$\int f(x)\,dx$]]) %thanks Manuel for pointing out [[ ]]

And running this in plain luatex seems to work as I want. But I
dropped and modified some stuff as noted, so maybe there's some
gotchas lurking in there. In particular, I as yet can't find the
definition of \zerocount, so I just left it out.

I also realize that I should be using catcode tables, and I will look
into this later today.

- Steve

More information about the luatex mailing list