# Defining new commands from LuaTeX

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}

• 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 '18 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 '18 at 20:00
• 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 '18 at 20:05

\documentclass{article}