[Tex/LaTex] Flowchart using TiKz – connecting arrows

flow chartstikz-arrowstikz-pgf

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?

enter image description here

\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}

Best 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}

sample output

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.

second code output

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