1

I have this piece of code working :

Tex file :

\documentclass{article}

\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}

placeTextNode.lua file:

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

Now, if I want to take a string and print it in place of AAA, all the various attempts I did in the past 3/4 days have failed.

I took inspiration from :

What I'd like to do is to write instead of \textNode

\textNode{"some 
multiline 
text"}

and have my text displayed in my tikz node.

One of my multiple attempts, even before trying to go multiline, was the following :

TeX file :

\documentclass{article}

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

\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{"AAA"} \end{tikzpicture} \end{tcolorbox} \end{document}

placeTextNode.lua :

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

I get the following error :

/home/new command.tex:14: Undefined control sequence.
\\textNode ...tlua {tex.print(textNode(\luastring 
                                                  {#1}))}
l.14             \textNode{
                         "AAA"}
placeTextNode.lua:2: attempt to concatenate a table value (local 't')
stack traceback:
    placeTextNode.lua:2: in function 'textNode'
    [\directlua]:1: in main chunk.
\\textNode ...ex.print(textNode(\luastring {#1}))}

l.14 \textNode{ "AAA"}

1 Answers1

1

You need to specify text width for text to be multiline.

\documentclass{article}
\usepackage{luacode}

\begin{luacode} function textNode(s) return string.format("\draw[xshift=50, yshift=50] node[draw, anchor=north west, text width=1cm, text centered]{%s};", s) end \end{luacode}

\usepackage{tcolorbox,graphicx,tikz}

\newcommand*{\textNode}[1]{% \directlua{tex.print(textNode("\luaescapestring{#1}"))}% }

\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{abc\ def} \end{tikzpicture} \end{tcolorbox} \end{document}

Alan Xiang
  • 5,227
  • 1
    You fell for the number one gotcha. Use "\luaescapestring{\unexpanded{#1}}" (or \luastringN) instead of only "\luaescapestring{#1}". See also https://tex.stackexchange.com/questions/550673/what-are-some-typical-luatex-gotchas – Henri Menke May 16 '21 at 16:59
  • 1
    Also I'd just use return "\\draw ... {" .. s .. "};" since you are not actually using any of the features of string.format. – Henri Menke May 16 '21 at 17:01
  • @AlanXiang thank you for your working example. I have noticed that what I was typing wrong was in the first place \newcommand*{\textNode[1]} instead of \newcommand*{\textNode}[1]. I was putting the square bracket denoting the number of arguments inside the curly ones instead of after them. With that all my attemps using \luaString, \luastringN were failing. You put me back on track. – Filippo Guenzi May 16 '21 at 20:08