Wednesday, September 20, 2023

luatex

luatex

Using LuaTeX: Minimal Examples

Authors: Tran Thu Le, Chat GPT 3.5

1. Introduction

LuaTeX is an extension of TeX, a typesetting system that allows for high-quality typesetting of documents. LuaTeX provides a way to integrate Lua, a powerful and flexible scripting language, into TeX, enabling users to leverage the strengths of both systems. LaTeX, on the other hand, is a popular document preparation system built on top of TeX, providing high-level commands to facilitate document creation.

In this guide, we’ll explore three methods of executing Lua code within LaTeX documents using LuaTeX.

We’ll use Overleaf, an online LaTeX editor, for compiling and generating PDF files. To execute a lua code in a latex file, we should change the Compiler to LuaLaTex as follows:

  • In Overleaf, click on Menu
  • In Settings, Compiler, choose LuaLaTex

It is done. Now let try the first lua code in LaTex file.

2. “Hello World” Example Using directlua{}

The simplest way to use Lua in LuaTeX is through the \directlua{} command. Let’s start with a classic “Hello World” example:

\documentclass{article}
\begin{document}
\directlua{tex.sprint("Hello World")}
\end{document}

The result of this code in the pdf file is the text Hello World. Note that if you do not input "Hello World" as a string, let say you write \directlua{tex.sprint(Hello World)}, you’ll encounter an error.

3. Using the luacode Package

The luacode package allows for a more structured integration of Lua code within LaTeX. Let’s consider an example demonstrating the use of this package to define mathematical primitives:

\documentclass{article}
\usepackage{amsmath, amsfonts}
\usepackage{luacode}
\begin{luacode}
primal = {
    ["var"] = "\\mathbf{x}",
    ["mat"] = "\\mathbf{A}",
    ["func"] = "P",
    ["space"] = "\\mathbb{R}^m",
    ["loss_function"] = "f(\\primal{mat} \\primal{var})+g(\\primal{var})"
}
\end{luacode}
\newcommand{\primal}[1]{\directlua{tex.sprint(primal["#1"])}}

\begin{document}
\begin{equation*}
    \min_{ \primal{var} \in \primal{space} } \quad
    \primal{func}(\primal{var}) = \primal{loss_function}
\end{equation*}
\end{document}

Compiling it, we should obtain the following output,

minxRmP(x)=f(Ax)+g(x)\min_{\mathbf x \in \mathbb R^m} \quad P(\mathbf x) = f(\mathbf A\mathbf x) + g(\mathbf x)

4. Separating Lua Code into a Separate File

Another approach is to keep the Lua code in a separate file and load it into the LaTeX document. Let’s demonstrate this method using a Lua file, say def.lua, and a corresponding LaTeX file.

Lua File (def.lua)

primal = {
    ["var"] = "\\mathbf{x}",
    ["mat"] = "\\mathbf{A}",
    ["func"] = "P",
    ["space"] = "\\mathbb{R}^m",
    ["loss_function"] = "f(\\primal{mat} \\primal{var})+g(\\primal{var})"
}

TeX File

\documentclass{article}
\usepackage{amsmath, amsfonts}
\usepackage{luacode}

\directlua{dofile("def.lua")} % Load the Lua file
\newcommand{\primal}[1]{\directlua{tex.sprint(primal["#1"])}}

\begin{document}
\begin{equation*}
    \min_{ \primal{var} \in \primal{space} } \quad
    \primal{func}(\primal{var}) = \primal{loss_function}
\end{equation*}
\end{document}

5. Conclusion

LuaTeX offers a powerful and flexible way to integrate Lua scripting into LaTeX documents, providing users with enhanced capabilities and functionality. In this guide, we explored three methods of executing Lua code within LaTeX:

  1. Direct Lua Execution: The simplest approach involves using \directlua{} to directly embed Lua code within a LaTeX document. This method is straightforward and ideal for simple Lua operations.

  2. Using the Lua code Package: The luacode package provides a structured way to incorporate Lua code into LaTeX. It allows for more organized management of Lua code and is particularly useful for defining complex structures or functions.

  3. Separating Lua Code into a Separate File: For better organization and reusability, Lua code can be stored in a separate file and then loaded into the LaTeX document. This approach keeps the Lua code isolated and easily maintainable.

Each method offers its own advantages, catering to various use cases and preferences. LuaTeX’s seamless integration of Lua empowers users to enhance their LaTeX documents with dynamic and customized content. Experiment with these approaches to unlock the full potential of LuaTeX in your LaTeX projects.

Happy typesetting with LuaTeX!

Popular Posts