4

Does lua scripting in LuaTeX entirely eliminate the need to use the TeX macro-definition commands \def, \gdef, \edef and \xdef?

What about \exapndafter, \noexpand, \protected, \long and \relax?

Evan Aad
  • 11,066

2 Answers2

10

It is possible to directly access the typesetting capabilities of luatex directly from the lua side without using tex macros see for example

http://wiki.luatex.org/index.php/TeX_without_TeX

However this does not use TeX macro expansion at all for example you could (and people have) arrange to pull text directly from some database via a Lua binding and directly construct typeset nodes as would have been produced by the normal tex workflow.

So as Lua does not use tex macro expansion, it does not make sense to ask if it can replace individual macro-related commands such as def or expandafter.

David Carlisle
  • 757,742
10

No, Lua does not replace TeX's macro expansion language. LuaTeX implements the token library which offers some very limited interoperability between TeX macros and Lua. For example, you can scan ahead for tokens and thus simulate the behaviour of primitives like \hskip which do not take an argument but scan for a dimension.

\def\scanandprintskip{%
  \directlua{
    local d = token.scan_dimen()
    tex.sprint(d)
  }%
}

\dimen0=10pt
\scanandprintskip 12pt
\scanandprintskip \dimen0
\scanandprintskip garbage % ! Missing number, treated as zero.

\bye

You can also set the replacement text of control sequences, but without access to the argument specification, i.e. control sequences defined that way do not take arguments.

\directlua{token.set_macro("hello", "World!")}

\hello % prints World!

\bye

But as far as I know there is no way to access the replacement text of control sequences from Lua other than passing it as a string as function argument.

\def\hello{World!}

\directlua{tex.sprint([[\hello]])}

\bye

Keep in mind that \directlua always performs full edef-type expansion, which can be inhibited by using, e.g., \unexpanded.

For more details see section »The token library« in the LuaTeX manual.

Henri Menke
  • 109,596
  • 2
    I think it's a matter of interpretation about what the question means: as you say, it is not (yet?) possible from Lua code to write something that would be exactly equivalent to constructions like \edef. On the other hand, if one looks at the purposes that TeX macros were written / meant to be used for (text substitution, conditional and looping constructs), then most of these can be done in Lua code directly… although with a different syntax. (Nevertheless this is a great answer and pointing to the token library is helpful.) – ShreevatsaR Jun 21 '17 at 02:45
  • Just wondering: if lua isn't meant to replace TeX's macro definition language, what is the point in the entire LuaTeX project? It just adds another substantial hump to the already gargantuan TeX/LaTeX monster. Is there any notable typesetting challenge that TeX/LaTeX/XeTeX isn't already able to meet with panache? I get projects like SILE, whose vision is to replace TeX altogether, but LuaTeX seems just to add a big, unneeded patch to the suit. – Evan Aad Jun 21 '17 at 07:01
  • 1
  • @EvanAad Also SILE is useless to me as it can't do math. – Henri Menke Jun 21 '17 at 07:11