About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Latest news 2019-11-04: The giveaway of two signed copies of “Quack, quack, quack. Give my hat back!” has closed and the winning entrants have been selected. Thank you to everyone who took part.


12.1 Flow Charts

The diagram-flow topic lists several packages for flow and similar diagrams, only two of which are available on both MiKTeX and TeX Live, and only one of these is for flow charts, and that's the flowchart package, which requires the makeshape and tikz packages. Alternatively, you can just use the tikz package directly.

The tikz package [102] has already been briefly introduced in Sections 7.5, 10.3 and 11.1. Drawing a flow chart will also require the tikz libraries2 arrows.meta and shapes.geometric, which can be loaded in the preamble using:

\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes.geometric}

The positioning library is also useful as it provides convenient ways of positioning nodes:

\usetikzlibrary{positioning}

Recall from §7.5 Displaying a Calendar, that within the tikzpicture environment, you can use

\path[path options] (⟨position⟩) node[node options] (⟨node name⟩) {text};

to position a node or you can use the shortcut:

\node[node options] (⟨node name⟩) {text};

The position can be specified within [node options] using at=(⟨position⟩) or using the at keyword:

\node[node options] at (⟨position⟩) (⟨node name⟩) {text};

Alternatively, if you use the positioning library, the node can be placed relative to another node using one of the following ⟨key⟩=⟨value⟩ options:

above
Place this node above the location specified in the ⟨value⟩.
below
Place this node below the location specified in the ⟨value⟩.
left
Place this node to the left of the location specified in the ⟨value⟩.
right
Place this node to the right of the location specified in the ⟨value⟩.
above left
Place this node above left of the location specified in the ⟨value⟩.
above right
Place this node above right of the location specified in the ⟨value⟩.
below left
Place this node below left of the location specified in the ⟨value⟩.
below right
Place this node below right of the location specified in the ⟨value⟩.

For each of these options, the ⟨value⟩ part may simply be in the form ⟨shift⟩ or in the form shift⟩ of ⟨label, where shift may be a dimension (or an expression that evaluates to a dimension) or a number (in which case the unit is the tikz unit currently in use). If of ⟨label is present then the shift is relative to the node identified by ⟨label⟩. If the ⟨shift⟩ part is omitted, the default node distance is used. For further details, and for details of other placement options, see the pgf manual [102].

Example:

\begin{tikzpicture}
\node (start) {Ray-gun doesn't work};
\node[below=of start] (query) {Is it charged?};
\node[right=of query] (recharge) {Recharge battery};
\node[below=of query] (repair) {Repair ray-gun};
\end{tikzpicture}

This produces the image shown in Figure 12.1.

Figure 12.1: Nodes Positioned Relative to Each Other
 
Image of nodes. The text is displayed in the node position. There are no borders or connecting lines.

Ray-gun doesn't work
Is it charged? Recharge battery
Repair ray-gun
End of Image.

At the moment this doesn't look much like a flow chart. The nodes all default to a rectangular shape, but the shape isn't visible unless you use draw, for the outline, or fill, for the interior, within the ⟨node options⟩ specifications. In both cases, you can optionally supply a colour name. Since the xcolor package is automatically loaded by tikz, you can apply colour mixtures using the ! specification, such as red!50 to indicate 50% red (see the xcolor documentation [41] for further details).

Example:

\begin{tikzpicture}
\node[draw,fill=red!30] (start) {Ray-gun doesn't work};
\node[draw,fill=yellow,below=of start] (query) {Is it charged?};
\node[draw,fill=green!40,right=of query] (recharge) {Recharge battery};
\node[draw,fill=green!40,below=of query] (repair) {Repair ray-gun};
\end{tikzpicture}

This produces the image shown in Figure 12.2.

Figure 12.2: Node Shapes Drawn and Filled
 
As the previous image but the text for each node is now inside a rectangle with a black outline and coloured interior.

The rectangles can be given round corners using the rounded corners option. For example:

\node[rounded corners,draw,fill=green!40,below=of query] (repair) {Repair ray-gun};

If you want to change the node shape, there are a number of shapes provided by various tikz libraries. For example, the diamond shape is provided by the shapes.geometric library. The shape name is given in the ⟨node options⟩. For example:

\node[diamond,draw,fill=yellow,below=of start] (query) 
  {Is it charged?};

The default aspect ratio of the diamond width and height is 1. You can change this using the aspect option. For example:

\node[diamond,aspect=2,draw,fill=yellow,below=of start] (query) 
  {Is it charged?};

A line can be drawn between two nodes using:

\draw[options] (⟨node1 label⟩) -- (⟨node2 label⟩);

For example:

\draw (start) -- (query);

Arrow heads can be added to the start and end of the line using the option start arrow⟩-⟨end arrow, where ⟨start arrow⟩ and ⟨end arrow⟩ indicate the type of arrow head. The simplest arrow types are given by < for an arrow head pointing to the start and > for an arrow head pointing to the end. The ⟨start arrow⟩ or ⟨end arrow⟩ may be omitted if no arrow head is needed at the start or end, respectively.

Example:

\begin{tikzpicture}
\node[rounded corners,draw,fill=red!30] (start) {Ray-gun doesn't work};
\node[diamond,aspect=2,draw,fill=yellow,below=of start] (query) {Is it charged?};
\node[draw,fill=green!40,rounded corners,right=of query] (recharge) {Recharge battery};
\node[draw,fill=green!40,rounded corners,below=of query] (repair) {Repair ray-gun};
% draw in arrows:
\draw[->] (start) -- (query);
\draw[->] (query) -- (recharge);
\draw[->] (query) -- (repair);
\end{tikzpicture}

This produces the image shown in Figure 12.3.

Figure 12.3: Nodes with Connecting Arrows
 
As previous image but now the query node is inside a diamond, the rectangle nodes have rounded corners and there are arrows connecting the nodes.

A node can be added to a path. For example:

\draw[->] (query) -- (recharge) node[midway,above] {No};

This places a node (with the text “No”) above and midway along the line between the query and recharge nodes.

Example 59. Flow Chart

This example builds on the above. The arrows.meta library is loaded in order to use the Triangle[] arrow tip. This can be used by replacing the > arrow tip specifier in the optional argument to \draw. For example:

\draw[-{Triangle[]}] (start) -- (query);

Alternatively, the > arrow tip specifier can be set to Triangle[] for the given scope. For example:

\begin{tikzpicture}[>={Triangle []}]

This helps to ensure consistent arrow tips within the picture and means that you only need to edit one line if you decide to change the arrow tips (for example, from Triangle[] to Stealth[]).

Common node settings can be specified using every node/.style={node options} within the optional argument of the tikzpicture environment (to apply to all nodes within the environment) or the effect can be scoped using the scope environment (recall §7.5 Displaying a Calendar). This method can be used for the common settings for the recharge and repair nodes.

In addition, a thicker line width is set using the ultra thick option.

\documentclass{article}

\usepackage{tikz}[2013/12/13]% use at least version 3.0
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[ultra thick,>={Triangle[]}]
\node[rounded corners,draw,fill=red!30] (start) {Ray-gun doesn't work};
\node[diamond,aspect=2,draw,fill=yellow,below=of start] (query) {Is it charged?};
\begin{scope}[every node/.style={draw,fill=green!40,rounded corners}]
 \node[right=of query] (recharge) {Recharge battery};
 \node[below=of query] (repair) {Repair ray-gun};
\end{scope}
\draw[->] (start) -- (query);
\draw[->] (query) -- (recharge) node[midway,above] {No};
\draw[->] (query) -- (repair) node[midway,right] {Yes};
\end{tikzpicture}

\end{document}

Note that the last three lines of the tikzpicture environment above can also have the arrow tips automatically added through the use of the scope environment:

\begin{scope}[->]
 \draw (start) -- (query);
 \draw (query) -- (recharge) node[midway,above] {No};
 \draw (query) -- (repair) node[midway,right] {Yes};
\end{scope}

This produces the image shown in Figure 12.4. You can download or view this example.

Figure 12.4: An Example Flow Chart
 
As the previous image but with thicker lines and the “Yes” and “No” arrows are annotated.

Ray-gun doesn't work
Is it charged? No Recharge battery
↓Yes
Repair ray-gun
End of Image.



Footnotes

... libraries2
These are fairly new libraries, so you'll need an up-to-date version of pgf/tikz in order to use them.

This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs