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?

10
  • 2
    The question is not really answerable. It is possible to typeset directly from lua, but not if you want the input syntax to be anything like TeX. Commented Jun 20, 2017 at 20:53
  • @DavidCarlisle: What do you mean by 'input syntax'? Passing of arguments?
    – Evan Aad
    Commented Jun 20, 2017 at 20:57
  • I mean any syntax using \ or { or involving expansion of macros. Commented Jun 20, 2017 at 20:58
  • 2
    As @DavidCarlisle says, this is not really amenable to an answer. There are typesetting systems other than TeX, and they can be written in whatever. SILE would be an obvious one using the typesetting ability derived from LuaTeX but with no TeX component. On the other hand, ConTeXt MkIV uses LuaTeX but is programmed in a mix of TeX and Lua. So it very much depends what one wants.
    – Joseph Wright
    Commented Jun 20, 2017 at 21:01
  • 3
    @EvanAad questions about the details of the lua language are probably better asked on stackoverflow, but the basic model is strict but you can implement what amounts to lazy evaluation by introducing suitable levels of indirection eg google turns up stackoverflow.com/questions/24518121/… Commented Jun 20, 2017 at 21:35

2 Answers 2

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.

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.

4
  • 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.) Commented Jun 21, 2017 at 2: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
    Commented Jun 21, 2017 at 7:01
  • 1
    @EvanAad Callbacks. Commented Jun 21, 2017 at 7:09
  • @EvanAad Also SILE is useless to me as it can't do math. Commented Jun 21, 2017 at 7:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .