我正在尝试在 tikz 中创建嵌套图表。我已经完成了一个非嵌套图表,但我正在尝试将蓝色框嵌套在第一个黄色框内。我正在尝试遵循给出的建议这里,所以我在 tikz 图片内创建了一个 tikz 图片。MWE 如下。 但是,这会使我的所有节点间距偏离(出于某种原因,嵌套图片内的路径不再对齐),并且还会使决策框具有圆角……这虽然很小,但可能相关?此外,我在侧面有数学公式,这些公式取决于蓝色框中的节点。我认为它们看起来仍会与嵌套的 tikz 图片一起编译,但前两个数学公式现在重叠了。
所以,我有几个问题。这是嵌套流程图的最佳方式吗?我可以使用“外部节点”来引用嵌套 tikz 图片中定义的节点吗?如何修复节点路径上的间距?
编辑:有人指出,嵌套 tikz 图片不是一种好的做法。下面的多个解决方案修改了 tikz 元素,使得图形看起来是嵌套的,而实际上并没有嵌套
tikzpicture
环境。
两个图表的 MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning}
\begin{document}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=yellow!40,
text width=4.5em, text badly centered, node distance=1.75cm, inner sep=0pt]
\tikzstyle{decisiong} = [diamond, draw, fill=blue!40,
text width=4.5em, text badly centered, node distance=1.75cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, node distance=1.75cm, minimum width=1cm, minimum height=0.5cm, draw, fill=yellow!20,
text width=20em, text centered, rounded corners, minimum height=1.5em]
\tikzstyle{blockg} = [rectangle, minimum width=1cm, minimum height=0.5cm, draw, fill=blue!20,
text width=15em, text centered, rounded corners, minimum height=1.5em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=yellow!20, node distance=1.5cm,
minimum height=2em]
\tikzstyle{invis} = [draw, fill=yellow!10, node distance=4.25cm,
minimum height=2em]
\tikzstyle{invisg} = [draw, fill=yellow!10, node distance=3.5cm,
minimum height=2em]
\tikzstyle{matheq} = [node distance=8.75cm, text width=21em, minimum width=1cm,
minimum height=2em, text centered]
\begin{tikzpicture}[node distance = 1.25cm, auto]
% Place nodes
\node [cloud] (init) {update time};
\node [matheq, right of=init] {$math$};
\node [block, below of=init] (second) {iterate through all elements};
\node [blockg, below of=second] (third) {local update};
\node [matheq, right of=third] {$math$};
\node [blockg, below of=third] (fourth) {calculate,\\ update };
\node [matheq, right of=fourth] {$math$};
\node [blockg, below of=fourth] (fifth) {calculate stuff};
\node [matheq, right of=fifth] {$math$};
\node [decisiong, below of=fifth] (conver1) {{\scriptsize converged?}};
% Draw edges
\path [line] (init) -- (second);
\path [line] (second) -- (third);
\path [line] (third) -- (fourth);
\path [line] (fourth) -- (fifth);
\path [line] (fifth) -- (conver1);
\coordinate[left of=third] (c1); %This is so the path doesn't intersect the diagram
\coordinate[left of=conver1] (d1);
\path [line] (conver1) -| node [near start] {no} ([xshift=-2.5cm]d1) -- ([xshift=-2.5cm]c1)-- (third);
\node [block, below of=conver1] (solve) {solve global equations};
\node [matheq, right of=solve] {$math$
\\ $math$ \\ $math$};
\node [decision, below of=solve] (conver) {{\scriptsize converged?}};
\coordinate[right of=init] (a1); %This is so the path doesn't intersect the diagram
\coordinate[left of=conver] (b1);
\coordinate[right of=conver] (e1);
\coordinate[left of=second] (f1);
% Draw edges
\path [line] (conver1) --node {yes} (solve);
\path [line] (solve) -- (conver);
\path [line] (conver) -| node [near start] {no} ([xshift=-3.25cm]b1) -- ([xshift=-3.25cm]f1)-- (second);
\path [line] (conver) -| node [near start] {yes} ([xshift=3.25cm]e1) -- ([xshift=3.25cm]a1)-- (init);
\end{tikzpicture}
\clearpage
This should be the same chart, but the blue boxes should be nested inside the first yellow box.
\begin{tikzpicture}[node distance = 1.25cm, auto]
% Place nodes
\node [cloud] (init) {update time};
\node [matheq, right of=init] {$math$};
\node [block, below of=init] (second) {iterate through all elements
\begin{tikzpicture}[node distance = 1.25cm, auto]
\node [blockg] (third) {local update};
\node [blockg, below of=third] (fourth) {calculate,\\ update};
\node [blockg, below of=fourth] (fifth) {calculate stuff};
\node [decisiong, below of=fifth] (conver1) {{\scriptsize converged?}};
\path [line] (third) -- (fourth);
\path [line] (fourth) -- (fifth);
\path [line] (fifth) -- (conver1);
\end{tikzpicture}
};
%Math equations
\node [matheq, right of=third] {$math$};
\node [matheq, right of=fourth] {$math$};
\node [matheq, right of=fifth] {$math$};
% Draw edges
\path [line] (init) -- (second);
%\path [line] (second) -- (third);
\coordinate[left of=third] (c1);
\coordinate[left of=conver1] (d1);
\path [line] (conver1) -| node [near start] {no} ([xshift=-2.5cm]d1) -- ([xshift=-2.5cm]c1)-- (third);
\node [block, below of=conver1] (solve) {solve global equations};
\node [matheq, right of=solve] {$math$
\\ $math$ \\ $math$};
\node [decision, below of=solve] (conver) {{\scriptsize converged?}};
\coordinate[right of=init] (a1);
\coordinate[left of=conver] (b1);
\coordinate[right of=conver] (e1);
\coordinate[left of=second] (f1);
% Draw edges
\path [line] (conver1) --node {yes} (solve);
\path [line] (solve) -- (conver);
\path [line] (conver) -| node [near start] {no} ([xshift=-3.25cm]b1) -- ([xshift=-3.25cm]f1)-- (second);
\path [line] (conver) -| node [near start] {yes} ([xshift=3.25cm]e1) -- ([xshift=3.25cm]a1)-- (init);
\end{tikzpicture}
\end{document}
答案1
下面您将看到一个使用背景拟合节点而不是嵌套的示例tikzpicture
。这是您想要的吗?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning, fit, backgrounds}
\begin{document}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=yellow!40,
text width=4.5em, text badly centered, node distance=1.75cm, inner sep=0pt]
\tikzstyle{decisiong} = [diamond, draw, fill=blue!40,
text width=4.5em, text badly centered, node distance=1.75cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, node distance=1.75cm, minimum width=1cm, minimum height=0.5cm, draw, fill=yellow!20,
text width=20em, text centered, rounded corners, minimum height=1.5em]
\tikzstyle{blockg} = [rectangle, minimum width=1cm, minimum height=0.5cm, draw, fill=blue!20,
text width=15em, text centered, rounded corners, minimum height=1.5em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=yellow!20, node distance=1.5cm,
minimum height=2em]
\tikzstyle{invis} = [draw, fill=yellow!10, node distance=4.25cm,
minimum height=2em]
\tikzstyle{invisg} = [draw, fill=yellow!10, node distance=3.5cm,
minimum height=2em]
\tikzstyle{matheq} = [node distance=8.75cm, text width=21em, minimum width=1cm,
minimum height=2em, text centered]
\begin{tikzpicture}[node distance = 1.25cm, auto]
% Place nodes
\node [cloud] (init) {update time};
\node [matheq, right of=init] {$math$};
\node [block, below of=init] (second) {iterate through all elements};
\node [blockg, below of=second] (third) {local update};
\node [matheq, right of=third] {$math$};
\node [blockg, below of=third] (fourth) {calculate,\\ update };
\node [matheq, right of=fourth] {$math$};
\node [blockg, below of=fourth] (fifth) {calculate stuff};
\node [matheq, right of=fifth] {$math$};
\node [decisiong, below of=fifth] (conver1) {{\scriptsize converged?}};
\begin{scope}[on background layer]
\node[draw, fill=yellow, fit=(second) (conver1), inner xsep=5mm]{};
\end{scope}
% Draw edges
\path [line] (init) -- (second);
\path [line] (second) -- (third);
\path [line] (third) -- (fourth);
\path [line] (fourth) -- (fifth);
\path [line] (fifth) -- (conver1);
\coordinate[left of=third] (c1); %This is so the path doesn't intersect the diagram
\coordinate[left of=conver1] (d1);
\path [line] (conver1) -| node [near start] {no} ([xshift=-2.5cm]d1) -- ([xshift=-2.5cm]c1)-- (third);
\node [block, below of=conver1] (solve) {solve global equations};
\node [matheq, right of=solve] {$math$
\\ $math$ \\ $math$};
\node [decision, below of=solve] (conver) {{\scriptsize converged?}};
\coordinate[right of=init] (a1); %This is so the path doesn't intersect the diagram
\coordinate[left of=conver] (b1);
\coordinate[right of=conver] (e1);
\coordinate[left of=second] (f1);
% Draw edges
\path [line] (conver1) --node {yes} (solve);
\path [line] (solve) -- (conver);
\path [line] (conver) -| node [near start] {no} ([xshift=-3.25cm]b1) -- ([xshift=-3.25cm]f1)-- (second);
\path [line] (conver) -| node [near start] {yes} ([xshift=3.25cm]e1) -- ([xshift=3.25cm]a1)-- (init);
\end{tikzpicture}
\end{document}
答案2
当我找到自己的解决方法时,我也想在这里发布它。它创建一个假节点并将其移动,但它保留了图表的外观:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning,fit,backgrounds}
\begin{document}
\tikzstyle{decision} = [diamond, draw, fill=yellow!40,
text width=4.5em, text badly centered, node distance=1.75cm, inner sep=0pt]
\tikzstyle{decisiong} = [diamond, draw, fill=blue!40,
text width=4.5em, text badly centered, node distance=1.75cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, node distance=1.75cm, minimum width=1cm, minimum height=0.5cm, draw, fill=yellow!20,
text width=20em, text centered, rounded corners, minimum height=1.5em]
\tikzstyle{blockjusttext} = [rectangle, node distance=1.75cm, minimum width=1cm, minimum height=0.5cm,
text width=20em, text centered, rounded corners, minimum height=1.5em]
\tikzstyle{blockfill} = [rectangle, node distance=1.75cm, minimum width=1cm, minimum height=14cm, draw, fill=yellow!20,
text width=20em, text centered, rounded corners, minimum height=16.8em]
\tikzstyle{blockg} = [rectangle, minimum width=1cm, minimum height=0.5cm, draw, fill=blue!20,
text width=15em, text centered, rounded corners, minimum height=1.5em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=yellow!20, node distance=1.5cm,
minimum height=2em]
\tikzstyle{invis} = [draw, fill=yellow!10, node distance=4.25cm,
minimum height=2em]
\tikzstyle{invisg} = [draw, fill=yellow!10, node distance=3.5cm,
minimum height=2em]
\tikzstyle{matheq} = [node distance=8.75cm, text width=21em, minimum width=1cm,
minimum height=2em, text centered]
\begin{tikzpicture}[node distance = 1.25cm, auto]
% Place nodes
\node [cloud] (init) {update time};
\node [matheq, right of=init] {$math$};
\node [blockfill, below=0.5cm of init] (second2) {};
\node [blockjusttext, below=0.5cm of init] (second) {iterate through all elements};
\node [blockg, below =-0.1cm of second] (third) {local update };
\node [matheq, right of=third] {$math$};
\node [blockg, below of=third] (fourth) {calculate,\\ update};
\node [matheq, right of=fourth] {$math$};
\node [blockg, below of=fourth] (fifth) {calculate stuff};
\node [matheq, right of=fifth] {$math$};
\node [decisiong, below of=fifth] (conver1) {{\scriptsize converged?}};
% Draw edges
\path [line] (init) -- (second);
\path [line] (third) -- (fourth);
\path [line] (fourth) -- (fifth);
\path [line] (fifth) -- (conver1);
\coordinate[left of=third] (c1);
\coordinate[left of=conver1] (d1);
\path [line] (conver1) -| node [near start] {no} ([xshift=-2.1cm]d1) -- ([xshift=-2.1cm]c1)-- (third);
\node [block, below of=conver1] (solve) {solve global equations};
\node [matheq, right of=solve] {$math$
\\ $math$ \\ $math$};
\node [decision, below of=solve] (conver) {{\scriptsize converged?}};
\coordinate[right of=init] (a1);
\coordinate[left of=conver] (b1);
\coordinate[right of=conver] (e1);
\coordinate[left of=second] (f1);
% Draw edges
\path [line] (conver1) --node {yes} (solve);
\path [line] (solve) -- (conver);
\path [line] (conver) -| node [near start] {no} ([xshift=-3.25cm]b1) -- ([xshift=-3.25cm]f1)-- (second);
\path [line] (conver) -| node [near start] {yes} ([xshift=3.25cm]e1) -- ([xshift=3.25cm]a1)-- (init);
\end{tikzpicture}
\end{document}