Choice of processor
Joseph Wright
joseph at texdev.net
Wed Mar 5 13:02:29 CET 2025
On 05/03/2025 11:59, Philip Taylor wrote:
>
> On 05/03/2025 11:50, Joseph Wright wrote:
>>
>> The above [snipped] relies on various other bits of Lua as LaTeX does
>> various bits
>> of emulation and tracks things. For a one-off use, you could go with
>>
>> \directlua{
>> local t = lua.get_functions_table()
>> local minus_tok = token.new(string.byte('-'), 12)
>> local zero_tok = token.new(string.byte('0'), 12)
>> local one_tok = token.new(string.byte('1'), 12)
>> t[1] = function()
>> local first = token.scan_string()
>> local second = token.scan_string()
>> if first < second then
>> token.put_next(minus_tok, one_tok)
>> else
>> token.put_next(first == second and zero_tok or one_tok)
>> end
>> end
>> }
>> \luadef\strcmp 1 %
>> \strcmp{A}{A}
>> \strcmp{A}{B}
>> \strcmp{B}{A}
>> \bye
>>
>> which hard-codes that we are using a known Lua function - done like
>> this, \strcmp has the same behaviour as a primitive.
>
> Thank you very much, Joseph — much appreciated. But could I trouble to
> explain the "\luadef\strcmp 1 % " part ? Does that in some way bind
> \strcmp to the preceding \directlua code ?
Indeed. What I've done is directly access the table of Lua functions (t
= lua.get_functions_table()) then added as index position 1 the
anonymous Lua function that will implement \strcmp. \luadef then links
the TeX token \strcmp to the Lua code at index position 1. The LuaTeX
manual covers this with some 'health warnings'.
In the LaTeX version, we take more precautions to avoid zapping other
people's functions, etc. - but as a result, there's more stuff you need.
So for a plain TeX user, the above is likely safe (depending on whether
they then load other Lua code).
Joseph
Joseph
More information about the texhax
mailing list.