\documentclass{article}

% Normal packages
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{caption}

%Tikz packages and libraries
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}

% add commands to create the Tikz font and a degree-symbol
\newcommand{\logo}{Ti\textit{k}z }
\newcommand{\grad}{$^\circ$}

%to highlight the latex-code
\lstset{
	basicstyle=\small\ttfamily,						% sets the basic style of the code
		language={[LaTeX]TeX},
	numbersep=5mm, 									% space between line-number and code
	numbers=left, 									% line-numbers on the left side
	numberstyle=\small,								% style of line-numbers
	breaklines=true,								% allows line-breaks
	framexleftmargin=8mm,							% border begins 8mm earlier (include line numbers into box)
	xleftmargin=1.5cm,								% 1.5cm space from the left of text-start
	aboveskip=0.8cm,								% 0.8cm space above listing
	belowskip=0.4cm,								% 0.4cm space below listing
	backgroundcolor=\color{green!7},				% some slight-green background color
	captionpos=b,									% caption should be below (b) the listing
	frame=lrtb,										% adds a frame around the code
	tabsize=4,										% sets default tabsize to 2 spaces
	escapeinside=??,								% escape sequence inside lstlisting
	rulecolor=\color{red},							% bordercolor
	morekeywords={\usetikzlibrary,					% adds more custom keywords
	\node,
	\path,
	\edge},
	keywordstyle=\color[rgb]{0,0,1},				% style for the keywords
    commentstyle=\color[rgb]{0.133,0.545,0.133},	% style for the comments
    stringstyle=\color[rgb]{0.627,0.126,0.941}		% style for the strings
}
\renewcommand{\lstlistingname}{Code}
\captionsetup[lstlisting]{font={footnotesize},margin=1.5cm,singlelinecheck=false } % removes "Listing 1: "
\definecolor{light-light-gray}{gray}{0.95}

%add a 1 to the authors thanks (instead of a *)
\makeatletter
\let\@fnsymbol\@arabic
\makeatother

% add some author and document
\author{Hauke Stieler\\\vspace{10pt}\small\textit{Universität Hamburg} \thanks{For further contact also via my ID \texttt{4stieler}.}}
\title{\Huge{\logo for state-machines}\\\vspace{10pt}\large{A paper about \logo for computer scientists to draw state-machines.}}
\date{\today\\\small{ver.: 0.1 \texttt{en\_EN}}}

\begin{document}
	\maketitle
	\begin{abstract}
You'll find a short explanation on how to draw state-machines with \logo. Because I'm not a professional, please send ideas, mistakes, notes and suggestions to my e--mail--adress \href{mailto:mail@hauke-stieler.de}{mail@hauke-stieler.de}.
		
		Generally this is a collection\footnote[2]{For further information and as a book for cozy eventing, you'll find a lot of information in this 1165-page-manual: \href{ftp://ftp.fau.de/ctan/graphics/pgf/base/doc/pgfmanual.pdf}{\logo manual}} of examples for  different state-machines and -models, which are dealt with in the summer-semester 2015 in the FGI I lecture.
	\end{abstract}\newpage
	\tableofcontents
	\vfill
	\subsubsection*{Copyright}
		On the base of copyright (German copyright act from the 9th September 1965), the reproduction, disclosure, conversion in (audio) visual material and its broadcast, as well as the taking over of same-word-content, is only allowed with written permission.\\
		For all other countries: All right reserved \copyright .\\
		Furthermore I do not guarantee in any correctness and completeness of this document.
		\newpage
	\section{Prepare you editor}
		To use \logo, you need of course the same packet, which can be easily imported with \texttt{\textbackslash usepackage}. Next to this we have to import several \logo-libraries.
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Alle Pakete, die wir benutzen (möchten).}]
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}
			\end{lstlisting}
		\end{minipage}
		There are many other libraries in \logo, but we need just these (which are important for state-machines).
	\section{The \logo environment and its parameter}
	\subsection{Create an environment}
		Here's just an simple example on how to draw an graph (drawed state-machines are nothing else than state-\textit{graphs} ;) ).
		
		To draw graphs, \logo uses \texttt{node}s and \texttt{path}s. Nodes are the states/boxes/circles of a graph, while the path describes one or more line between them.
		
		To getting stared with drawing in \logo, we must create an environment:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Create a simple \logo-enviroment.}]
\begin{tikzpicture}
...
\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}
	\subsection{Parameter of an environment}
		For this environment, there are different parameter, we can choose from. For instance:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={A example with parameter in a \logo-environment.}]
\begin{tikzpicture}[->, 
	>=stealth', 
	shorten >= 5pt, 
	node distance = 2.8cm,
	semithick]
...
\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}
		Here's a short explanation of these parameter:\\
		\begin{itemize}
			\item \texttt{->} : All lines between nodes are arrows
			\item \texttt{>=stealth'} : The arrow heads are filled and triangles
			\item \texttt{shorten >= 5pt} : The arrow head stops \texttt{5pt} before the node
			\item \texttt{node distance=2.8cm} : All nodes are \texttt{2.8cm} away from each other
			\item \texttt{semithick} : Specifies the thickness of the line
		\end{itemize}
		More detailed information on the different parameter now.
	\subsection{Basic and miscellaneous parameter}
		Here just a few basic parameter.
		\begin{itemize}
			\item \texttt{-} : All connections are normal lines
			\item \texttt{->} : All connections are arrows
			\item \texttt{>= <Option>} : Specifies the type of the arrow head
			\item \texttt{shorten >= x} : Distance arrow head -- Node (e.g. 5pt, 1cm, ...)
		\end{itemize}
	\subsection{Color of graphs}
		You can change the color of the graph with the paremeter. To do that, just use the \texttt{color=<color>} option.\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Choosing a slight red color (60\% red, 40\% white/background)}]
\begin{tikzpicture}[->,
	thin,
	color=red!60]
...
\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}
	\section{Arrows and lines}
	\subsection{Thickness of lines and arrows}
		There're different thickness options available for arrows and lines (top: very thin, bottom: thickest):
		\begin{itemize}	
			\item \texttt{\textbackslash ultra thin}
			\item \texttt{\textbackslash very thin}
			\item \texttt{\textbackslash thin}
			\item \texttt{\textbackslash semithick}
			\item \texttt{\textbackslash thick}
			\item \texttt{\textbackslash very thick}
			\item \texttt{\textbackslash ultra thick}
		\end{itemize}
	\subsection{Arrow heads}
		To use arrows properly, add the \logo library \texttt{arrows} to your collection:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={The library \texttt{arrows} is used to draw arrows.}]
\usetikzlibrary{arrows}
			\end{lstlisting}
		\end{minipage}
		There're many different - party strange - types of arrows.
		The type of an arrow can also be specified at the beginning of an option.\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Specification of an arrow type.}]
\begin{tikzpicture}[->,
	>=<arrow-type>]
...
\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}
		Here's a small - for state-machines interesting - selection of different arrow types:
		\begin{itemize}
			\item no value : \tikz\draw[->,semithick] (0,0) -- (1,0) node [pos=0.2, right] {};
			\item \texttt{latex} : \tikz\draw[->,>=latex,semithick] (0,0) -- (1,0) node [pos=0.2, right] {};
			\item \texttt{latex'} : \tikz\draw[->,>=latex',semithick] (0,0) -- (1,0) node [pos=0.2, right] {};
			\item \texttt{stealth} : \tikz\draw[->,>=stealth,semithick] (0,0) -- (1,0) node [pos=0.2, right] {};
			\item \texttt{stealth'} : \tikz\draw[->,>=stealth',semithick] (0,0) -- (1,0) node [pos=0.2, right] {};
			\item \texttt{triangle <angle>} und \texttt{open triangle <angle>} : \\
				\hspace*{10pt} With 45\grad : \tikz\draw[->,>=triangle 45,semithick] (0,0) -- (1,0) node [pos=0.2, right] {}; \tikz\draw[->,>=open triangle 45,semithick] (2,0) -- (3,0) node [pos=0.2, right] {};\\
				\hspace*{10pt} With 60\grad : \tikz\draw[->,>=triangle 60,semithick] (0,0) -- (1,0) node [pos=0.2, right] {}; \tikz\draw[->,>=open triangle 60,semithick] (2,0) -- (3,0) node [pos=0.2, right] {};\\
				\hspace*{10pt} With 90\grad : \tikz\draw[->,>=triangle 90,semithick] (0,0) -- (1,0) node [pos=0.2, right] {}; \tikz\draw[->,>=open triangle 90,semithick] (2,0) -- (3,0) node [pos=0.2, right] {};
			\item \texttt{angle <angle>} \\
				\hspace*{10pt} With 45\grad : \tikz\draw[->,>=angle 45,semithick] (0,0) -- (1,0) node [pos=0.2, right] {};\\
				\hspace*{10pt} With 60\grad : \tikz\draw[->,>=angle 60,semithick] (0,0) -- (1,0) node [pos=0.2, right] {};\\
				\hspace*{10pt} With 90\grad : \tikz\draw[->,>=angle 90,semithick] (0,0) -- (1,0) node [pos=0.2, right] {};
		\end{itemize}
		The option \texttt{'} with the \texttt{latex'} creates a hyperbolic sharpened triangle as the arrow head. \texttt{stealth'} makes the edges more round than sharp.
		
		Furthermore can the option \texttt{->>} be used to create a double arrow with two heads (e.g. with the \texttt{stealth'} option: \tikz\draw[->>,>=stealth',semithick] (0,0) -- (1,0) node [pos=0.2, right] {}; ). This is possible with all kinds of arrows.
	\section{Nodes}
		Nodes are used to create states for our state-machine(s). Thanks to the library \texttt{automata} there are lots of different predefined options. To use these options, just integrate the library \texttt{automata}\\
		
		To position the created node very quickly and easily, you should use the library \texttt{positioning}:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Using the library \texttt{automata} and \texttt{positioning}}]
\usetikzlibrary{automata}
\usetikzlibrary{positioning}
			\end{lstlisting}
		\end{minipage}
	\subsection{Create nodes}
		Now we can create the first nodes with \texttt{\textbackslash node}. Wherein it should be noted that there's a specific syntax:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Syntax of the \texttt{\textbackslash node} command.}]
\node[<optionen>] (name) [<optional: position>] {text};
			\end{lstlisting}
		\end{minipage}\\
		When no position is specified, the node will be placed at the origin (normally at the very left side). The parameter for the positioning will be discussed later.
		
		Important to be mentioned is the opportunity to write the displayed text of a node in math-mode, which allows indices like $z_0$ or something.
		\subsubsection{Example machine without connections}
		A state-machine with the three states $z_0$, $z_1$ and $z_2$ can be look like this:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={Drawing of a very simple machine.}]
\begin{tikzpicture}[->,
	>=stealth',
	semithick]
	
	\node[state,initial]	(0)              {$z_0$};
	\node[state]			(1) [right of=0] {$z_1$};
	\node[state,accepting]	(2) [right of=1] {$z_2$};
\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}
		Ans here's the result:\\\\
		\begin{figure}[ht]
			\centering
			\begin{tikzpicture}[->,
				>=stealth',
				semithick]
				
				\node[state,initial]	(0)              {$z_0$};
				\node[state]			(1) [right of=0] {$z_1$};
				\node[state,accepting]	(2) [right of=1] {$z_2$};
			\end{tikzpicture}
			\caption{A simple machine with three states.}
		\end{figure}\\\\
		You can passing over parameters to the \texttt{\textbackslash node} command from the \texttt{automata} library as well.
	\subsection{Options for states}
		Here's a list of some useful options for states (\texttt{\textbackslash node}s):\\
		\begin{itemize}
			\item \texttt{state} : Has to be specified to create a state
			\item \texttt{initial} : Specifies a start-state\\
				\hspace*{10pt} Creates an arrow with \textit{start}-Label
			\item \texttt{accepting} : Indicates the final state of the machine\\
				\hspace*{10pt} Draws a double circle around the state
			\item \texttt{with output} : Creates a state with output\\
				\hspace*{10pt} More information \href{mailto:mail@hauke-stieler.de}{if desired}
		\end{itemize}
	\subsection{Positioning of the states}
		The position of the states can easily be specified by using the \texttt{positioning} library.
		\subsubsection{Options for positioning}
		For the positioning are - amongst others - the following options available. These options can also be combined:\\
		\begin{itemize}
			\item \texttt{right, left, above, below} : Next to the node (in given direction)
			\item \texttt{of = <Node>} : Specifies the node use mean by using \texttt{right, left, ...}
			\item \texttt{and} : When you want to use several distances, use \texttt{and}\\
			Ex.: \texttt{[...=2cm \underline{and} 1cm of A]}
		\end{itemize}
		\subsubsection{Automatic distance}
		It should be considered that there's a specific syntax for the \textbf{automatic distance} of nodes.\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Syntax for the positioning of nodes.}]
[<position> of = <reference-node>]
			\end{lstlisting}
		\end{minipage}\\
		The value of the automatic distance can be defined by using \texttt{node distance=...} with the normal \LaTeX units (\texttt{cm, em, pt, ...}).
		So for example:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={Automatic positioning of a node to the right of A.}]
[right of = A]
			\end{lstlisting}
		\end{minipage}\\
		This placed a node to the right of a node A, while the distance defined by \texttt{node distance=...} is used.
		\subsubsection{Manual distance}
		To use a \textbf{manual distance}, which can be specified by using the well known \LaTeX units \texttt{cm, em, pt, ...}, you have to consider the syntax:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[caption={Syntax for the manual positioning of nodes.}]
[<position> = <distance> of <reference-node>]
			\end{lstlisting}
		\end{minipage}\\
		For example:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={Manual positioning of a node right of A}]
[right = 2cm of A]
			\end{lstlisting}
		\end{minipage}\\
		This placed the one node 2cm to the right of node A.
		\subsubsection{Example for the positioning of nodes}
		Given are three nodes: $a$, $b$ and $c$.
		\begin{itemize}
			\item Node $b$ should be placed right and above (2cm right, 1cm above) the Node $a$
			\item Node $c$ should be placed automatically right and below of node $a$
		\end{itemize}
		Therefore you can skillfully combine the options:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={Combination of options for the positioning.}]
\begin{tikzpicture}[->,
	>=stealth',
	semithick,
	node distance=2cm]
	
\node [state] (a)                                {$a$};
\node [state] (b) [above right=1cm and 2cm of a] {$b$};
\node [state] (c) [below right of = a]           {$c$};

\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}\\
		If you choose a nice value for the distance (e.g. \texttt{node distance=2cm}), it can look pretty fancy:\\\\
		\begin{figure}[ht]
			\centering
			\begin{tikzpicture}[->,
				>=stealth',
				semithick,
				node distance=2cm]
				
				\node [state] (a)                                {$a$};
				\node [state] (b) [above right=1cm and 2cm of a] {$b$};
				\node [state] (c) [below right of = a]           {$c$};
			\end{tikzpicture}
			\caption{Three different placed nodes.}
		\end{figure}\\\\
	\section{Nodes verbinden}
	To not only have a bunch of states but a real state-machine, you have to connect these states. There's a command called \texttt{\textbackslash path} to create those connections.
	\subsection{Create arrows}
		\texttt{\textbackslash path} has a syntax as well:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={Syntax of the \texttt{\textbackslash path} command.}]
\path (<from-node>) edge [<options>] node {text} (<to-node>);
			\end{lstlisting}
		\end{minipage}\\
		Important to be noted is that you need to write \texttt{\textbackslash path} just once (see example below).\\
		What kind of options exist, is coming soon, but at first we'll see an example of this:\\
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={The well known machine from before, but this time with some connections.}]
\begin{tikzpicture}[->,
	>=stealth',
	semithick,
	node distance=2cm]
	
\node [state,initial] (a)                        {$a$};
\node [state] (b) [above right=1cm and 2cm of a] {$b$};
\node [state,accepting] (c) [below right of = a] {$c$};

\path (a) edge node {0} (b)
          edge node {1} (c)
      (c) edge node {2} (b);

\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}\\
		And here's the belonging \logo-machine\\
		\begin{figure}[ht]
			\centering
			\begin{tikzpicture}[->,
				>=stealth',
				semithick,
				node distance=2cm]
	
				\node [state,initial] (a)                        {$a$};
				\node [state] (b) [above right=1cm and 2cm of a] {$b$};
				\node [state,accepting] (c) [below right of = a] {$c$};
			
				\path (a) edge node {0} (b)
				          edge node {1} (c)
				      (c) edge node {2} (b);
			
			\end{tikzpicture}
			\caption{Three states with come connections (\textbf{without} options.}
		\end{figure}
	\subsection{Options for the \texttt{\textbackslash path} command}
		You can choose on different options as well:
		\begin{itemize}
			\item \texttt{right, left, above, below} : Specifies the position of the text
			\item \texttt{bend <right, left>} : Creates a bended edge. The direction of the bending is specified from the perspective of the arrow
			\item \texttt{bend <right, left> = <angle>} : The \texttt{angle} describes the strength of the bending
			\item \texttt{loop <right, left, above, below>} : Creates a loop above, below, left or right of the state
		\end{itemize}
	\section{Examples}
		Because the most important aspects as been discussed, you'll find a few examples down here.
	\subsection{Example 1}
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={}]
\begin{tikzpicture}[->,>=stealth',
	shorten >=5pt,
	node distance=2.5cm,
	semithick]

\node[initial,state]   (R)              {$z_r$};
\node[state]           (S) [right of=R] {$z_s$};
\node[state]           (T) [right of=S] {$z_t$};
\node[state,accepting] (E) [right of=T] {$z_e$};

\path 	(R) edge [loop,above]       node {0} (R)
            edge [below]            node {1} (S)
		(S) edge [below]            node {0} (T)
            edge [loop,above]       node {1} (S)
		(T) edge [bend left,below]  node {0} (R)
            edge [below]            node {1} (E)
		(E) edge [loop,above]       node {0,1} (E)
            ;
\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}\\
		\begin{figure}[ht]
			\centering
			\begin{tikzpicture}[->,>=stealth',
				shorten >=5pt,
				node distance=2.5cm,
				semithick]
		
				\node[initial,state]   (R)              {$z_r$};
				\node[state]           (S) [right of=R] {$z_s$};
				\node[state]           (T) [right of=S] {$z_t$};
				\node[state,accepting] (E) [right of=T] {$z_e$};
				
				\path 	(R) edge [loop,above]       node {0} (R)
				            edge [below]            node {1} (S)
						(S) edge [below]            node {0} (T)
				            edge [loop,above]       node {1} (S)
						(T) edge [bend left,below]  node {0} (R)
				            edge [below]            node {1} (E)
						(E) edge [loop,above]       node {0,1} (E)
			            ;
			\end{tikzpicture}\\
			\caption{A machine, accepting the language $L$ with $L=\{0,1\}^*\cdot \{101\}\cdot \{0,1\}^*$.}
		\end{figure}
	\subsection{Example 2}
		\begin{minipage}{\linewidth}
			\begin{lstlisting}[mathescape,caption={}]
\begin{tikzpicture}[->,
	>=stealth',
	node distance=2.8cm,
	semithick]

\node[initial,state]   (0)                    					{$z_0$};
\node[state]           (1) [above right=0.65cm and 1.5cm of 0]	{$z_1$};
\node[state]           (2) [below right=0.65cm and 1.5cm of 0]	{$z_2$};
\node[state,accepting] (E) [right=3.75cm of 0] 					{$z_e$};

\path	(0)	edge [above]				node {1} (1)
			edge [below]				node {0} (2)
		(1) edge [left, bend right=10]	node {0} (2)
			edge [above]				node {1} (E)
		(2) edge [right,bend right=10]	node {1} (1)
			edge [below,loop below]		node {0} (2)
		(E) edge [below]				node {0} (2)
			edge [right,loop right]		node {1} (E)
            ;
\end{tikzpicture}
			\end{lstlisting}
		\end{minipage}\\
		\begin{figure}[ht]
			\centering
			\begin{tikzpicture}[->,
				>=stealth',
				node distance=2.8cm,
				semithick]
			
			  \node[initial,state]   (0)                    					{$z_0$};
			  \node[state]           (1) [above right=0.65cm and 1.5cm of 0]	{$z_1$};
			  \node[state]           (2) [below right=0.65cm and 1.5cm of 0]	{$z_2$};
			  \node[state,accepting] (E) [right=3.75cm of 0] 					{$z_e$};
			
			  \path (0) edge [above]					node {1} (1)
			            edge [below]					node {0} (2)
		            (1) edge [left,bend right=10]		node {0} (2)
			            edge [above]					node {1} (E)
		            (2) edge [right,bend right=10]		node {1} (1)
			            edge [loop below,below]		node {0} (2)
		            (E) edge [below]					node {0} (2)
			            edge [loop right,right]		node {1} (E)
			            ;
			\end{tikzpicture}\\
			\caption{A machine accepting the language $L$ with $L=\{0,1\}^*\cdot \{0,11\}$.}
		\end{figure}
\end{document}