我有一段代码可以工作:
Tex 文件:
\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 文件:
function textNode()
return "\\draw[xshift=50, yshift=50] node[draw, anchor=north west]{AAA};"
end
现在,如果我想取一个字符串并将其打印出来代替 AAA,那么过去 3/4 天我所做的各种尝试都失败了。
我的灵感来自于:
我想做的是写而不是\textNode
\textNode{"some
multiline
text"}
并将我的文本显示在我的 tikz 节点中。
在我尝试使用多线之前,我曾多次尝试过以下方法:
TeX 文件:
\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
我收到以下错误:
/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
您需要指定多行文本的宽度。
\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}