# Flowchart using TiKz - connecting arrows

Here is flowchart drawn using TiKz. I'm trying to connect the arrows in a clean way. For instance, I want to connect box3.west to retF.south and box2.east to retT.south. I would like to have the arrow end at the midpoint of retF.south and retT.south. Is there a general way to do this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows,fit,matrix,positioning,shapes.multipart}
\tikzset
{
process/.style={rectangle, minimum width=2cm, minimum height=1cm, align=center, text width=2cm, draw},
arrow/.style={thick, ->, >=stealth},
decision/.style ={diamond, draw=black, minimum width=1cm, minimum height=1cm, text badly centered, node distance=3cm, inner sep=0pt}
}
\begin{document}
\begin{figure}[htp]
\centering
{
\begin{tikzpicture}[scale=0.5, transform shape]
\node (p0) [] {foo(K)};
\node (p1) [process, below of=p0, text width=4cm] {box1};
\node (p2) [process, below of=p1, yshift=-1.5cm, text width=4.5cm] {box2};
\node (p3) [decision, below of=p2, yshift=-1.5cm, text width=2cm] {box3};
\node (retT) [process, right of=p1, xshift=4cm, text width=1cm, minimum width=1cm] {retT};
\node (retF) [process, left of=p2, xshift=-5cm, text width=1cm, minimum width=1cm] {retF};

\draw [arrow] (p1) -- node[anchor=west] {need more steps} (p2);
\draw [arrow] (p1) -- node[anchor=south] {no more steps} (retT);
\draw [arrow] (p2.east) -- ++(1.5,0) node[anchor=north,pos=1] {K = X}   |- (retT.west);
\draw [arrow] (p2.west) -- node[anchor=north,pos=0.5] {K $<$ X}  (retF.east);
\draw [arrow] (p2) -- node[anchor=east] {K $>$ X} (p3);
\draw [arrow] (p3.west) --+(-4.2,0)  node[anchor=north]{No} |- (retF.south);

\end{tikzpicture}
}
\caption{connect box3.west to retF.south}
\end{figure}
\end{document}

• You can do \draw [arrow] (p3.west) -| (retF.south) node[below, pos=0.5] {No}; Nov 29 '15 at 3:32
• Oh. Now I understand the meaning of |-. I was assuming that as a perpendicular line. But it actually means "go perpendicular first and then go straight". Now I can use -| for my use case. Thanks. Nov 29 '15 at 3:41
• @arunmoezhi it actually depends on where |- is being used. Here it is an L shaped arrow but in (A |- B)  it is a point on a horizontal line through B vertically down/up from A. Nov 29 '15 at 16:03
• Not really perpendicular, it means "go vertically, then horizontally", and vice versa for -|. Try for example \draw (0,0) -- (1,1) |- (0,0);. Unrelated note: The brace pair surrounding the tikzpicture environment is unnecessary I think. Nov 30 '15 at 22:07

## 1 Answer

Just summarizing the comments so this question has an actual answer (it was highlighted as unanswered in a recent newsletter). You want to use the -| path operation to draw an L-shaped path.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows,fit,matrix,positioning,shapes.multipart}
\tikzset
{
process/.style={rectangle, minimum width=2cm, minimum height=1cm, align=center, text width=2cm, draw},
arrow/.style={thick, ->, >=stealth},
decision/.style ={diamond, draw=black, minimum width=1cm, minimum height=1cm, text badly centered, node distance=3cm, inner sep=0pt}
}
\begin{document}
\begin{tikzpicture}[scale=0.5, transform shape]
\node (p0) [] {foo($K$)};
\node (p1) [process, below of=p0, text width=4cm] {box1};
\node (p2) [process, below of=p1, yshift=-1.5cm, text width=4.5cm] {box2};
\node (p3) [decision, below of=p2, yshift=-1.5cm, text width=2cm] {box3};
\node (retT) [process, right of=p1, xshift=4cm, text width=1cm, minimum width=1cm] {retT};
\node (retF) [process, left of=p2, xshift=-5cm, text width=1cm, minimum width=1cm] {retF};

\draw [arrow] (p1) -- node[anchor=west] {need more steps} (p2);
\draw [arrow] (p1) -- node[anchor=south] {no more steps} (retT);
\draw [arrow] (p2.east) -| (retT.south) node[anchor=north,pos=0.5] {$K = X$}  ;
\draw [arrow] (p2.west) -- node[anchor=north,pos=0.5] {$K < X$}  (retF.east);
\draw [arrow] (p2) -- node[anchor=east] {$K > X$} (p3);
\draw [arrow] (p3.west) -| (retF.south);

\end{tikzpicture}
\end{document}


If you want to have multiple L-shaped paths you can do that by adding a coordinate in between. Adding this line:

\draw [arrow] (p3.east) -| ++(1.5cm,1.5cm) node[red] {$\bullet$} -| ([xshift=1cm]p2.south);


will draw an L-shaped path horizontally from p3.east to a point 1.5cm up and to the right. I put the red bullet there just to illustrate it; it's not necessary for your final diagram. Then the path goes to a point 1cm to the right of p2.south.

You can fiddle with the positioning of that intermediate node any way you like.

• Thanks for the answer. If I were to connnect box3 back to box2, I usually use squiggly line. But if I were to use straight lines, how do I do that. For example I want to go from box3.east to box2.south. I would do -| but I want to connect to right of box2.south. Can I go horizontal then vertical, then horizontal and then vertical again? Dec 2 '15 at 15:00
• @arunmoezhi: Yes, you can do that if you put a coordinate in between. But I'm not sure what you mean by "right of box2.south". Is that the same as box2.south east? Or do you mean a point between box2.south and box2.south east? Dec 2 '15 at 15:04
• i meant 1cm right of the midpoint of box2.south Dec 2 '15 at 15:05
• @arunmoezhi: see edit. If you have questions about other techniques it's probably better to start a new question (the one you originally asked has been answered). Dec 2 '15 at 15:32