# 3-labelled automaton

I need to draw a (kind of) automaton with every arc having:

1. a "letter label" at the midpoint of the arc,

and (some) having

1. a "number label" at the beginning of the arc,

2. a "number label" at the end of the arc.

I thought that, instead of using plain tikz, it will be more confortable to use some existing library or package (say tikzcd, automata, tkzgraph...). But in every case I find some issues that I can not properly solve.

For example, using tikzcd I obtained the following (quite satisfactory) result

\documentclass{article}

\usepackage{tikz,tikz-cd}
\usetikzlibrary{arrows.meta,bending}

\tikzcdset{
arrow style=tikz,               % Latin Modern, math font, tikz
diagrams={>=stealth},            % to, stealth, latex, space
diagrams={nodes={inner sep=1pt}},
}

\begin{document}

\begin{tikzcd}[column sep = 50,row sep = 40]
\odot \arrow[loop left,distance=3em,
out=145,in=215,
"y" description,
blue]
\arrow[r,bend left=90,red,"x" description]
& \bullet
\arrow[l,red, "x" description,
"\scriptscriptstyle{(0,2)}"' very near start,
"\scriptscriptstyle{(1,2)}"' very near end]
\arrow[loop right,distance=3em,
out=35,in=-35,
"y" description,
"\scriptscriptstyle{(0,2)}"' very near start,
"\scriptscriptstyle{(1,2)}"' very near end,
blue]\\
&\bullet \arrow[lu,red,"x" description,
"\scriptscriptstyle{(1,0)}" very near end]
\arrow[u,blue,"y" description]
\end{tikzcd}

\end{document}


The problem is, of course, the "number labeling" of the loops. Is there a way to correctly position this labels (to appear in the start and end of the edge loop)?

I have also tried with the automata library (which I suppose is more appropriate for drawing automata, right?) with the following result:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\usetikzlibrary{arrows.meta,bending}

\tikzset{every state/.style={
inner sep=2pt,
minimum size=4pt,
%fill=black
}}

\tikzset{every edge/.append style={
font=\tiny    %\scriptsize  \tiny
}}

\tikzset{>=stealth}  %latex, to, stealth

\begin{document}

\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
\node[state,accepting] (1) {1};
\node[state] (4) [right=of 1] {4};
\node[state] (3) [below=of 4] {3};
\path[->]
(1) edge[red,bend left,in=90,out=90]
node {$a$}(4)
edge[loop left,blue,min distance=10mm]
node {$b$}(1)
(3) edge[red]
node {$a$}
node[very near end] {${(1,0)}$}(1)
edge[blue]
node {$b$}(4)
(4) edge[red]
node {$a$}
node[near end,swap] {${(1,2)}$}
node[very near start,swap] {${(0,2)}$} (1)
edge[loop right,blue,min distance=10mm]
node {$b$}
node[pos=0] {${(0,2)}$}
node[pos=1] {${(1,2)}$}(4);
\end{tikzpicture}

\end{document}


Here, the problems are more. At least:

1. positioning of "number labels" in loops (as before),

2. simulating the "description" effect to position "letter labels" like in the tikcd example,

3. bad spacing in "number labels" (probably caused by the \tiny font size).

Do you know how to fix these problems? Which option do you think is better for drawing this kind of automata?

Of course, I am not an expert. Sorry if I am asking something trivial o easy to find.

• Did you check this: texample.net/tikz/examples/feature/automata-and-petri-nets ? – Sigur May 12 '15 at 10:06
• Welcome to TeX.SE! Do you mind adding your preamble as well? I mean the document class and the packages. – Alenanno May 12 '15 at 10:16
• Ok, I will add the preamble as soon as i can reach my computer (several hours). Thanks. – suitangi May 12 '15 at 10:52
• @suitangi No problem. It's just to see what packages are you using. :D – Alenanno May 12 '15 at 12:00

Instead of auto you can fix where you want your labels. By default auto place labels above or below the path. Without auto they are placed on the path and if you want they cut the path, use fill=white.

Just an example:

\documentclass[tikz,border=3mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\usetikzlibrary{arrows.meta,bending}

\tikzset{every state/.style={
inner sep=2pt,
minimum size=4pt,
%fill=black
}}

\tikzset{every edge/.append style={
font=\tiny
}}

\tikzset{>=stealth}  %latex, to, stealth

\begin{document}

\begin{tikzpicture}[shorten >=1pt, node distance=2cm, on grid]
\node[state,accepting] (1) {1};
\node[state] (4) [right=of 1] {4};
\node[state] (3) [below=of 4] {3};
\path[->]
(1) edge[red,bend left,in=90,out=90]
node[fill=white] {$a$}(4)
edge[loop left,blue,min distance=10mm]
node[fill=white] {$b$}(1)
(3) edge[red]
node[fill=white] {$a$}
node[near end, below left=-1mm] {(1,0)}(1)
edge[blue]
node[fill=white] {$b$}(4)
(4) edge[red]
node[fill=white] {$a$}
node[pos=.8,above=-1mm] {(1,2)}
node[pos=.2,above=-1mm] {(0,2)} (1)
edge[loop right,blue,min distance=10mm]
node[fill=white] {$b$}
node[pos=0.1, above] {(0,2)}
node[pos=0.9, below] {(1,2)}(4);
\end{tikzpicture}

\end{document}

• thank you!, this fairly solves the positioning of "number labels" in loops. However I still have some doubts: (1) can I reduce the white space around "letter labels"? (2) now the spacing in "number labels" is correct, why is that? (3) why are you including tkz-graph and tkz-berge packages? what is its use? – suitangi May 13 '15 at 8:58
• @suitangi (1): Space around letter inside nodes is adjusted with inner sep (initially .3333em). node[fill=white, inner sep=0pt] will suppress it. (2): Because they don't use math format. Try \documentclass{article} \begin{document} (1,2) $(1,2)$ $(1,\!2)$ (a,b) $(a,b)$ $(a,\!b)$ \end{document} a look at differences between them. (3): Because they where there and I forgot to delete them. Now I did it. – Ignasi May 13 '15 at 9:15
• great!. Thank you again, and oops... sorry about (3). :) – suitangi May 13 '15 at 9:27