# [luatex] Manipulating arguments

Taco Hoekwater taco at elvenkind.com
Thu Aug 13 09:25:11 CEST 2009

Joseph Wright wrote:
> Hello all,
>
> A couple of things have come up on the LaTeX-L list (readers of that
> list will soon spot where) that I'd like to get a feel of in a LuaTeX
> context. Both are to do with arguments.
>
> 1) How does Lua affect TeX argument parsing.

Nothing much has changed at all (yet), we have been focusing on
completely different things than the input macro language.

> Is it possible, for
> example, to use LuaTeX to allow the currently impossible LaTeX:
>
> \footnote{\verb|%|}

Not really.

You can read input one token at a time using lua, but that only
assigns catcodes when luatex is reading a file at that time.
Inside a token list like a macro argument, the lua get_next
function just receives the next token in the list.

What you could do is apply preprocessing to the input stream before
catcodes are even assigned, but at that point it is just a line of
bytes and you would have to do all parsing yourself. That solution
is comparable to running an external preprocessor (and equally hard).

> 2) Can Lua be used to replace numbered arguments with names ones (at
> least when delimited argument are not needed):
>
> \def\foo #text{Stuff using #text}

Not really (again).

You could parse this with lua by redefining \def to read one token
at a time until the end of the macro body is reached, but that you
have to convert it back to a string with #1's in it to feed it back
into the 'real' \def.

If we ever support this directly, it will be either in the macro
language itself via a direct extension to the \def command, or by
having a pure lua method of defining control sequences, more like
this:

function do_stuff (text)
tex.print ("Stuff using " .. text)
end

tex.define('foo',
'macro',  -- the command code
{ {["param"] = "text"} }, -- the argument mapping
do_stuff}  -- the macro body

(argument mapping needs careful designing)

> (A bonus question for people with the correct knowledge: is ConTeXt Mark
> IV still using TeX to do basic parsing of input at the user level, or

Yes.

Best wishes,
Taco