# [luatex] lua script on the run

Philipp Stephani st_philipp at yahoo.de
Thu Jan 6 22:23:36 CET 2011

Am 06.01.2011 um 21:05 schrieb Paul Isambert:

> Le 06/01/2011 20:50, Philipp Stephani a écrit :
>> Am 06.01.2011 um 20:06 schrieb Luis Rivera:
>>
>>> "tables and functions". So how would you set a table for the six
>>> special characters of esperanto (^c, ^g, ^h, ^j, ^s, ^u)?
>> \catcode\^=12
>> \begingroup
>> \catcode\%=12
>> \gdef\percentchar{%}
>> \endgroup
>> \directlua{%
>>   local translate = {
>>     c = "ĉ",
>>     g = "ĝ",
>>     h = "ĥ",
>>     j = "ĵ",
>>     s = "ŝ",
>>     u = "û"
>>   }
>>   function texperanto(text)
>>     result = text:gsub("\percentchar^(\percentchar a)", translate)
>>     tex.sprint(result)
>>   end
>> }
>> \def\texperanto#1{\directlua{texperanto([[#1]])}}
>> \font\rm={name:DejaVu Sans:}\relax
>> \rm
>> \texperanto{^a ^b ^c ^d ^e ^f ^g ^h ^i ^j ^s ^u}
>> \bye
>>
>
> A small simplification: again, the catcode change for "^" is unnecessary,

In this example it is necessary because otherwise you get errors (but only because I used ^b and other unmodified sequences to test the functions)

> and you could as well use \direclua in the group and use "%" as is (i.e. without \percentchar), since \directlua is immune to TeX groups.

Indeed, I have forgotten that (the luacode environment does the same).

>
>
>>> I thought of a macro package independent solution. This one works for
>>> LuaLaTeX, but Patrick's should also work for plain LuaTeX.
>> of course that's possible, but a bit more awkward since you have to e.g. escape percent characters. "macro package independence" is even harder with LuaTeX than with pdfTeX, since you need lots of external packages to e.g. load fonts. Luaotfload works with LaTeX and plain, but probably not with ConTeXt, which is completely different anyway.
>
> Luaotfload should work with ConTeXt, since the code is ConTeXt's code. And format-independance should be enforced as often as possible (a personal hobby): when it comes to Lua, it's rather easy, since Lua code doesn't give a damn about formats (unless you use format-dependant Lua function, that is).

Maybe in this simple example, but in general I think it is quite hard (but I have to admit that I've never written format-independent code).

>
>>> What occurs to me is to use a babel-like solution: make ^ active in
>>> textual context, and define it in terms of Lua functions so that it
>>> may check the following character and apply a conversion on a match:
>>> otherwise, typeset the original two character string.
>> Never do this if you have LuaTeX, it is completely obsolete and causes many issues. If you are sure to run on LuaTeX, do everything in Lua, and avoid catcode changes and the like.
>
> Indeed, the solution with catcodes would be ok with anything but LuaTeX, and you actually don't need any Lua function.

But making ^ active disables ^^-escapes:

\def\normalcircumflex{^}
\edef\twonormalcircumflexes{\normalcircumflex\normalcircumflex}
\catcode\^=\active
\def^{\futurelet\next\dotexperanto}
\def\dotexperanto{%
\ifx\next g%
\let\next\dodotexperanto
\else
\ifx\next^%
\let\next\twonormalcircumflexes
\else
\let\next\normalcircumflex
\fi
\fi
\next
}
\def\dodotexperanto#1{\^#1}
g ^g h
^^J
\bye

`