LuaLaTeX writing LaTeX commands

I am trying to have LuaLatex automate some LaTex.

For example if I write :

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{tcolorbox,graphicx,tikz}

\usepackage[paperwidth=120pt, paperheight=120pt, margin=10pt]{geometry}
\begin{document}
\begin{tcolorbox}[boxsep=0pt, left=0pt, right=0pt, top=0pt, bottom=0pt, sharpish corners, opacityframe=0, opacityback=0, boxrule=0pt]
\begin{tikzpicture}[x=1pt, y=1pt]
\draw[line width=0mm, white] (0,0) -- (100,0) -- (100,100) -- (0, 100);
\draw[xshift=50, yshift=50] node[draw, anchor=north west]{AAA};
\end{tikzpicture}
\end{tcolorbox}
\end{document}


my build works.

Now I would like to make Lua write this line :

            \draw[xshift=50, yshift=50] node[draw, anchor=north west]{AAA};


If I write :


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{tcolorbox,graphicx,tikz}
\directlua{dofile("placeTextNode.lua")}
\newcommand*{\textNode}{%
\directlua{tex.write(textNode())}%
}

\usepackage[paperwidth=120pt, paperheight=120pt, margin=10pt]{geometry}
\begin{document}
\begin{tcolorbox}[boxsep=0pt, left=0pt, right=0pt, top=0pt, bottom=0pt, sharpish corners, opacityframe=0, opacityback=0, boxrule=0pt]
\begin{tikzpicture}[x=1pt, y=1pt]
\draw[line width=0mm, white] (0,0) -- (100,0) -- (100,100) -- (0, 100);
\textNode
\end{tikzpicture}
\end{tcolorbox}
\end{document}


and

function textNode()
return "\draw[xshift=50, yshift=50] node[draw, anchor=north west]{AAA};"
end


I have an error message saying :

invalid escape sequence near '"\d'

If I write :

function textNode()
return "\\draw[xshift=50, yshift=50] node[draw, anchor=north west]{AAA};"
end


having added a \ in front of \draw, I don't have the error anymore, but the node does not appear.

Do you have an idea of what I should write in Lua ?

Thank you.

• I'd suggest some change in your question title, something like LuaLaTeX automate LaTeX writing – sztruks May 13 at 9:45
• You have to use tex.sprint. – Henri Menke May 13 at 9:45
• Do you plan to do more complicated printings or not? Because your example doesn't really need LuaLaTeX. – projetmbc May 13 at 12:25
• @projetmbc Yes indeed. – Filippo Guenzi May 13 at 13:58

As written in the documentation, tex.write is intended "for use as a quick way to dump information". It intentionally adjusts catcodes to ensure that it's result does not get interpreted as special TeX commands. To pass TeX code, you need one of the tex.print/sprint/tprint functions instead. Since you want to print full lines with standard catcodes, tex.print is the most appropriate:

\documentclass{article}

% \usepackage[utf8]{inputenc}
\usepackage{tcolorbox,graphicx,tikz}
\directlua{dofile("placeTextNode.lua")}
\newcommand*{\textNode}{%
\directlua{tex.print(textNode())}%
}

\usepackage[paperwidth=120pt, paperheight=120pt, margin=10pt]{geometry}
\begin{document}
\begin{tcolorbox}[boxsep=0pt, left=0pt, right=0pt, top=0pt, bottom=0pt, sharpish corners, opacityframe=0, opacityback=0, boxrule=0pt]
\begin{tikzpicture}[x=1pt, y=1pt]
\draw[line width=0mm, white] (0,0) -- (100,0) -- (100,100) -- (0, 100);
\textNode
\end{tikzpicture}
\end{tcolorbox}
\end{document}


• I don't know why but to me it says the following with print : [\directlua]:1: attempt to call a nil value (global 'textNode') stack traceback: [\directlua]:1: in main chunk. \textNode ->\directlua {tex.print(textNode())} l.15 \textNode – Filippo Guenzi May 13 at 11:10
• @FilippoGuenzi That message means that there is no defined function textNode. Either you no longer load the file placeTextNode.lua or you changed the file to no longer define the function. If placeTextNode.lua exists and contains the second definition of textNode from your question (the one with \\draw) then the code from my answer will work – Marcel Krüger May 13 at 11:26
• Thank you I didn't understand that I had to keep the \\draw version in the Lua file. It works now. – Filippo Guenzi May 13 at 13:56
• I was attempting to add text as a parameter and now get 0 as text :  function textNode(t) return "\\draw[xshift=50, yshift=50] node[draw, anchor=north west]{" .. t .. "};" end  \textNode{BBB} Do you know what should I do to get BBB. Even \textNode{"BBB"} produces the same. – Filippo Guenzi May 13 at 15:19
• @FilippoGuenzi That seems like it's a separate question. See e.g. tex.stackexchange.com/questions/492630/… to learn how to pass parameters to Lua functions. – Marcel Krüger May 13 at 17:29