[luatex] lua script on the run

Luis Rivera jlrn77 at gmail.com
Thu Jan 6 20:06:50 CET 2011


(I am not very familiar with this mailing list's interface, so this
message may be a little cluttered. My apologies.)

On 6 January 2011 05:00,  <luatex-request at tug.org> wrote:
>
> Message: 3
> Date: Wed, 05 Jan 2011 22:28:35 +0100
> From: Paul Isambert <zappathustra at free.fr>
> To: "General discussion of LuaTeX." <luatex at tug.org>
> Subject: Re: [luatex] lua script on the run
>
> Le 05/01/2011 22:19, Patrick Gundlach a ?crit :
>>
>>> but I was thinking about defining a command like, say,
>>> \texperanto, which performs a conversion of its argument on the fly,
>>> so that I may write
>>>
>>> \texperanto{Alportu ^caron de sopiro!}
>>>
>>> And LuaTeX actually typesets "Alportu ?aron de sopiro!"
>> ------------------------------------------------
>> \def\texperanto#1{\directlua{
>>    local str = string.gsub("#1","\%^c","?")
>>    tex.print(str)
>> }}
>>
>> \catcode`\^=11
>>
>> \texperanto{Alportu ^caron de sopiro!}
>>
>> \bye
>
> As indicated by Patrick's code (where the catcode change is
> unnecessary), your task is no big deal: string.gsub can indeed handle
> it, and you want to read the Lua manual on string manipulation
> (http://www.lua.org/manual/5.1/manual.html#5.4). Two things are worth
> noting: string.gsub can also take (as its third arguments) tables and
> functions, which makes it much more powerful; and magic characters
> should be taken care of ("^" is one of them).
>

"tables and functions". So how would you set a table for the six
special characters of esperanto (^c, ^g, ^h, ^j, ^s, ^u)?

>
> Message: 6
> Date: Thu, 06 Jan 2011 11:41:44 +0100
> From: Arno Trautmann <Arno.Trautmann at gmx.de>
> To: "General discussion of LuaTeX." <luatex at tug.org>
> Subject: Re: [luatex] lua script on the run
>
> Philipp Stephani wrote:
>>
>> [?]
>>> \texperanto{Alportu ^caron de sopiro!}
>>>
>>> And LuaTeX actually typesets "Alportu ?aron de sopiro!"
>>
>> If you only need simple textual replacements, you can get quite far with Lua's built-in functions:
>>
>> \documentclass{minimal}
>> \usepackage{fontspec}
>> \usepackage{luacode}
>> \begin{luacode*}
>>  function texperanto(text)
>>    tex.sprint((text:gsub("%^c", "?")))
>>  end
>> \end{luacode*}
>> \newcommand*{\texperanto}[1]{\luadirect{texperanto([[#1]])}}
>> \setmainfont{DejaVu Sans}
>> \begin{document}
>> \texperanto{Alportu ^caron de sopiro!}
>> \end{document}
>

I thought of a macro package independent solution. This one works for
LuaLaTeX, but Patrick's should also work for plain LuaTeX.

> These solutions (Patrick's, Paul's and yours) are nice, but how would
> one apply it to a whole document? I have a very similar need and it
> would be nice not to put the whole document inside the \texperanto
> macro. Would it be a good ideo to use this with \everypar to get the
> paragraph gsub'd?
>

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.

(I am not even familiar enough with Lua's syntax to write the solution; sorry!)

I am checking the documentation to get a better grasp about what is going on.

Thanks,

-- 
Luis Rivera
O< http://www.asciiribbon.org/ campaign



More information about the luatex mailing list