Creating a Flowchart with TikZ and LaTeX

In this post I’ll discuss how to make simple flowcharts in LaTeX using TikZ. Probably the best collection of TikZ examples can be found at TeXample.net, but there are other helpful examples like these two PDFs, here and here. In case you’re wondering, TikZ is a recursive acronym “TikZ ist kein Zeichenprogramm,” a reminder (in German) that it is not an interactive drawing program.

I assume that you have LaTeX up and running. If you’re using a Mac and you’re a little stuck on on how to get started, I have a tutorial here covering that process.

First we define some stuff in the header of the document, namely that we are writing a report (not important) and that we’re using the tikz package, and the shapes and arrows libraries from tikz.

\documentclass[11pt]{report}

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

Then we begin the document and define some shapes. We’ll have a decision block that is a diamond shape with aspect=2, meaning that it is somewhat squashed. We’ll also have a rectangular block called block with rounded corners.

\begin{document}

\pagestyle{empty}

\tikzstyle{decision} = [ diamond, aspect=2, draw, fill=blue!20, text width=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' ]

Finally, we begin the TikZ picture with the nodes in one block of code, and then the edges in the next block of code. The general format for the \node objects is the following:

  • In square braces, state the type of node, and its position relative to previous named nodes
  • In round braces, state the name of the node, for internal use
  • In curly braces, provide the text to be printed on output

So, the first line is a block named (internally) init, that contains the text “start here”. The next line is a decision (a diamond shape) that is placed below the block named `init, containing the text “start here”, and it says, “decision-1?”. And so on and so forth.

\begin{tikzpicture}[node distance=3.5cm, auto]

% place nodes
\node [block] (init) {start here} ;
\node [decision, below of=init] (decision-1) {decision-1?} ;
\node [block, right of=decision-1] (block-1) {block-1} ;
\node [block, below of=decision-1] (block-2) {block-2} ;
\node [block, right of=block-2] (block-3) {block-3} ;
\node [decision, below of=block-3] (decision-2) {decision-2?} ;
\node [block, right of=decision-2] (block-4) {block-4} ;
\node [decision, below of=decision-2] (decision-3) {decision-3?} ;
\node [block, left of=decision-3] (block-6) {block-6} ;
\node [block, right of=decision-3] (block-5) {block-5} ;
\node [block, below of=block-6] (block-7) {block-7} ;
\node [block, right of=block-7] (block-8) {block-8} ;
\node [block, right of=block-8] (block-9) {block-9} ;

After the block content and orientation definitions, we define how the blocks are connected. This is pretty simple. In the first line we connect (init) to (decision-1) in a directed manner with an arrow denoted by --. Adding a yes or no to an arrow is achieved with a node {yes} or node {no} bit.

% draw edges
\path [line] (init) -- (decision-1) ;
\path [line] (decision-1) -- node {yes}(block-1) ;
\path [line] (decision-1) -- node {no}(block-2) ;
\path [line] (block-2) -- (block-3) ;
\path [line] (block-3) -- (decision-2) ;
\path [line] (decision-2) -- node {no}(block-4) ;
\path [line] (decision-2) -- node {yes}(decision-3) ;
\path [line] (decision-3) -- node {yes}(block-6) ;
\path [line] (decision-3) -- node {no}(block-5) ;
\path [line] (block-6) -- (block-7) ;
\path [line] (block-7) -- (block-8) ;
\path [line] (block-8) -- (block-9) ;

\end{tikzpicture}

\end{document}

Now we can compile the .tex file with the pdflatex utility to produce a .pdf document with one of the following command line calls. If you’re on a Mac, then you can open the .pdf with the open utility.

pdflatex flowchart.tex && open flowchart.pdf

If you’re using some flavor of Debian Linux, you can probably call

pdflatex flowchart.tex && evince flowchart.pdf

The && only executes the open or evince utilities if the pdflatex call finishes successfully with error code 0. (For zero errors.) Anyway, this should produce the following figure.