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 . 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
-
Create a variable
LuaData
in Lua code. -
Create a set method (using Latex command) to add key
"primal_var"
and value"\\mathbf{x}"
intoLuaData
. -
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:
\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.