10

Latex provides several commands relating to creating new commands such as \newcommand. If I want to define new commands from a Lua script one way is use tex.print to write this directly into the document.

It this really the best way though? The whole point of LuaTeX is to avoid programming with Tex macros, yet this approaches involves doing exactly that. How can I directly access, edit and create commands directly from Lua?

script.lua

tex.print("\\newcommand{\\hello}{Hello world}")

document.tex

\documentclass{article}
\begin{document}
    \directlua{ require("script") }
    \hello
\end{document}
Mico
  • 506,678
raiksey
  • 221

1 Answers1

12
\documentclass{article}
\begin{document}
    \directlua{token.set_macro("hello","goodbye") }
    \hello
\end{document}

enter image description here

David Carlisle
  • 757,742
  • Accepted, thank you. Do you know of any good packages/classes on CTAN I could look at that are implemented in Lua and do this sort of thing? (To try to understand the common patterns a bit better. I'm looking at building a document class) – raiksey Sep 14 '18 at 16:54
  • 3
    @raiksey I think it's rather rare to be setting tex macros from lua, code either is all in tex or all the way to typesetting in lua, this is half and half, it can be useful sometimes though. – David Carlisle Sep 14 '18 at 19:08
  • To outline my problem at a higher level, I'm trying to create (or adapt) a CV document class/template. Rather than deal with tex macros which are infamously difficult to wrangle with, I thought it would be a better approach to code the "logic" of the template in lua using Luatex, but still have the class be usable much like any other document class, with custom latex commands for cv sections, social media handles etc. Hence the need to define new commands in lua. Is this not a reasonable approach? This is all very much a learning experience for me. – raiksey Sep 15 '18 at 19:57
  • 1
    @raiksey it is much easier to define tex commands from the tex side than the lua side. In tex you have the tex parser available so the definition is automatically tokenised, but in lua if you are not just defining a simple string as here you'd have to build up the tokens "by hand". – David Carlisle Sep 15 '18 at 20:20
  • To add a use case for token.set_macro: for a CI pipeline, I am fetching git metadata (in this example, short SHA and ref name) from environment variables (as predefined by the CI env), to print in the PDF. If those aren't available, fallback commands like git rev-parse --short HEAD are tried (e.g. for local compilation). The result (just a string) is written into a macro using set_macro. Accessing env variables (os.getenv) and running outside commands (pcall, io.popen) is easy to do in Lua but, as far as I know, much, much harder in (La)TeX. – Alex Povel Dec 10 '20 at 19:02
  • 1
    @AlexPovel yes as I say if the macro replacement text is a string the lua setting has better use cases, but if you want to do the equivalent of \def\foo{$\sqrt{\phi}+\cos 2$} then doing it in tex is a lot easier than doing it in lua as you would have to build each token separately and build up the token list. – David Carlisle Dec 10 '20 at 19:08