是否有用于向节点添加默认文本的 TikZ 样式属性?例如,我希望能够执行以下操作:
\begin{tikzpicture}[%
stuff/.style={%
draw,
text=Bla bla
},
]
\node at (0,0) [stuff] {};
\node at (0,1) [stuff] {};
\node at (0,2) [stuff] {};
\node at (0,3) [stuff] {};
\end{tikzpicture}
生成包含 4 个节点的图片,每个节点都带有文本“Bla bla”。我希望能够创建具有相同文本的节点,而无需手动添加。
答案1
这并不完全是它的设计目的,但您可以向参数添加固定文本font
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%
stuff/.style={%
draw,
font={A}}
]
\node at (0,0) [stuff] {};
\node at (0,1) [stuff] {};
\node at (0,2) [stuff] {};
\node at (0,3) [stuff] {};
\end{tikzpicture}
\end{document}
答案2
TikZ 3.0 引入了node contents
替代{...}
还有另一个例子可以轻松测试它
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%
stuff/.style={%
draw,
node contents={A}}
]
\node at (0,0) [stuff];
\node at (0,1) [stuff];
\node at (0,2) [stuff];
\node at (0,3) [stuff, node contents={B}];
\end{tikzpicture}
\end{document}