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}
4
  • 1
    LuaTeX is about combining Lua and TeX: if you look at ConTeXt, it is still a macro system using Lua _where it adds functionality
    – Joseph Wright
    Sep 14, 2018 at 18:00
  • I'm afraid I'm not really sure what you're driving at. Combining Lua and (La)TeX is my objective. How does ConTeXt fit into the picture? What distinction are you pointing out?
    – raiksey
    Sep 15, 2018 at 20:00
  • 1
    LuaTeX is developed by the ConTeXt developers: Hans et al. more than anyone know where to use Lua in a TeX system. They don't try to define everything from Lua: they use a mix, depending on what makes most sense. For macro definition, it's almost always going to be clearest and easiest to do things from the 'TeX side'.
    – Joseph Wright
    Sep 15, 2018 at 20:05
  • Note that if you use the luacode environment instead see luatex - Use lualatex to create macros - TeX - LaTeX Stack Exchange
    – user202729
    Jan 6, 2022 at 2:28

1 Answer 1

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

enter image description here

6
  • 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, 2018 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. Sep 14, 2018 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, 2018 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". Sep 15, 2018 at 20:20
  • 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. Dec 10, 2020 at 19:08

You must log in to answer this question.

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