Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm working on creating flowcharts directly inside LaTeX documents using tikz. I am admittedly an extreme novice with tikz, although I have quite a bit of experience using LaTeX.

My problem is when trying to show an endless loop with the flowchart, i.e., an edge or path that starts and ends at the same node.

For example,

this image

is an example of what I want (right) and what I'm getting (left) using the following LaTeX code:

\usetikzlibrary{shapes.geometric,arrows}

\tikzstyle{decision} = [ diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep-0pt]  
\tikzstyle{block} = [ rectangle, draw, fill=blue!20, text width=5em, text badly centered, rounded corners, minimum height=4em]  
\tikzstyle{line} = [ draw, -latex']  
\tikzstyle{terminator} = [ draw, ellipse, fill=red!20, node distance=3cm, minimum height=2em]  

\begin{tikzpicture}[node distance=2cm, auto]  
  \node [terminator]           (puc)  {Power-Up Reset};  
  \node [block, below of=puc]  (wdt)  {Stop Watchdog};  
  \node [block, below of=wdt]  (port) {Setup Port Pins};  
  \node [block, below of=port] (loop) {Loop Forever};  
  \path [line] (puc)  -- (wdt);  
  \path [line] (wdt)  -- (port);  
  \path [line] (port) -- (loop);  
  \path [line] (loop) -- (loop);  
\end{tikzpicture}

I assume I'm going to need something a bit more complex.

share|improve this question
    
@Thorsten: My editing interfered with your editing. –  lockstep Nov 8 '10 at 21:01
    
@Zanfar: I put the image into the post for ya'. Feel free to rollback if you don't like it. –  Yossi Farjoun Nov 8 '10 at 21:43
    
@Yossi: you should be aware that doing that isn't always helpful. If the questioner doesn't have enough rep to do that themselves, then putting it in means that if they edit the question, they have to remove the image. –  Loop Space Nov 9 '10 at 9:00
    
hmmm. I did realize that. @Zanfar: if you want to edit your question, let me know and I'll re-add the image after your edits, if you want. –  Yossi Farjoun Nov 9 '10 at 10:38
    
@Andrew, @Yossi: the “remove new user restrictions” is (currently) at 10 rep, so after a question is upvoted two times, there is no problem with adding a picture. –  Caramdir Nov 9 '10 at 16:16

2 Answers 2

up vote 18 down vote accepted

Simply change \path [line] (loop) -- (loop); to \path [line] (loop) edge[loop right] (); (or loop below or loop left).

example 1

Alternatively, change it to \path [line] (loop) |- ($(loop.south east) + (0.5,-0.5)$) |- (loop); (with \usetikzlibrary{calc}) for

example 2

Further, adding rounded corners (e.g. \path [line,rounded corners] (loop) |- ($(loop.south east) + (0.5,-0.5)$) |- (loop);) gives

example 3

share|improve this answer
    
What happens if you do (loop.south) to[out=-90,in=0] (loop.east)? (Nice answer, btw) –  Loop Space Nov 8 '10 at 21:23
    
Thanks, all those options work perfectly. Andrew: <code>(loop.south) to[out=-90,in=0] (loop.east)</code> cuts across the lower-right corner of the box. –  Zanfar Nov 8 '10 at 21:33
    
This happens. (@Zanfar: use backticks ` for code in comments) –  Caramdir Nov 8 '10 at 21:37
    
I tried adding looseness=3 which means that it doesn't cut across the box, but it still looks pretty ugly. I was trying to avoid having to specify a third coordinate. –  Loop Space Nov 9 '10 at 9:05

A few more details in for of a minimal example could have been very helpful because there are some tikZ libraries necessary to compile your example.

You can get the desired loop can by adding a coordinate to the last path that forces the path to be drawn through this point.

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}

\tikzstyle{decision}=[diamond,draw,fill=blue!20,text width=4.5em,text badly centered,node distance=3cm,inner sep=0pt]  
\tikzstyle{block}=[rectangle,draw,fill=blue!20,text width=5em,text badly centered,rounded corners,minimum height=4em]  
\tikzstyle{line}=[draw,-latex']  
\tikzstyle{terminator}=[draw,ellipse,fill=red!20,node distance=3cm,minimum height=2em]  

\begin{document}
  \begin{tikzpicture}[node distance=2cm,auto]  
    \node[terminator]           (puc)  {Power-Up Reset};  
    \node[block, below of=puc]  (wdt)  {Stop Watchdog};  
    \node[block, below of=wdt]  (port) {Setup Port Pins};  
    \node[block, below of=port] (loop) {Loop Forever};  
    \path[line] (puc)  -- (wdt);  
    \path[line] (wdt)  -- (port);  
    \path[line] (port) -- (loop);  
    \path[line] (loop) |- +(2,-1) |- (loop.east);  
  \end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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