我创建了以下示例脚本来绘制包含嵌套 tikz 的 TikZ:
\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.multipart, backgrounds, fit}
\tikzset{
box/.style={
draw,
rectangle,
minimum height=3cm,
fill=white,
align=center,
inner sep=2ex
}
}
\begin{document}
\begin{tikzpicture}[]
% Box 1
\node[box, label={Label 1}] (Box1) {\rotatebox{90}{Content 1}};
% Box 2 - Inner
\node[right=of Box1] (Box2) {
\begin{tikzpicture}
\node[box] (Inner) {\rotatebox{90}{\ttfamily{Something goes in here}}};
\end{tikzpicture}
};
% Box 2 - Outer
\begin{scope}[on background layer]
\node[box, inner sep=0ex, fit=(Box2), label={Label 2}] {};
\end{scope}
% Box 3
\node[box, right=of Box2, label={Label 3}] (Box3) {\rotatebox{90}{Content 2}};
\draw[dashed] (Box1) -- (Box2);
\draw[dashed] (Box2) -- (Box3);
\end{tikzpicture}
\end{document}
假设的真实内容Box2
(其中我们当前有占位符“这里有一些东西”)是一个具有许多节点的更复杂的 TikZ 绘图。
Box2 内是否有必要有\begin{tikzpicture}
?或者有更好的方法吗?我尝试不用,但会产生错误。
谢谢!
答案1
不要使用嵌套tikzpictures
- 开始绘制复杂的图形。
- 在层上将节点放在
fit
它周围background
。 - 在左侧绘制 Box1。
- 在右侧绘制 Box2。
重新排序的代码:
\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.multipart, backgrounds, fit}
\tikzset{
box/.style={
draw,
rectangle,
minimum height=3cm,
fill=white,
align=center,
inner sep=2ex
}
}
\begin{document}
\begin{tikzpicture}[]
% Box 2 - Inner
\node[box, font=\ttfamily, text width=2cm] (a) {Something goes in here};
\node[box, circle, below right=2mm and 5mm of a, font=\ttfamily, text width=2cm] (b) {Something goes in here};
% % Box 2 - Outer
\begin{scope}[on background layer]
\node[box, inner sep=1ex, fit=(a) (b), label={Label 2}] (Box2) {};
\end{scope}
% Box 1
\node[box, label={Label 1}, left =of Box2 ] (Box1) {\rotatebox{90}{Content 1}};
% Box 3
\node[box, right=of Box2, label={Label 3}] (Box3) {\rotatebox{90}{Content 2}};
\draw[dashed] (Box1) -- (Box2);
\draw[dashed] (Box2) -- (Box3);
\end{tikzpicture}
\end{document}