Tikz Tutorial
Tikz Tutorial
A Tutorial
Satyaki Sikdar and David Chiang
[email protected]
Version 3
January 17, 2018
1 Introduction
“LATEX (pronounced lay-tek) is an open-source, multiplatform document preparation system for producing
professional-looking documents. . . .It is particularly suited to producing long, structured documents, and is
very good at typesetting equations” [University of Edinburgh Information Services, 2014].
The capabilities of the system are greatly enhanced with the help of native and third-party packages.1
TikZ2 is a package for drawing all kinds of graphics.
This tutorial introduces the reader to LATEX and the TikZ package, particularly for drawing state diagrams
of finite automata.
2 Setting up LATEX
To proceed with the tutorial, a working LATEX setup is necessary. You may install it locally on your machine,
but the simplest thing to do is use ShareLATEX (sharelatex.com). If you sign up using your nd.edu
address, you’ll get unlimited private project. For further information regarding setup, visit http://www.
latex-project.org/get/.
3 The preamble
Every LATEX document starts with a preamble. To make our automata look like the ones in the textbook
[Sipser, 2012], use the following preamble:
1
draw,
->,>=stealth’, % Makes edges directed with bold arrowheads
auto,
semithick}}
\let\epsilon\varepsilon
\begin{document}
% Content goes here
\end{document}
4 Basics
While there are many tutorials online, I suggest two: University of Edinburgh Information Services [2014]
and https://www.latex-tutorial.com/.
Here are some symbols often used in this course:
symbol control sequence usual meaning
Σ \Sigma alphabet
Γ \Gamma another alphabet
ε \varepsilon empty string
◦ \circ concatenation
# \texttt{\#} marker symbol
$ \texttt{\$} marker symbol
\textvisiblespace blank symbol
{} \{ \} delimiters for sets
∅ \emptyset empty set
6= \neq is not equal to
∈ \in is an element of
∈
/ \notin is not an element of
⊆ \subseteq is a subset of
→ \rightarrow (various meanings)
δ \delta transition function
α \alpha regular expression
∗
^\ast Kleene star
5 Drawing automata
Let’s start off with a simple DFA from Sipser [2012] (Figure 1.6). The formal description of the DFA is:
2
\begin{tikzpicture}
\node[state, initial] (q1) {$q_1$};
0 1
\node[state, accepting, right of=q1] (q2) {$q_2$};
0 \node[state, right of=q2] (q3) {$q_3$};
q1 1 q2 q3 \draw (q1) edge[loop above] node {\tt 0} (q1);
\draw (q1) edge node {\tt 1} (q2);
\draw (q2) edge[loop above] node {\tt 1} (q2);
0, 1
\draw (q2) edge[bend left] node {\tt 0} (q3);
\draw (q3) edge[bend left] node {{\tt 0}, {\tt 1}} (q2);
\end{tikzpicture}
\begin{tikzpicture}
% tikz code goes here
\end{tikzpicture}
5.2 Nodes
Let’s start off by drawing the nodes. Nodes can be positioned either manually or relative to other nodes.
Relative placement is often much easier.
\begin{tikzpicture}
q1 q2 q3 \node[state, initial] (q1) {$q_1$};
\node[state, accepting, right of=q1] (q2) {$q_2$};
\node[state, right of=q2] (q3) {$q_3$};
\end{tikzpicture}
The <options>, (<name>), and at (<x>,<y>) are all optional, but the {<label>} is required.
Name The name of a node is the name by which you refer to the node, when positioning other nodes
relative to it or when drawing edges into or out of it.
3
Position You specify the absolute position of a node using at (<x>,<y>) where <x> and <y> are coordi-
nates (<x> coordinates go to the right; <y> coordinates go up).
Or you can specify a relative position using left of=<name>, right of=<name>, above of=<name>,
below of=<name>. There’s also above left of=<name>, etc.3
The positioning library which we have already imported provides some further options.
• xshift=x, yshift=y: Gives manual control of the node positions after relative placement. Eg:
Label This can be anything you want. Typically you will surround it with dollar signs to use math mode.
5.3 Edges
Once the states are all in place, let’s start adding the transitions, that is, the edges between the states.
The \draw command can be used to draw the edges between the already created nodes (states).
\begin{tikzpicture}
\node[state, initial] (q1) {$q_1$};
q1 1 q2 q3 \node[state, accepting, right of=q1] (q2) {$q_2$};
\node[state, right of=q2] (q3) {$q_3$};
\draw (q1) edge node {\tt 1} (q2);
\end{tikzpicture}
Source and destination nodes Note that <source node> and <dest node> are the names of the nodes,
not their labels.
• By default, the edges are straight, so to prevent overlaps use bend left or bend right.
• To modify the placement of the edge label, use above or below.
Label This can be anything you want. Note that Sipser uses typewriter font for symbols, so you probably
want to write {\tt 0} or \texttt{0}.
Shorthand Multiple edges can be drawn with the same draw command, like so:
4
6 More examples
As another example, let’s draw a NFA (Sipser, Figure 1.42).
\begin{tikzpicture}
\node[state, initial, accepting] (1) at (1.5,2.6) {$1$};
1 \node[state] (2) at (0,0) {$2$};
\node[state] (3) at (3,0) {$3$};
ε
\draw (1) edge node{\tt b} (2)
b a edge[bend left=15] node {$\epsilon$} (3)
(2) edge[loop left] node{\tt a} (2)
a edge[below] node{{\tt a}, {\tt b}} (3)
2 3
a, b (3) edge[bend left=15] node{\tt a} (1);
\end{tikzpicture}
Our final example is the state diagram of the DFA equivalent to the NFA N1 :
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}}) ,
where δ is given by
a b
∅ ∅ ∅
{1} ∅ {2}
{2} {2, 3} {3}
{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.
a b
a, b ∅ {1} {2} {1, 2}
a, b
b b
b a a
a
a
{3} {1, 3} a {2, 3} {1, 2, 3}
b
b
5
\begin{tikzpicture}
\tikzset{every state/.append style={rectangle, rounded corners}}
\node[state] (emp) {$\emptyset$};
\node[state, accepting, right of=emp] (1) {$\{1\}$};
\node[state, right of=1] (2) {$\{2\}$};
\node[state, accepting, right of=2] (12) {$\{1, 2\}$};
\node[state, below of=emp] (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\}$};
\draw (emp) edge[loop left] node {{\tt a}, {\tt b}} (emp)
(1) edge[above] node {\tt a} (emp)
(1) edge node {\tt b} (2)
(2) edge node {\tt a} (23)
(2) edge[above] node {\tt b} (3)
(12) edge[auto=right,near start] node {{\tt a}, {\tt b}} (23)
(3) edge node {\tt b} (emp)
(3) edge node {\tt a} (13)
(13) edge[loop right] node {\tt a} (13)
(13) edge node {\tt b} (2)
(23) edge[bend left,above] node {\tt a} (123)
(23) edge[bend left] node {\tt b} (3)
(123) edge[loop above] node {\tt a} (123)
(123) edge[bend left,below] node {\tt b} (23);
\end{tikzpicture}
References
Michael Sipser. Introduction to the Theory of Computation. Cengage Learning, 3rd edition, 2012.
University of Edinburgh Information Services. LaTeX for beginners. http://www.docs.is.ed.ac.uk/
skills/documents/3722/3722-2014.pdf, 2014.