[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
together:
--------------------------------------------------------------------------------
 \def\obeylualines
   {\obeylines
    \obeyspaces}

%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
    \def\\{\string\\}\def\|{\string\|}\def\-{\string\-}%
    \def\({\string\(}\def\){\string\)}\def\{{\string\{}\def\}{\string\}}%
    \def\'{\string\'}\def\"{\string\"}%
    \def\n{\string\n}\def\r{\string\r}\def\f{\string\f}\def\t{\string\t}%
    \def\a{\string\a}\def\b{\string\b}\def\v{\string\v}\def\s{\string\s}%
    \def\1{\string\1}\def\2{\string\2}\def\3{\string\3}\def\4{\string\4}\def\5{\string\5}%
    \def\6{\string\6}\def\7{\string\7}\def\8{\string\8}\def\9{\string\9}\def\0{\string\0}}

\long\def\dodostartluacode#1\stopluacode
  {\expanded{\endgroup\noexpand\directlua{#1}}}

%The above was
%\long\def\dodostartluacode#1\stopluacode
%  {\normalexpanded{\endgroup\noexpand\directlua\zerocount{#1}}}

\long\def\dostartluacode
  {\begingroup
   \obeylualines
   \obeyluatokens
   \dodostartluacode}

\def\startluacode{\dostartluacode}
% this was
%\unexpanded\def\startluacode{\dostartluacode}


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

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