我得到了以下 TikZ 图:
\begin{tikzpicture}[auto,node distance=2cm,thick,
node/.style={shape = rectangle,draw}]
\node[node] (en) {ENTRY};
\node[node, label=180:$B_1$:] (1) [below of=en]{
\begin{lstlisting}
p = 0.0
i = 0
\end{lstlisting}
};
\node[node, label=180:$B_2$:] (2) [below of=1]{
\begin{lstlisting}
if n<=i goto EXIT
\end{lstlisting}
};
\node[node, label=180:$B_3$:] (3) [below of=2]{
\begin{lstlisting}[mathescape]
i = i + 1
$t_1$ = a[i]
$t_2$ = b[i]
$t_3$ = $t_2$ * $t_3$
p = p + $t_3$
\end{lstlisting}
};
\node[node, label=180:$B_4$:, anchor=north east] (4) [below of=3]{
\begin{lstlisting}[mathescape]
goto $B_2$
\end{lstlisting}
};
\node[node] (ex) [below of = 4] {EXIT};
\path (en) edge (1);
\path (1) edge (2);
\path (2) edge (3);
\path (3) edge (4);
\path (4) edge (ex);
\path (4) edge [bend right] (2);
\end{tikzpicture}
问题在于从节点 (4) 到节点 (2) 的边正好穿过节点 (3)。我很想为边定义某种锚点,例如节点 (4) 的右上角和节点 (2) 的右下角。我发现可以设置一个名为“锚点”的属性,但这样做不会导致任何变化。
如果这是重复的,我很抱歉(因为这似乎是一个相当常见的问题)但我的搜索没有得到任何有用的结果。
答案1
和
\draw (4.east) -- ++(2cm,0) |- (2);
你得到
和
\draw (4.20) to [bend right,looseness=1.5] (2.350);
你得到
与
\draw (4.east) to[bend right] (2.east);
\documentclass{article}
\usepackage{tikz,listings}
\begin{document}
\begin{tikzpicture}[auto,node distance=2cm,thick,
node/.style={shape = rectangle,draw}]
\node[node] (en) {ENTRY};
\node[node, label=180:$B_1$:] (1) [below of=en]{
\begin{lstlisting}
p = 0.0
i = 0
\end{lstlisting}
};
\node[node, label=180:$B_2$:] (2) [below of=1]{
\begin{lstlisting}
if n<=i goto EXIT
\end{lstlisting}
};
\node[node, label=180:$B_3$:] (3) [below of=2]{
\begin{lstlisting}[mathescape]
i = i + 1
$t_1$ = a[i]
$t_2$ = b[i]
$t_3$ = $t_2$ * $t_3$
p = p + $t_3$
\end{lstlisting}
};
\node[node, label=180:$B_4$:, anchor=north east] (4) [below of=3]{
\begin{lstlisting}[mathescape]
goto $B_2$
\end{lstlisting}
};
\node[node] (ex) [below of = 4] {EXIT};
\path (en) edge (1);
\path (1) edge (2);
\path (2) edge (3);
\path (3) edge (4);
\path (4) edge (ex);
\draw (4.east) -- ++(2cm,0) |- (2);
%\draw (4.20) to [bend right,looseness=1.5] (2.350);
%\draw (4.east) to[bend right] (2.east);
\end{tikzpicture}
\end{document}
答案2
您可以直接在节点 ID 中指定要使用的锚点,如下所示(例如):
\path (4.east) edge [bend right] (2.south east);
参见第 17.11 节(第 246 页)pgf 手册,标题为使用节点作为坐标。