Tikz Tutorial
Tikz Tutorial
A Tutorial
Satyaki Sikdar
[email protected]
August 31, 2017
1 Introduction
Paraphrasing from [beg14], LATEX (pronounced lay-tek) is an open-source, multiplatform document prepa-
ration system for producing professional-looking documents, it is not a word processor. It is particularly
suited to producing long, structured documents, and is very good at typesetting equations.
The capabilities of the system are greatly enhanced with the help of native and third-party packages1 .
The tikz 2 library is one such package which primarily focuses on data visualization.
This tutorial will aim to introduce the reader to the tikz library, particularly for drawing state diagrams
of DFAs and NFAs.
2 Setting up LATEX
To proceed with the tutorial, a working LATEX setup is necessary. You may choose to install it locally on your
machine, or use an online service like ShareLATEX3 or Overleaf4 . For further information regarding setup,
visit http://www.latex-project.org/get/.
While there exists a myriad of tutorials online, [beg14] and https://www.latex-tutorial.com/ are
suggested to make you familiar with LATEX.
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows}
Each object in a tikz picture is called a node, and each node can be customized as per requirement.
Edges or paths can span between nodes.
Inside the document, each tikz diagram must reside in the tikzpicture environment. It’s advisable to
put the tikzpicture environment inside the figure environment, as shown below.
1 The Comprehensive T X Archive Network (CTAN) is the central place for all kinds of material around T X. https:
E E
//www.ctan.org/?lang=en
2 https://www.ctan.org/pkg/pgf?lang=en
3 https://www.sharelatex.com/
4 https://www.overleaf.com/
1
\begin{figure}[ht] % ’ht’ tells LaTeX to place the figure ’here’ or at the top of the page
\centering % centers the figure
\begin{tikzpicture}
% tikz code goes here
\end{tikzpicture}
\caption{Caption of the FSM}
\label{fig:my_label}
\end{figure}
The attributes of each tikzpicture can be declared either globally, or in the environment declaration
for each individual tikzpicture.
Setting the attributes of the pictures globally allows for all the figures to be consistent. To make the
FSMs consistent to those in the [Sip12], use the following code snippet in the document preamble after
importing the tikz library.
\tikzset{
->, % makes the edges directed
>=stealth’, % makes the arrow heads bold
node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
every state/.style={thick, fill=gray!10}, % sets the properties for each ’state’ node
initial text=$ $, % sets the text that appears on the start arrow
}
3.1 Nodes
Let’s start off by drawing nodes. Nodes can be positioned either manually or relative to other nodes. Relative
placement is often much easier. Note that the following two subsections is a slightly modified version of the
corresponding subsections in section 4 of [Sti15].
3.1.1 Options
The options (in the context of FSMs) are:
3.1.2 Positioning
The position of the states can easily be specified by using the positioning library which we have already
imported.
For the positioning are - amongst others - the following options available. These options can also be
combined:
• of = <node>: Specifies the node use mean by using right, left, .... Eg:
2
• xshift=x, yshift=y: Gives manual control of the node positions after relative placement. Eg:
• at (x, y): Forces tikz to place the node at (x, y) in the figure. The origin (0, 0) is at the center of
the figure. Eg:
\begin{tikzpicture}
\node[state] (q1) {normal};
\node[state, initial, right of=q1] (q2) {start};
\node[state, accepting] at (1.5, 2) (q3) {accept};
\end{tikzpicture}
accept
normal start
3.2 Edges
Once the states are all in place, let’s start adding the transitions, i.e. the edges between the states.
The draw command can be used to draw the edges between the already created nodes (states). The
syntax is as follows
Note:
• <source node> and <dest node> are the names of the nodes and NOT the labels.
• <edge properties> modify the apperance of the edge. For edges that start and end in the same node,
i.e. the loops, the loop <direction> property must be used. The direction determines the direction
of the loop. It can be above, below, left and right.
• By default, the edges are straight, so to prevent overlaps, they can be bent either left or right.
• The edge labels can be positioned either above or below the edge, with the keywords above and below
in the edge options.
Multiple edges can be drawn with the same draw command, in which case put each edge in it’s own line,
and on the final line end with the ;.
3
\draw (<source node1>) edge[<edge options1>] node{<edge label1>} (<dest node1>)
(<source node2>) edge[<edge options2>] node{<edge label2>} (<dest node2>)
...
...
...
(<source node k>) edge[<edge options k>] node{<edge label k>} (<dest nodek>);
\begin{tikzpicture}
\node[state, initial] (q1) {$q_1$};
\node[state, accepting, right of=q1] (q2) {$q_2$};
\node[state, right of=q2] (q3) {$q_3$};
0 1
0
q1 1 q2 q3
0, 1
4
\begin{tikzpicture}
\node[state, initial, accepting] (1) {$1$};
\node[state, below left of=1] (2) {$2$};
\node[state, right of=2] (3) {$3$};
b a
a 2 3
a, b
D2 = ({∅, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}, {a, b}, δ, {1, 3}, {{1}, {1, 2}, {1, 3}, {1, 2, 3}}) ,
a b
∅ ∅ ∅
{1} ∅ {2}
{2} {2, 3} {3}
where δ is given by {3} {1, 3} ∅ .
{1, 2} {2, 3} {2, 3}
{1, 3} {1, 3} {2}
{2, 3} {1, 2, 3} {3}
{1, 2, 3} {1, 2, 3} {2, 3}
Below is the code that generates the state diagram of the D2 . The output is shown in figure 4.
\begin{tikzpicture}
\node[state] (phi) {$\emptyset$};
\node[state, accepting, right of=phi] (1) {$\{1\}$};
\node[state, right of=1] (2) {$\{2\}$};
\node[state, accepting, right of=2] (12) {$\{1, 2\}$};
\node[state, below of=phi] (3) {$\{3\}$};
\node[state, initial, initial where=above, accepting, right of=3] (13) {$\{1, 3\}$};
\node[state, right of=13] (23) {$\{2, 3\}$};
\node[state, accepting, right of=23] (123) {$\{1, 2, 3\}$};
5
\draw (phi) edge[loop left] node{$a, b$} (phi)
(1) edge[above] node{$a$} (phi)
(1) edge[above] node{$b$} (2)
(2) edge[right] node{$a$} (23)
(2) edge[above] node{$b$} (3)
(12) edge[above, pos=.3, left=2pt] node{$a, b$} (23)
(3) edge[left] node{$b$} (phi)
(3) edge[below] node{$a$} (13)
(13) edge[loop right] node{$a$} (13)
(13) edge[above] node{$b$} (2)
(23) edge[bend left, above] node{$a$} (123)
(23) edge[bend left, below] node{$b$} (3)
(123) edge[loop above] node{$a$} (123)
(123) edge[bend left, below] node{$b$} (23);
\end{tikzpicture}
a b
a, b ∅ {1} {2} {1, 2}
b a, b a
b a
b
a
b
b
4 Wrapping up
By this point of the tutorial, the reader should be able to draw state diagrams of arbitrary DFAs and NFAs.
If needed, this tutorial will be extended to handle the state diagrams of PDAs, Turing Machines, Moore and
Mealy machines.
References
[beg14] Latex for beginners. http://www.docs.is.ed.ac.uk/skills/documents/3722/3722-2014.pdf,
2014.
[Sip12] M. Sipser. Introduction to the Theory of Computation. Cengage Learning, 2012. Third edition.