# [luatex] macro definition inside a callback

Paul Isambert zappathustra at free.fr
Fri Sep 7 09:45:30 CEST 2012

Arno Trautmann <Arno.Trautmann at gmx.de> a écrit:
> maybe a somewhat strange question again from me: Is it possible to
> define a macro inside the *_linebreak_filters? I want to use a function
> in the pre_linebreak_filter, and this function should define a macro. So
> the macro should be present with a new definition at the beginning of
> the next paragraph, but only if the previous one has been typeset with
> that function registered, which removes itself after running.
>
> I tried something like
>
> tex.print("\\gdef\\something{}")
>
> but this does not work as the effect of tex.print is not defined in the
> *_linebreak_filters, if I remember correctly.
>
> So, is there any way to do this?

I can see two solutions (both untested); the first is a ugly hack, I
wouldn't count much on it. In both, the target macro is \mymacro; in the
second solution, the definition of that macro is left untouched, since
it simply prints a Lua variable, whose contents changes instead.

However, with Lua under the hood, I'm sure there are other ways to
achieve what you want without any single macro.

% SOLUTION 1

-- Should be modified to take care of functions possibly registered in
-- process_input_buffer.
function redefine (str)
callback.register("process_input_buffer",
function (s)
callback.register("process_input_buffer", nil)
return "\\gdef\\mymacro{" .. str .. "}" .. s
end)
end

-- Then, in whatever linebreak callback:
callback.register("pre_linebreak_filter",
function (...)
...
redefine(<something>)
...
end)

% SOLUTION 2

% Tex:
\def\mymacro{\directlua{tex.print(macro_definition)}}

% Lua:
callback.register("pre_linebreak_filter",
function (...)
...
macro_definition = <something>
...
end)

Best,
Paul