将标签放置在靠近 tikz 流程图中特定框的位置

将标签放置在靠近 tikz 流程图中特定框的位置

我需要将标签放置No在更靠近盒子的位置check if flag set。我应该使用什么选项。

\documentclass{sig-alternate}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\tikzstyle{process} = [rectangle, minimum width=2cm, minimum height=1cm, text centered, text width=2cm, draw=black]
\tikzstyle{connector} = [circle, minimum width=1cm, minimum height=0.5cm, text centered, text width=1cm, draw=black]
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{document}
\begin{tikzpicture}
\node (p0) [] {foo(K)};
\node (p1) [process, below of=p0, text width=3cm] {search for key K};
\node (p2) [process, below of=p1, yshift=-1cm, text width=3.5cm] {Create key K for insertion};
\node (p3) [process, below of=p2, yshift=-1cm, text width=3.5cm] {Attempt to insert};
\node (retF) [process, right of=p1, xshift=3cm, text width=1cm, minimum width=1cm] {return false};
\node (p4) [process, below of=p3, yshift=-1cm, text width=3cm] {check if flag set};
\node (retT) [process, right of=p3, xshift=3cm, text width=1cm, minimum width=1cm] {return true};
\node (h1) [connector, below of=p4, yshift=-1cm] {bar()};



\draw [arrow] (p1) -- node[anchor=west] {K not found} (p2);
\draw [arrow] (p1) -- node[anchor=south] {K found} (retF);
\draw [arrow] (p2) -- node {} (p3);
\draw [arrow] (p3) -- node[anchor=east] {failed} (p4);
\draw [arrow] (p3) -- node[anchor=south] {successful} (retT);
\draw [arrow] (p4) -- node[anchor=west] {Yes} (h1);
\draw [arrow] (p4.west) -- ++(-1,0) |- node[anchor=south] {No} (p1.west);
\draw [arrow] (h1.west) -- ++(-2.5,0) |- node[anchor=south] {} (p1.west);
\end{tikzpicture}
\end{document}

输出

答案1

你可以使用类似

\draw [arrow] (p4.west) -- ++(-1,0) node[anchor=south,pos=0.5] {No} |- (p1.west);

完整示例:

\documentclass{sig-alternate}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows,positioning}

\tikzset{ 
process/.style={
  rectangle, 
  minimum width=2cm, 
  minimum height=1cm, 
  align=center, 
  text width=2cm,
  draw
  },
connector/.style={
  circle, 
  minimum width=1cm, 
  minimum height=0.5cm, 
  align=center, 
  text width=1cm, 
  draw
},
arrow/.style={
  thick,
  ->,
  >=stealth
}
}  

\begin{document}

\begin{tikzpicture}[
node distance=1cm and 2cm
]
\node (p0) [] {foo(K)};
\node (p1) [process, below = 0.2cm of p0, text width=3cm] {search for key K};
\node (p2) [process, below =of p1, text width=3.5cm] {Create key K for insertion};
\node (p3) [process, below =of p2,text width=3.5cm] {Attempt to insert};
\node (retF) [process, right =of p1,, text width=1cm, minimum width=1cm] {return false};
\node (p4) [process, below =of p3, text width=3cm] {check if flag set};
\node (retT) [process, right =of p3,text width=1cm, minimum width=1cm] {return true};
\node (h1) [connector, below =of p4] {bar()};

\draw [arrow] (p1) -- node[anchor=west] {K not found} (p2);
\draw [arrow] (p1) -- node[anchor=south] {K found} (retF);
\draw [arrow] (p2) -- node {} (p3);
\draw [arrow] (p3) -- node[anchor=east] {failed} (p4);
\draw [arrow] (p3) -- node[anchor=south] {successful} (retT);
\draw [arrow] (p4) -- node[anchor=west] {Yes} (h1);
\draw [arrow] (p4.west) -- ++(-1,0) node[anchor=south,pos=0.5] {No} |- (p1.west);
\draw [arrow] (h1.west) -- ++(-2.5,0) |- node[anchor=south] {} (p1.west);
\end{tikzpicture}

\end{document}

在此处输入图片描述

请注意,我改为\tikzstyle\tikzset参见应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?),并且我还使用定位库of=将有问题的语法改为(参见)。=of

相关内容