Draw a flowchart in LaTeX with TikZ
A flowchart in TikZ consists of nodes with a frame and arrows between them. Define one style per kind of box with \tikzset, place the nodes relative to each other with the positioning library and connect them with \draw[->].
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows.meta, positioning}
\tikzset{
startstop/.style={rectangle, rounded corners, draw, fill=red!15,
minimum width=2.5cm, minimum height=0.8cm},
process/.style={rectangle, draw, fill=blue!10,
minimum width=2.5cm, minimum height=0.8cm},
decision/.style={diamond, aspect=2, draw, fill=green!15}
}Define a style for each kind of box
\tikzset in the preamble stores a list of options under a name. Here startstop is a rounded rectangle, process a rectangle and decision a diamond; aspect=2 makes the diamond twice as wide as high. The diamond shape needs the library shapes.geometric. The following steps use these three styles.
This goes into the preamble.
\usetikzlibrary{shapes.geometric, arrows.meta, positioning}
\tikzset{
startstop/.style={rectangle, rounded corners, draw, fill=red!15,
minimum width=2.5cm, minimum height=0.8cm},
process/.style={rectangle, draw, fill=blue!10,
minimum width=2.5cm, minimum height=0.8cm},
decision/.style={diamond, aspect=2, draw, fill=green!15}
}Place the nodes below each other
Each node gets a style, a name in parentheses and its text in braces. below=of start puts the node below the node start; node distance=0.8cm is the gap between the borders of the two. right=of, left=of and above=of work the same way.
\begin{tikzpicture}[node distance=0.8cm]
\node[startstop] (start) {Start};
\node[process, below=of start] (read) {Read $n$};
\node[decision, below=of read] (test) {$n > 0$?};
\end{tikzpicture}Connect the nodes with arrows
\draw[->] (start) -- (read); draws an arrow from border to border. >=Stealth selects a filled arrow tip from the library arrows.meta for the whole picture. A node between -- and the target puts a label at the middle of the arrow.
\begin{tikzpicture}[node distance=0.8cm, >=Stealth]
\node[startstop] (start) {Start};
\node[process, below=of start] (read) {Read $n$};
\node[decision, below=of read] (test) {$n > 0$?};
\node[process, below=of test] (print) {Print $n$};
\draw[->] (start) -- (read);
\draw[->] (read) -- (test);
\draw[->] (test) -- node[right] {yes} (print);
\end{tikzpicture}Add a branch that loops back
(test.east) is the right corner of the diamond. ++(2,0) goes 2 cm to the right from there, and |- continues with a vertical and then a horizontal line to the target. -| draws the horizontal part first.
\begin{tikzpicture}[node distance=0.8cm, >=Stealth]
\node[startstop] (start) {Start};
\node[process, below=of start] (read) {Read $n$};
\node[decision, below=of read] (test) {$n > 0$?};
\node[process, below=of test] (print) {Print $n$};
\node[startstop, below=of print] (stop) {Stop};
\draw[->] (start) -- (read);
\draw[->] (read) -- (test);
\draw[->] (test) -- node[right] {yes} (print);
\draw[->] (print) -- (stop);
\draw[->] (test.east) -- node[above] {no} ++(2,0) |- (read);
\end{tikzpicture}Pitfalls
- Without
\usetikzlibrary{shapes.geometric}, thediamondshape stops with I do not know the key '/tikz/thik' and I am going to ignore it. Perhaps you misspelled it. Withoutpositioning,below=of startstops with a PGF Math error about the unknown functionof. - A node is as wide as its text. For text that should wrap, add
text width=3cm, align=centerto the style.
Compiled with pdfLaTeX from TeX Live 2023.