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}
  • 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 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 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 at 20:05
up vote 11 down vote accepted
\documentclass{article}
\begin{document}
    \directlua{token.set_macro("hello","goodbye") }
    \hello
\end{document}

enter image description here

  • 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 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 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 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 at 20:20

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.