3

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!

  • 1
    Welcome to TeX.SX! Please always post a compilable minimal working example. Your current code does not compile, as we don't know how the keys block, line etc are defined. – Herr K. Feb 27 '15 at 21:17
  • Thanks, I edited the code such that it should be easy to run & compile. – Alex-Antoine Fortin Feb 27 '15 at 21:26
  • Like this: \draw[-latex']($(Kmeans)!.5!(KGaussians)$)--+(1.5,0)|-(Kmeans.east);? You need to add calc to the list of \usetikzlibrary for this to work. – Herr K. Feb 27 '15 at 21:32
  • woohoo, it gives the desired effect! (Note: you can replace 1.5 by -1.5 if you'd prefer the arrow to go to the left instead of to the right) Thanks! – Alex-Antoine Fortin Feb 27 '15 at 21:50
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.