Example: Simple flow chart

Published 2006-11-20 | Author: Kjell Magne Fauske

With PGF/TikZ you can draw flow charts with relative ease. This flow chart from [1] outlines an algorithm for identifying the parameters of an autonomous underwater vehicle model.

Note that relative node placement has been used to avoid placing nodes explicitly. This feature was introduced in PGF/TikZ >= 1.09.

 [1] Bossley, K.; Brown, M. & Harris, C. Neurofuzzy identification of an autonomous underwater vehicle International Journal of Systems Science, 1999, 30, 901-913

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}

% Define block styles
\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 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) {initialize model};
\node [cloud, left of=init] (expert) {expert};
\node [cloud, right of=init] (system) {system};
\node [block, below of=init] (identify) {identify candidate models};
\node [block, below of=identify] (evaluate) {evaluate candidate models};
\node [block, left of=evaluate, node distance=3cm] (update) {update model};
\node [decision, below of=evaluate] (decide) {is best candidate better?};
\node [block, below of=decide, node distance=3cm] (stop) {stop};
% Draw edges
\path [line] (init) -- (identify);
\path [line] (identify) -- (evaluate);
\path [line] (evaluate) -- (decide);
\path [line] (decide) -| node [near start] {yes} (update);
\path [line] (update) |- (identify);
\path [line] (decide) -- node {no}(stop);
\path [line,dashed] (expert) -- (init);
\path [line,dashed] (system) -- (init);
\path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}

\end{document}


• #1 Blithe, January 12, 2009 at 11:17 p.m.

A little improvement

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

\begin{tikzpicture}[scale=2, node distance = 2cm, auto]
% Place nodes
\node [block] (init) {initialize model};
\node [cloud, left of=init] (expert) {expert};
\node [cloud, right of=init] (system) {system};
\node [block, below of=init] (identify) {identify candidate models};
\node [block, below of=identify] (evaluate) {evaluate candidate models};
\node [block, left of=evaluate, node distance=2.5cm] (update) {update model};
\node [decision, below of=evaluate] (decide) {is best candidate better?};
\node [block, below of=decide, node distance=2.5cm] (stop) {stop};
% Draw edges
\path [line] (init) -- (identify);
\path [line] (identify) -- (evaluate);
\path [line] (evaluate) -- (decide);
\path [line] (decide) -| node [near start, color=black] {yes} (update);
\path [line] (update) |- (identify);
\path [line] (decide) -- node [, color=black] {no}(stop);
\path [line,dashed] (expert) -- (init);
\path [line,dashed] (system) -- (init);
\path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}

• #2 sintayhu, February 17, 2009 at 2:21 a.m.

i read it so it is intersting

• #3 Matthew Leingang, March 25, 2009 at 2:29 p.m.

Very helpful, thanks! It seems the code for formatting the yes/no nodes can also be styled:

\tikzstyle{decision answer}=[near start,color=black]
...
\path[line] (decide) -| node[decision answer] {yes} (update);


You can also use anchors like decide.east if you want to force the arrows to come out of the points of the decision rhombus. This isn't needed here but if you want a diagonal line coming out of the decision node it will by default come from the center and emerge from one of the sides.

• #4 Kjell Magne Fauske, March 25, 2009 at 2:41 p.m.

In the PGF >= 2.0 manual there is a version of this flow chart that uses a tikz matrix to place the blocks. Requires less typing than my version.

• #5 Matthew Leingang, March 25, 2009 at 5:46 p.m.

I have the 2.0 manual and I didn't catch it, which is why I turned to google and found this page. Guess I'll look harder.

• #6 Kjell Magne Fauske, March 26, 2009 at 8:46 a.m.

Section "16.6 Examples". Page 181 in the current CTAN version of the manual (PGF 2.0).

• #7 Matthew Leingang, March 26, 2009 at 2:15 p.m.

Found it, thanks!

• #8 LeSpocky, May 24, 2009 at 6:53 p.m.

Meanwhile this section has another number, so reference by name would be easier. ;-)

It's part III, chapter »Matrices and Alignment«, section »Examples«. 8-)

• #9 Matze, August 18, 2009 at 11:15 a.m.

How do I do an other box around system, initialize model and expert? I need this for modified flow chart.

• #10 marla, October 14, 2009 at 3 a.m.

I want To learn how to make a flowchart easily!!

• #11 John Chain, January 7, 2010 at 9:33 p.m.

Drop the semicolon for avoiding the semicolon at the top of the picture.

\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm, minimum height=2em];

• #12 Kjell Magne Fauske, January 11, 2010 at 7:56 p.m.

@John Chain: Fixed.

• #13 Jan Verbesselt, June 8, 2010 at 2:26 p.m.

Would it be possible to include a figure (e.g. eps from R software) within a block so that it can be included in the flow chart?

Thanks, Jan

• #14 Kjell Magne Fauske, June 9, 2010 at 7:48 a.m.

@Jan: You can put nearly anything inside a node, including an eps or pdf:

...
\node [block] (init) {\includegraphics{figure}};
...

• #15 Travis, July 24, 2010 at 3:42 p.m.

• #16 emad, August 10, 2010 at 2:15 p.m.

If the "update" block is removed ..how to connect the path between the decision block and the "identify" block

• #17 James, March 23, 2012 at 7:05 p.m.

does anyone know if it possible to number the boxes in say the top right hand corner?

• #18 Jorge, March 26, 2012 at 12:07 a.m.

How can i make a flow a simple system with realimentation, that the line derive by another line and not by a box, for example? Thanks.

• #19 Wolfson, April 11, 2012 at 9:58 a.m.

Thanks for this example!

One question for adapting it to my needs: How can I keep the edge from EVALUATE to INIT without having the node SYSTEM? I tried for example "|-|" but it won't work :(

• #20 collen, April 28, 2012 at 9:24 a.m.

good!!!

• #21 Zahid, May 4, 2012 at 4:41 p.m.

I have two questions: 1. How can I put a title of the flow chart, under the flow chart? 2. How can I put a block in the MIDDLE of two blocks, but below the both of blocks?

Thanks!

• #22 Tatill, May 16, 2012 at 9:34 a.m.

Zahid >

1) using \begin{figure} \caption{Figure} \end{figure}

2) using "right below" or "left below" ?

• #23 Zahid, June 24, 2012 at 11:28 a.m.

Thanks Tatill for your response, I now could put caption but couldn't use right below or left below commands.

• #24 Allen Joseh, September 27, 2012 at 11:37 a.m.

Hi guys Im finding on my Miktex that the flow chart is not centred do you know how i can make it stay in the middle of my PDF like you have on yours?

• #25 Allen Joseh, September 27, 2012 at 11:38 a.m.

Sorry I mean Texworks

• #26 Elton, February 9, 2013 at 3:34 a.m.

24 @Allen Joseph.

To make it stay in center use something like this:

\documentclass{article} \usepackage[a4paper,margin=2.0cm]{geometry}

To make it a standalone picture (or pdf) with no borders (i.e. size automatically set to picture), use the standalone package instead:

\documentclass[tikz]{standalone}

Check page 7 from this manual for more: http://ctan.mirror.rafal.ca/macros/latex/contrib/standalone/standalone.pdf

• #27 Aijaz, February 25, 2013 at 11:37 a.m.

I have to draw a stencil with two parallel lines and one perpendicular line cutting them in the middle and I also want to name their nodes. but I am not able to do this so please help me.

• #28 thomgraf, September 19, 2013 at 5:06 p.m.

How can I move the "no" vertically?

• #29 Neal, October 3, 2013 at 1:37 p.m.

I tried the example above with MIKTEX on Win7, and it compiled for a while, but them stopped with:

ABD: EveryShipout initializing macros) *

It seemed to be waiting for input from me at that point. Any suggestions?

Thanks Neal

• #30 T. Kook, October 4, 2013 at 7:28 a.m.

How can you bend the arrows so that it doesn't go directly from one node to the other?

• #31 Neal, October 4, 2013 at 4:46 p.m.

I would like to allow other users to drop lines of text into the blocks of a pre-constructed flowchart like this. I know the "box and arrow" skeleton of the flowchart, so to speak, but not the number of text lines in any particular block. (Every block will have at least one text line.) In the code given here, if I put too many text lines in block "A", that block will expand so as to clobber blocks below it. Is there a way to modify this script such that blocks and arrows below "A" will automatically "move down" to accomodate many lines of text in "A"?

• #32 Neal, October 4, 2013 at 11:37 p.m.

Please ignore my previous two questions. BTW, I would have found a simple example of a flowchart that uses the \matrix command to be very useful. Most currently-available examples are pretty complex, at least for me.