[Tex/LaTex] Flowchart Tikzpicture – Draw arrow from path

tikz-arrowstikz-pgf

I am creating a flowchart using the tikzpicture package.

Because I am short on space, I would like to draw an arrow from a path to a box.

Using this code below, I would like to have an arrow that start from the path between Kmeans and KGaussians and that would loop-back to Kmeans.

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=gray!20, 
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=gray!20, 
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [block] (init) {User input};
\node [block, below of=init] (denoise) {denoise};
\node [block, below of=denoise] (Kmeans) {K means};
\node [block, below of=Kmeans] (KGaussians) {K Gaussians};
\node [block, below of=KGaussians] (output) {Output};     
% Draw edges
\path [line] (init) -- (denoise);
\path [line] (denoise) -- (Kmeans);
\path [line] (Kmeans) -- (KGaussians);
\path [line] (KGaussians) -- (output);
\end{tikzpicture}

\end{document}

I am thinking of adding a command line that would look like:

\draw [arrow] (\path (Kmeans) -- (KGaussians)) |- (Kmeans);

Thanks!

Best Answer

Without the calc library (suggested in comments), you can place an auxiliary coordinate midways in the path between Kmeans and KGaussians:

\path [line] (Kmeans) -- coordinate (aux) (KGaussians);

and then use this coordinate to draw the desired path, as in

\path [line] (aux) -- ++(-1.5,0) |- (Kmeans.west);

A complete example:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=gray!20, 
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=gray!20, 
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [block] (init) {User input};
\node [block, below of=init] (denoise) {denoise};
\node [block, below of=denoise] (Kmeans) {K means};
\node [block, below of=Kmeans] (KGaussians) {K Gaussians};
\node [block, below of=KGaussians] (output) {Output};     
% Draw edges
\path [line] (init) -- (denoise);
\path [line] (denoise) -- (Kmeans);
\path [line] (Kmeans) -- coordinate (aux) (KGaussians);
\path [line] (KGaussians) -- (output);
\path [line] (aux) -- ++(-1.5,0) |- (Kmeans.west);
\end{tikzpicture}

\end{document}

enter image description here