# Flowchart in TikZ: the template explained step by step

> How a TikZ flowchart is built: styles for terminals, steps and decisions, positioning, labelled arrows and a |- loop, plus chain and swimlane variations.

URL: https://www.labcanvas.com/tikz/examples/diagrams/flowchart-template

Updated 2026-11-22. Original figure: Flowchart template, by OpenTikZ contributors, CC0 1.0 (licence text: https://www.labcanvas.com/tikz-library/credits.html#opentikz), from https://github.com/opentikz/opentikz/blob/359befbf8e8af7ce08e7e387b2c2a198e0ca735d/templates/flowchart/template.tex. Walkthrough and variations by LabCanvas; the variation code is CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/). Pictures typeset with TeX Live 2026 and PGF 3.1.12. Category: [TikZ diagram examples: flowcharts, mind maps, Venn](https://www.labcanvas.com/tikz/examples/diagrams).

## Quick answer

This template draws a small flowchart: Start, Read input, a Valid? decision, Process data and End down a vertical spine, with a No branch to Report error and a purple retry arrow back to Read input. Each kind of box is a style (`terminal`, `process`, `decision`) built on a common `base`, the boxes are placed with `below=of` and `right=1.7cm of` from the `positioning` library, and the loop is a single `|-` path. All the labels and sizes are macros at the top, so you can reuse it by editing parameters only.

## The figure and its code

![A vertical flowchart: a green rounded terminal Start, a blue box Read input, an orange diamond Valid?, a blue box Process data and a green terminal End joined by grey arrows, Yes beside the arrow below the diamond, a No arrow to a blue box Report error on the right, and a purple arrow labelled retry from the top of Report error back to the right side of Read input.](https://www.labcanvas.com/tikz-snippets/fig-flowchart-template/original.webp)

By OpenTikZ contributors, CC0 1.0 (https://www.labcanvas.com/tikz-library/credits.html#opentikz). The code below is exactly the library's file (pdfLaTeX); its first lines name the source and the licence. Open it in the TikZ editor from the page: https://www.labcanvas.com/tikz/examples/diagrams/flowchart-template

```latex
% Flowchart template
% Source: https://github.com/opentikz/opentikz/blob/359befbf8e8af7ce08e7e387b2c2a198e0ca735d/templates/flowchart/template.tex
% By OpenTikZ contributors, dedicated to the public domain (CC0 1.0).
\documentclass[border=8pt]{standalone}

% --- packages (mirror these in template.meta.json "requires") ---
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta,shapes.geometric,shapes.misc}

% --- palette (canonical source: reference/color-palettes/color-palettes.md; light variant) ---
\definecolor{otblue}{HTML}{0072B2}
\definecolor{otorange}{HTML}{E69F00}
\definecolor{otteal}{HTML}{009E73}
\definecolor{otpurple}{HTML}{CC79A7}
\definecolor{otgray}{HTML}{5A5A5A}

\begin{document}
% ==== parameters (edit these) ============================================
\def\rowsep{0.95}   % vertical gap between steps (cm)
\def\boxminw{2.4}   % minimum process/terminal width (cm)
\def\boxminh{0.95}  % minimum box height (cm)
% labels
\def\startlabel{Start}
\def\readlabel{Read input}
\def\checklabel{Valid?}
\def\proclabel{Process data}
\def\endlabel{End}
\def\errorlabel{Report error}
\def\yeslabel{Yes}
\def\nolabel{No}
\def\looplabel{retry}
% =========================================================================

\begin{tikzpicture}[
    node distance=\rowsep cm,
    >={Stealth[length=2.4mm]},
    base/.style={
      line width=0.7pt, align=center, font=\sffamily\small,
      minimum width=\boxminw cm, minimum height=\boxminh cm,
    },
    terminal/.style={base, shape=rounded rectangle, draw=otteal!70!black, fill=otteal!16},
    process/.style={base, rounded corners=3pt, draw=otblue!70!black, fill=otblue!16},
    decision/.style={base, shape=diamond, aspect=2.2, inner sep=1pt,
      draw=otorange!80!black, fill=otorange!20, minimum width=2.6cm},
    flow/.style={draw=otgray!75, line width=0.9pt, ->},
    loop/.style={draw=otpurple!85!black, line width=1.0pt, ->},
    elabel/.style={font=\sffamily\footnotesize, text=otgray},
  ]

  % --- main vertical spine ---
  \node[terminal] (start) {\startlabel};
  \node[process,  below=of start] (read)  {\readlabel};
  \node[decision, below=of read]  (check) {\checklabel};
  \node[process,  below=of check] (proc)  {\proclabel};
  \node[terminal, below=of proc]  (end)   {\endlabel};
  % --- error branch to the right of the decision ---
  \node[process, right=1.7cm of check] (error) {\errorlabel};

  % --- flow ---
  \draw[flow] (start) -- (read);
  \draw[flow] (read)  -- (check);
  \draw[flow] (check) -- (proc) node[midway, right, elabel] {\yeslabel};
  \draw[flow] (proc)  -- (end);
  \draw[flow] (check) -- (error) node[midway, above, elabel] {\nolabel};
  % --- retry loop: error back up to read ---
  \draw[loop] (error.north) |- (read.east)
    node[pos=0.75, above, font=\sffamily\footnotesize, text=otpurple!85!black] {\looplabel};

\end{tikzpicture}
\end{document}
```

## Libraries for the shapes and the palette

*Lines 4–15 of the code.*

Besides `positioning` and `arrows.meta`, the template loads two shape libraries: `shapes.geometric` for the `diamond` of the decision and `shapes.misc` for the `rounded rectangle` of the terminals, a box with semicircular ends. The five colours are defined by hex code; blue, orange, teal and purple are Okabe–Ito colours, chosen to stay distinct for colour-blind readers.

## Parameters: spacing, sizes and every label

*Lines 18–32 of the code.*

The block between the two rules of `=` signs is meant to be edited: `\def\rowsep{0.95}` is the vertical gap between steps, `\boxminw` and `\boxminh` the minimum box size, and nine macros hold every piece of text, from `\startlabel` to `\looplabel`. Keeping the text out of the drawing code means a translation or a different process only touches this block.

## A base style and three box styles

*Lines 34–48 of the code.*

`node distance=\rowsep cm` sets the gap that `below=of` will use, 0.95 cm between borders. `base` holds what every box shares: line width, centred text, a small sans-serif font and the minimum size. The three box styles start with `base` and add a shape and colours: `terminal` uses `shape=rounded rectangle`, `process` a rectangle with `rounded corners=3pt`, and `decision` a `diamond` with `aspect=2.2`, which makes it 2.2 times as wide as tall, so a one-word question does not produce a tall, narrow diamond. The decision also gets its own `minimum width=2.6cm`, given after `base` so that it overrides the base value. `flow` and `loop` are the two arrow styles, and `elabel` the small grey font for Yes and No.

## The spine, placed box under box

*Lines 50–57 of the code.*

Only the first node has no position; every other node is placed from a previous one: `\node[process, below=of start] (read) {\readlabel};`, then the decision below Read input, and so on. With the positioning library, `below=of` leaves exactly `node distance` between the two borders, so the gaps stay equal although the diamond is taller than the boxes. The error box goes `right=1.7cm of check`, beside the decision. The extra spaces inside the brackets only align the code into columns.

## Arrows, Yes and No

*Lines 59–64 of the code.*

Arrows between named nodes, such as `\draw[flow] (start) -- (read);`, start and end on the node borders, including the slanted sides of the diamond. The Yes and No labels are nodes written after the target: `\draw[flow] (check) -- (proc) node[midway, right, elabel] {\yeslabel};`. A node at the end of a path is placed on the segment just drawn, `midway` puts it at the middle of that segment and `right` moves it off the line; for the horizontal No arrow, `above` does the same.

## The retry loop with |-

*Lines 65–67 of the code.*

`\draw[loop] (error.north) |- (read.east)` goes straight up from the top of Report error and then left into the right side of Read input: `|-` means vertical first, then horizontal, so the corner is directly above the error box and level with Read input. The label is placed with `pos=0.75`: on a `|-` path, position 0.5 is the corner, so 0.75 is the middle of the horizontal part, and `above` puts “retry” over it in the loop’s purple.

## Techniques used

- [Flowcharts](https://www.labcanvas.com/tikz/flowchart): terminal, process and decision styles, labelled branches and loops.
- [Placement with positioning](https://www.labcanvas.com/tikz/nodes): `below=of` and `right=1.7cm of` with one `node distance`.
- [Shapes: diamond and rounded rectangle](https://www.labcanvas.com/tikz/shapes): `shape=diamond, aspect=2.2` and `shape=rounded rectangle`.
- [Elbow arrows with |-](https://www.labcanvas.com/tikz/arrows): the retry loop, with its label at `pos=0.75`.

## Variations

Our own code, CC0 1.0, each typeset with TeX Live 2026 in a standalone document with the preamble lines shown.

### Left to right with the chains library

For a slide, the same process runs left to right. The `chains` library removes the explicit placement: `start chain=going right` starts a chain, and a `box` style containing `on chain, join` puts each new node to the right of the previous one and draws the arrow to it, styled by `every join`. The error box is not on the chain; it is placed `below=10mm of check`, and the retry arrow returns with `-|` (horizontal, then up).

![A horizontal flowchart: Start, Read input, a diamond Valid?, Process data and End joined by arrows, Yes above the arrow after the diamond, a No arrow down to a red box Report error, and a violet arrow labelled retry from the error box left and up into Read input.](https://www.labcanvas.com/tikz-snippets/fig-flowchart-template/chain-left-to-right.webp)

*the flowchart left to right: steps on a chain joined automatically, the error branch below*

Preamble: `\usepackage{tikz}`, `\usetikzlibrary{chains, positioning, shapes.geometric, shapes.misc, arrows.meta}`

```latex
\begin{tikzpicture}[
    start chain=going right, node distance=7mm, font=\sffamily\small,
    every join/.style={->, thick, >={Stealth[length=2mm]}},
    % every box is placed on the chain and joined to the previous one
    box/.style={on chain, join, draw, thick, align=center, minimum height=9mm},
    terminal/.style={box, shape=rounded rectangle, fill=teal!15},
    process/.style={box, rounded corners=2pt, fill=blue!12},
    decision/.style={box, shape=diamond, aspect=1.8, inner sep=1pt, fill=orange!20},
  ]
  \node[terminal] {Start};
  \node[process] (read) {Read\\input};
  \node[decision] (check) {Valid?};
  \node[process] (proc) {Process\\data};
  \node[terminal] {End};
  \path (check) -- (proc) node[midway, above, font=\sffamily\footnotesize] {Yes};
  % the error branch is not on the chain
  \node[draw, thick, rounded corners=2pt, fill=red!10, minimum height=9mm, align=center,
        below=10mm of check] (err) {Report\\error};
  \draw[->, thick] (check) -- node[right, font=\sffamily\footnotesize] {No} (err);
  \draw[->, thick, violet] (err) -| node[pos=0.25, below, font=\sffamily\footnotesize] {retry} (read);
\end{tikzpicture}
```

### Swimlanes for who does what

Swimlanes show which actor performs each step. The steps are placed with positioning as usual, user steps on the top row and system steps on the bottom row. The lanes are two `fit` nodes on the background layer; corner coordinates such as `(e.east |- n.north)` make both lanes span the whole width of the diagram, not just their own steps.

![A flowchart in two horizontal bands, a blue band labelled User with Start, Fill in form, Submit and Read confirmation, and an orange band labelled System with Validate, Save record and End; arrows run from Submit down to Validate, along to Save record and End, and from Save record up to Read confirmation.](https://www.labcanvas.com/tikz-snippets/fig-flowchart-template/swimlanes.webp)

*a two-lane flowchart: user steps on top, system steps below, lanes drawn behind with fit*

Preamble: `\usepackage{tikz}`, `\usetikzlibrary{positioning, fit, backgrounds, shapes.misc, arrows.meta}`

```latex
\begin{tikzpicture}[
    node distance=6mm and 8mm, font=\sffamily\small, >={Stealth[length=2mm]},
    box/.style={draw, thick, rounded corners=2pt, fill=white, minimum height=8mm, align=center},
    flow/.style={->, thick},
  ]
  % user steps on the top row, system steps on the bottom row
  \node[box, shape=rounded rectangle, sharp corners] (s) {Start};
  \node[box, right=of s] (u1) {Fill in\\form};
  \node[box, right=of u1] (u2) {Submit};
  \node[box, below=16mm of u2] (v) {Validate};
  \node[box, right=of v] (sv) {Save\\record};
  \node[box, shape=rounded rectangle, sharp corners, right=of sv] (e) {End};
  \node[box, above=16mm of sv] (n) {Read\\confirmation};
  \draw[flow] (s) -- (u1);
  \draw[flow] (u1) -- (u2);
  \draw[flow] (u2) -- (v);
  \draw[flow] (v) -- (sv);
  \draw[flow] (sv) -- (e);
  \draw[flow] (sv) -- (n);
  % two lanes as wide as the whole diagram, behind the steps
  \coordinate (ne) at (e.east |- n.north);
  \coordinate (sw) at (s.west |- v.south);
  \begin{scope}[on background layer]
    \node[fill=blue!8, fit=(s) (n) (ne), inner xsep=6mm, inner ysep=4mm] (user) {};
    \node[fill=orange!12, fit=(sw) (v) (e), inner xsep=6mm, inner ysep=4mm] (system) {};
  \end{scope}
  \node[anchor=north west, font=\sffamily\footnotesize\bfseries] at (user.north west) {User};
  \node[anchor=north west, font=\sffamily\footnotesize\bfseries] at (system.north west) {System};
\end{tikzpicture}
```

## Frequently asked questions

### How do I make a flowchart in TikZ?

Define styles for the box types (a rounded rectangle for start and end, a rectangle for steps, a diamond for decisions), place the nodes with below=of from the positioning library, and connect them with \draw[->] (a) -- (b);. Label a branch with node[midway, right] {Yes} after the target. The template on this page is a complete starting point.

### How do I draw a loop back arrow in a TikZ flowchart?

Use an elbow path: \draw[->] (error.north) |- (read.east); goes up and then across. With -| it goes across first. For more room, route through a point: (error.east) -- ++(1,0) |- (read.east).

### Why is my diamond too tall?

A diamond grows in both directions to fit its text. Set aspect=2 (or more) to make it wider than tall, reduce inner sep, or break the text into two lines.

## References

- [opentikz/opentikz at commit 359befb: templates/flowchart/template.tex (CC0 1.0)](https://github.com/opentikz/opentikz/blob/359befbf8e8af7ce08e7e387b2c2a198e0ca735d/templates/flowchart/template.tex)
- [PGF/TikZ manual, §17.5.3 Advanced Placement Options (positioning, node distance)](https://tikz.dev/tikz-shapes#sec-17.5.3)
- [PGF/TikZ manual, §17.8 Placing Nodes on a Line or Curve Explicitly (pos, midway)](https://tikz.dev/tikz-shapes#sec-17.8)
- [PGF/TikZ manual, §14.2.2 Horizontal and Vertical Lines (|-)](https://tikz.dev/tikz-paths#sec-14.2.2)
- [PGF/TikZ manual, §71.3 Geometric Shapes (diamond, aspect)](https://tikz.dev/library-shapes#sec-71.3)
- [PGF/TikZ manual, §71.8 Miscellaneous Shapes (rounded rectangle)](https://tikz.dev/library-shapes#sec-71.8)
- [PGF/TikZ manual, §48 Chains](https://tikz.dev/library-chains)
- [PGF/TikZ manual, §54 Fitting Library](https://tikz.dev/library-fit)
- [PGF/TikZ manual, §45 Background Library](https://tikz.dev/library-backgrounds)

## TikZ guides for these techniques

- [TikZ flowchart: shapes, decisions, loops and a template](https://www.labcanvas.com/tikz/flowchart): Flowcharts in LaTeX with TikZ: the standard shapes, yes/no decisions, right-angled arrows, loops, swimlanes and a reusable template with compiled output.
- [TikZ diagrams: block, cycle, flowchart, tree and more](https://www.labcanvas.com/tikz/diagrams): How to draw diagrams in TikZ: block, cycle, flowchart, tree, graph, timeline, matrix, commutative and circuit diagrams, each with compiled code.
- [TikZ nodes: shapes, anchors, positioning and labels](https://www.labcanvas.com/tikz/nodes): How TikZ nodes work: shapes, size (inner sep, minimum size), all anchors, the positioning library (right=of, node distance), labels, pins and scaling.
- [TikZ shapes: triangles, stars, clouds, cylinders, crosses and braces](https://www.labcanvas.com/tikz/shapes): Every TikZ node shape (shapes.geometric, symbols, misc, arrows) with options: triangle, star, cloud, cylinder, cross, curly braces, and custom styles.
- [TikZ arrows: arrow tips, sizes, curves and labels](https://www.labcanvas.com/tikz/arrows): All TikZ arrow tips in one rendered table, arrows.meta sizing (length, width, scale, open, round), and curved, labelled, thick, block and squiggly arrows.
