我正在使用 tikz 直接在 LaTeX 文档中创建流程图。虽然我使用 LaTeX 有相当多的经验,但我承认自己是 tikz 的新手。
我的问题是当尝试用流程图显示无限循环时,即在同一节点开始和结束的边或路径。
例如,
是使用以下 LaTeX 代码我想要的结果(右)和我得到的结果(左)的示例:
\usetikzlibrary{shapes.geometric,arrows}
\tikzstyle{decision} = [ diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep-0pt]
\tikzstyle{block} = [ rectangle, draw, fill=blue!20, text width=5em, text badly centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [ draw, -latex']
\tikzstyle{terminator} = [ draw, ellipse, fill=red!20, node distance=3cm, minimum height=2em]
\begin{tikzpicture}[node distance=2cm, auto]
\node [terminator] (puc) {Power-Up Reset};
\node [block, below of=puc] (wdt) {Stop Watchdog};
\node [block, below of=wdt] (port) {Setup Port Pins};
\node [block, below of=port] (loop) {Loop Forever};
\path [line] (puc) -- (wdt);
\path [line] (wdt) -- (port);
\path [line] (port) -- (loop);
\path [line] (loop) -- (loop);
\end{tikzpicture}
我想我需要一些更复杂的东西。
答案1
只需更改\path [line] (loop) -- (loop);
为\path [line] (loop) edge[loop right] ();
(或loop below
或loop left
)。
或者,将其更改为\path [line] (loop) |- ($(loop.south east) + (0.5,-0.5)$) |- (loop);
(使用\usetikzlibrary{calc}
)
此外,加上rounded corners
(例如\path [line,rounded corners] (loop) |- ($(loop.south east) + (0.5,-0.5)$) |- (loop);
)可得
答案2
最小示例中的更多细节可能会非常有用,因为有一些 tikZ 库对于编译示例是必要的。
您可以通过向最后一条路径添加一个坐标来获得所需的循环,强制路径通过该点绘制。
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\tikzstyle{decision}=[diamond,draw,fill=blue!20,text width=4.5em,text badly centered,node distance=3cm,inner sep=0pt]
\tikzstyle{block}=[rectangle,draw,fill=blue!20,text width=5em,text badly centered,rounded corners,minimum height=4em]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{terminator}=[draw,ellipse,fill=red!20,node distance=3cm,minimum height=2em]
\begin{document}
\begin{tikzpicture}[node distance=2cm,auto]
\node[terminator] (puc) {Power-Up Reset};
\node[block, below of=puc] (wdt) {Stop Watchdog};
\node[block, below of=wdt] (port) {Setup Port Pins};
\node[block, below of=port] (loop) {Loop Forever};
\path[line] (puc) -- (wdt);
\path[line] (wdt) -- (port);
\path[line] (port) -- (loop);
\path[line] (loop) |- +(2,-1) |- (loop.east);
\end{tikzpicture}
\end{document}