我在多个 tikzpicture 环境中使用 tikz 绘制一些节点。它们是完全相同的节点,所以我想是否可以创建一个 tikz 命令来反复绘制相同的 5 个节点?
最好的做法是只使用
\newcommand{\samefivenodes}{%
\node (a) {a};
\node[right=of a] (b) {b};
\node[right=of b] (c) {c};
\node[right=of c] (d) {d};
\node[right=of d] (e) {e};
};
答案1
与森林:
\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\begin{document}
\forestset{
declare toks={split five}{},
make five/.style={
before typesetting nodes={append'=a, split five=#1, delay={split option={split five}{:}{before five,five and,just five}}},
},
just five/.style={
for descendants={#1},
},
five and/.style={
for tree={#1},
},
before five/.style={
#1,
},
}
\begin{forest}
delay={
for tree={
if content={}{phantom}{
name/.option=content,
delay={content/.wrap value=\strut#1},
},
grow'=0,
},
},
before typesetting nodes={
for tree={
font=\sffamily
}
}
[[a[b[c,make five[d[e,make five=:{grow'=90}:make five]]]]][modified copy, make five={top color=black, bottom color=black, middle color=white, text=red, !1.edge=->}:{grow'=-90}:{inner color=blue!50!cyan, outer color=blue!30!cyan, text=white, thick, draw=blue!75!cyan, edge={blue!75!cyan, thick}, font=\sffamily\bfseries, circle} ]]
\end{forest}
\end{document}
将创建许多完全相同的副本,一些经过旋转,以及一个经过高度修改的版本: