Showing posts with label Lua. Show all posts
Showing posts with label Lua. Show all posts

Wednesday, September 20, 2023

Latex_commands_using_LuaTex

Latex_commands_using_LuaTex

Creating Stable LaTeX Commands with Underscores and Dots using LuaTeX

1. Motivation

In LaTeX, the standard way to create LaTeX commands/macros is via \newcommand{}{}. For example:

\newcommand{\primalVar}{\mathbf{x}}

where the macro \primalVar creates a bold variable x\mathbf{x}. To call this command, we simply write \primalVar.

However, the newly defined command \primalVar (using \newcommand{}{}) does not accommodate special characters like underscore _ and dot ., making the command hard to read, especially for lengthy commands. Taking inspiration from programming languages like Python, it would be preferable to utilize commands such as \primal.var or \primal_var.

In LaTeX, we can define such commands using \def keyword:

\def\primal.var{\mathbf{x}}

or

\def\primal_var{\mathbf{x}}

However, when using these commands in math mode, you may encounter errors or conflicts due to the altered meanings of . and _ in math mode.

To define such commands to be stable in math mode, we propose in this post a method using LuaTeX.

2. Solution approach using LuaTex

  1. Create a variable LuaData in Lua code.

  2. Create a set method (using Latex command) to add key "primal_var" and value "\\mathbf{x}" into LuaData.

  3. Create a get method (using LaTeX command) to extract the value from the key.

The following is a minimal example demonstrating the suggested approach for creating such macros. To set and get macros, use the following syntaxes:

\setLuaData{primal_var}{\\mathbf{x}}
\getLuaData{primal_var}

In the following example code, please note that the desired output should resemble the following:
minxRnP(x)\min_{\mathbf{x} \in \mathbb R^n}\quad P(\mathbf{x})

\documentclass{article}

% Internal settings for LuaData
\usepackage{luacode}
\begin{luacode}
LuaData = {}
\end{luacode}
\newcommand{\setLuaData}[2]{\directlua{LuaData["#1"] = "#2"}}
\newcommand{\getLuaData}[1]{\directlua{tex.sprint(LuaData["#1"])}}

% LuaData defined by user
\usepackage{amsfonts}
\setLuaData{primal_var}{\\mathbf{x}}
\setLuaData{primal_func}{P}
\setLuaData{primal_space}{\\mathbb{R}^n}
\newcommand{\luda}[1]{\getLuaData{#1}} % optional

\begin{document}
$$\min_{\luda{primal_var} \in \luda{primal_space}} \quad 
\luda{primal_func}(\luda{primal_var})$$
\end{document}

Note. The provided code is sensitive to spaces. For instance, an error will occur if a space is present in the key, as in \getLuaData{primal_func }. To mitigate this issue, we need to remove spaces from the key. The following demonstrates how to remove spaces from the key, resulting in a command that is insensitive to spaces. Additionally, it throws an error when you try to get a key that is not defined.

\documentclass{article}

% Internal settings for LuaData
\usepackage{luacode}
\begin{luacode}
LuaData = {}
function setLuaData(key, value)
    local key = key:gsub("%s", "") -- remove spaces
    LuaData[key] = value
end
function getLuaData(key)
    local key = key:gsub("%s", "") -- remove spaces
    if LuaData[key] then
        tex.sprint(LuaData[key])
    else
        tex.sprint("LuaDataError")
    end
end
\end{luacode}
\newcommand{\setLuaData}[2]{\directlua{setLuaData("#1","#2")}}
\newcommand{\getLuaData}[1]{\directlua{getLuaData("#1")}}

% LuaData defined by user
\usepackage{amsfonts}
\setLuaData{primal_var}{\\mathbf{x}}
\setLuaData{primal_func}{P}
\setLuaData{primal_space}{\\mathbb{R}^n}
\newcommand{\luda}[1]{\getLuaData{#1}} % optional

\begin{document}
$$\min_{\luda{primal_var} \in \luda{primal_space}} \quad 
\luda{primal_func}(\luda{primal_var})$$
\end{document}

3. Conclusion

In this post, we suggested a method using LuaTeX to create stable LaTeX commands with underscores and dots, allowing for more versatile and readable macro names in LaTeX documents.

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