流程图几乎就是我想要的,但我在让箭头在其中一条有转弯的路径上指向正确方向时遇到了问题。图片显示了这个问题,我用来生成的代码看起来不错,但就是不起作用。
我做错了什么?我该如何修复?我想要真正理解修复方法,而不仅仅是修复这个流程图,因为我必须为手册排版许多流程图。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,arrows.meta}
\tikzstyle{startstop} = [draw, rounded rectangle, text centered, draw=black,thick]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, text centered]
\tikzstyle{process} = [rectangle, text centered, draw=black,thick]
\tikzstyle{decision} = [diamond, text centered, draw=black,thick]
\tikzstyle{arrow} = [-{Stealth[scale=1.2]},rounded corners,thick]
\begin{document}
\begin{tikzpicture}
\node (start) [startstop] {Start};
\node (io1) [io,below=0.5 of start] {Read $R$};
\node (box1) [process,below=0.5 of io1] {$X \gets 0$};
\node (branch1) [decision,aspect=2,below=0.7 of box1] {$X > R-1$};
\node (return) [startstop,left=1 of branch1] {Return};
\node (box2) [process,below=0.7 of branch1] {$C \gets 0$};
\node (branch2) [decision,below=0.7 of box2] {$C > 2$};
\node (box3) [process,right=1 of branch2] {$X \gets X+1$};
\node (io2) [io,below=0.7 of branch2] {Read $A_{X,C}$};
\node (box4) [process,below=0.7 of io2] {$C \gets C+1$};
\draw [arrow] (start) -- (io1);
\draw [arrow] (io1) -- (box1);
\draw [arrow] (box1) -- coordinate[midway](m1)(branch1);
\draw [arrow] (branch1) -- coordinate[pos=0.4](m3)(box2);
\node [black,right=0.1 of m3] {False};
\draw [arrow] (branch1) -- coordinate[pos=0.4](m4)(return);
\node [black,above=0.1 of m4] {True};
\draw [arrow] (box2) -- coordinate[midway](m2)(branch2);
\draw [arrow] (branch2) -- coordinate[pos=0.4](m5)(io2);
\node [black,right=0.1 of m5] {False};
\draw [arrow] (branch2) -- coordinate[pos=0.4](m6)(box3);
\node [black,above=0.1 of m6] {True};
\draw [arrow] (io2) -- (box4);
\draw [arrow] (box4) |- ++(0,-1) -| ++(-2.5,0) |- ([xshift=-2.5]m2) -- (m2.west);
\draw [arrow] (box3) |- (m1);
\end{tikzpicture}
\end{document}
答案1
代替
\draw [arrow] (box4) |- ++(0,-1) -| ++(-2.5,0) |- ([xshift=-2.5]m2) -- (m2.west);
使用
\draw [arrow] (box4) |- + (-2.5,-1) |- (m2);
|-
箭头方向错误的原因是箭头路径使用不正确。
升级: 完整代码(已更正错误并简化)如下:
\documentclass[border=5mm,tikz,preview]{standalone}
\usetikzlibrary{arrows, arrows.meta,
chains,
positioning,
shapes}
\makeatletter
\tikzset{reset join/.code={\def\tikz@after@path{}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
node distance= 7mm and 13 mm,
start chain = going below,
base/.style = {draw, thick, align=center,
inner ysep=1mm, inner xsep=2mm,
join=by arrow, on chain},
startstop/.style = {base, rounded rectangle},
io/.style = {trapezium, base,
trapezium left angle=70, trapezium right angle=110},
process/.style = {base},
decision/.style = {diamond, aspect=1.5, base, inner xsep=0pt},
arrow/.style = {-{Stealth[scale=1.2]}, rounded corners, thick}
]
% main
\node (start) [startstop] {Start};
\node (io1) [io] {Read $R$};
\node (box1) [process] {$X \gets 0$};
\node (branch1) [decision] {$X > R-1$};
\node (box2) [process] {$C \gets 0$};
\node (branch2) [decision] {$C > 2$};
\node (io2) [io] {Read $A_{X,C}$};
\node (box4) [process,below=0.7 of io2] {$C \gets C+1$};
% left and right
\node (return) [startstop,left=1 of branch1,reset join] {Return};
\node (box3) [process, right=1 of branch2,reset join] {$X \gets X+1$};
% coordinates
\node (return) [startstop,left=of branch1,reset join] {Return};
\node (box3) [process, right=of branch2,reset join] {$X \gets X+1$};
\draw [arrow] (box1) -- coordinate[midway](m1)(branch1);
\draw [arrow] (box2) -- coordinate[midway](m2)(branch2);
\draw[arrow] (branch2.east) node[above right] {True} -- (box3);
%
\draw[arrow] (branch1.west) node[above left] {True} -- (return);
\node[below right] at (branch1.south) {False};
%
\draw[arrow] (box3) |- (m1);
\draw[arrow] (box4) |- + (-2.5,-1) |- (m2);
\end{tikzpicture}
\end{document}
它给:
代码主要变化:
- 节点之间的连接是通过样式
join
中的参数完成的base
。 - 对于不需要使用
join
参数的两个节点,设置在“主”节点的末尾,并使用reset join
MWE 序言中定义的宏 - 替换过时的
\tikzstyle{ ...}
(现在建议\tikzset{...}
使用作为选项的样式)tikzpicture
答案2
- 我只是消掉了
([xshift=-2.5]m2) --
,因为|-
应该到达最后一点,(m2)
。 - 无论如何我认为
\tikzstyle{io}
这是不完整的。编辑:选项draw=black,thick
缺失。 - 此外我还必须加载
petri
库。
MWE 如下。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,arrows.meta,petri}
\tikzstyle{startstop} = [draw, rounded rectangle, text centered, draw=black,thick]
\tikzstyle{io} = [draw=black,thick,trapezium, trapezium left angle=70, trapezium right angle=110,text centered]
\tikzstyle{process} = [rectangle, text centered, draw=black,thick]
\tikzstyle{decision} = [diamond, text centered, draw=black,thick]
\tikzstyle{arrow} = [-{Stealth[scale=1.2]},rounded corners,thick]
\tikzstyle{arrow2} = [{Stealth[scale=1.2]}-,rounded corners,thick]
\begin{document}
\begin{tikzpicture}
\node (start) [startstop] {Start};
\node (io1) [io,below=0.5 of start] {Read $R$};
\node (box1) [process,below=0.5 of io1] {$X \gets 0$};
\node (branch1) [decision,aspect=2,below=0.7 of box1] {$X > R-1$};
\node (return) [startstop,left=1 of branch1] {Return};
\node (box2) [process,below=0.7 of branch1] {$C \gets 0$};
\node (branch2) [decision,below=0.7 of box2] {$C > 2$};
\node (box3) [process,right=1 of branch2] {$X \gets X+1$};
\node (io2) [io,below=0.7 of branch2] {Read $A_{X,C}$};
\node (box4) [process,below=0.7 of io2] {$C \gets C+1$};
\draw [arrow] (start) -- (io1);
\draw [arrow] (io1) -- (box1);
\draw [arrow] (box1) -- coordinate[midway](m1)(branch1);
\draw [arrow] (branch1) -- coordinate[pos=0.4](m3)(box2);
\node [black,right=0.1 of m3] {False};
\draw [arrow] (branch1) -- coordinate[pos=0.4](m4)(return);
\node [black,above=0.1 of m4] {True};
\draw [arrow] (box2) -- coordinate[midway](m2)(branch2);
\draw [arrow] (branch2) -- coordinate[pos=0.4](m5)(io2);
\node [black,right=0.1 of m5] {False};
\draw [arrow] (branch2) -- coordinate[pos=0.4](m6)(box3);
\node [black,above=0.1 of m6] {True};
\draw [arrow] (io2) -- (box4);
\draw [arrow] (box4) |- ++(0,-1) -| ++(-2.5,0) |- (m2);
\draw [arrow] (box3) |- (m1);
\end{tikzpicture}
\end{document}