4

Here's my code:

\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows}
\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
}    
\begin{figure}[ht]
      \centering
      \begin{tikzpicture}
    
      \node[state, initial] (0) {$q_0$};
      \node[state] (1) at(0,2.5) {$q_1$};
      \node[state] (2) at(2.5,2.5) {$q_2$};
      \node[state] (3) at(0,-2.5) {$q_3$};
      \node[state] (4) at(2.5,-2.5) {$q_4$};
      \node[state] (5) at(2.5,0) {$q_5$};
      \node[state] (a) at(-5,-2.5) {$q_{acc}$};
      \node[state] (r) at(7.5,0) {$q_{rej}$};
        
      \path (0) edge[right] node{$1 \; \sqcup \; R$} (1)
        (0) edge[right] node{$0 \; \sqcup \; R$} (3)
        (0) edge[left, above=5cm] node{$\sqcup \; \sqcup \; R$} (a)
        (1) edge[loop left] node{$\begin{matrix} 1\; 1\; R \\ 0\;0\;R \end{matrix}$} (1)
        (1) edge[above] node{$\sqcup \; \sqcup \; L$} (2)
        (3) edge[below] node{$\sqcup \; \sqcup \; L$} (4)
        (3) edge[loop left] node{$\begin{matrix} 0 \; 0 \; R \\ 1 \; 1 \; R \end{matrix}$} (3)
        (2) edge[right] node{$1 \; \sqcup \; L$} (5)
        (4) edge[right] node{$0 \; \sqcup \; L$} (5)
        (5) edge[loop right] node{$\begin{matrix} 0 \; 0 \; L \\ 1 \; 1 \; L \end{matrix}$} (5)
        (5) edge[above] node{$\sqcup \; \sqcup \; R$} (0)
        (2) edge[right, above=5cm] node{$0 \; 0 \; R$} (r)
        (4) edge[right, below=5cm] node{$1 \; 1 \; R$} (r);
        
        
    \end{tikzpicture}
    \label{fig:my_label}
    \end{figure}

And here's the result:

enter image description here

I want to make the labels on the edges to q_rej and q_acc farther away from the edge itself but increasing the distance above/below has a threshold and you can't move the label to the right/left if you use "above" or "below" in the edge properties. Would appreciate any help!

2 Answers 2

5

enter image description here

I rewrote your code a bit.

  • on grid positions the nodes on a grid. The nodes can be conveniently positioned relative to each other with above/right/below/left/above left/..., as defined by the tikz library positioning.

  • auto places the edge labels automatically. If they appear on the wrong side, use swap to put them on the other side.

  • sloped places the node labels, well, sloped, which maybe solves your original question.

  • Define a command \blank to typeset the visible space. You may of course use $\sqcup$, but there is a predefined command \visiblespace as well. There are other options, like the visible space used in typewriter mode.

  • Define a command \el (edge label) to typeset the triples. This way you can adjust the formatting of the labels in one place.

\documentclass[tikz]{standalone}
\usetikzlibrary{automata, positioning, arrows}
\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
on grid,
auto
}
\newcommand\blank{\textvisiblespace}
\newcommand\el[3]{#1\,#2\,#3}
\begin{document}
      \begin{tikzpicture}
      \node[state, initial] (0) {$q_0$};
      \node[state] (1) [above=of 0] {$q_1$};
      \node[state] (2) [right=of 1] {$q_2$};
      \node[state] (3) [below=of 0] {$q_3$};
      \node[state] (4) [right=of 3] {$q_4$};
      \node[state] (5) [right=of 0] {$q_5$};
      \node[state] (a) [left=4 of 3] {$q_{acc}$};
      \node[state] (r) [right=4 of 5] {$q_{rej}$};
        
      \path
        (0) edge node[swap]{\el 1\blank R} (1)
        (0) edge node{\el 0\blank R} (3)
        (0) edge node[sloped]{\el \blank\blank R} (a)
        (1) edge[loop left] node[align=left]{\el 11R \\ \el 00R} (1)
        (1) edge node{\el \blank\blank L} (2)
        (3) edge node{\el \blank\blank L} (4)
        (3) edge[loop left] node[align=left]{\el 00R \\ \el 11R} (3)
        (2) edge node{\el 1\blank L} (5)
        (4) edge node[swap]{\el 0\blank L} (5)
        (5) edge[loop right] node[align=left]{\el 00L \\ \el 11L} (5)
        (5) edge node[swap]{\el \blank\blank R} (0)
        (2) edge node[sloped]{\el 00R} (r)
        (4) edge node[sloped]{\el 11R} (r);
        
        
    \end{tikzpicture}
  \end{document}  

If you do not want the labels to be sloped, just omit the keyword sloped. In my solution the labels will not overlap with the edge. For fine tuning, you can use xshift and yshift, like below.

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{automata, positioning, arrows}
\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
on grid,
auto
}
\newcommand\blank{\textvisiblespace}
\newcommand\el[3]{#1\,#2\,#3}
\begin{document}
      \begin{tikzpicture}
      \node[state, initial] (0) {$q_0$};
      \node[state] (1) [above=of 0] {$q_1$};
      \node[state] (2) [right=of 1] {$q_2$};
      \node[state] (3) [below=of 0] {$q_3$};
      \node[state] (4) [right=of 3] {$q_4$};
      \node[state] (5) [right=of 0] {$q_5$};
      \node[state] (a) [left=4 of 3] {$q_{acc}$};
      \node[state] (r) [right=4 of 5] {$q_{rej}$};
        
      \path
        (0) edge node[swap]{\el 1\blank R} (1)
        (0) edge node{\el 0\blank R} (3)
        (0) edge node[xshift=-3mm]{\el \blank\blank R} (a)
        (1) edge[loop left] node[align=left]{\el 11R \\ \el 00R} (1)
        (1) edge node{\el \blank\blank L} (2)
        (3) edge node{\el \blank\blank L} (4)
        (3) edge[loop left] node[align=left]{\el 00R \\ \el 11R} (3)
        (2) edge node{\el 1\blank L} (5)
        (4) edge node[swap]{\el 0\blank L} (5)
        (5) edge[loop right] node[align=left]{\el 00L \\ \el 11L} (5)
        (5) edge node{\el \blank\blank R} (0)
        (2) edge node[xshift=-3mm]{\el 00R} (r)
        (4) edge node[swap,xshift=-3mm]{\el 11R} (r);
        
        
    \end{tikzpicture}
  \end{document}
2

Only as a complement and inevitable to add the great adaptation of style that Gernot made, only this time as a definition and test if it is also possible to do it that way, and if it works... as for the complements, although on grid allows them to be distanced from the center of the nodes and not from the edges of the node if you draw, in my opinion I prefer to define a constant in order to be able to make separations proportional to a specific factor, also to take advantage of the fact that the node names can be mnemonic, so when drawing you can easily remember where you are going from, then I added the other node positioning options between nodes, those that are tied to the connections between nodes, to do what you need you can change leaving the node empty and placing what you want in a label that is relative to the node and can be positioned using polar coordinates, and each can have a different style.

RESULT:

enter image description here

MWE:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{automata, positioning, arrows}
\begin{document}
    \begin{tikzpicture}[
            ->, % 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
            test/.style={thick, fill=gray!10,font=\tiny,align=center}, % sets the properties for each ’state’ node
            initial text=$ $, % sets the text that appears on the start arrow
            on grid % Node distance separation from node.center
        ]
        \def\MyDistance{3.5} %Node distance is the default but you cant call this to use like int*\MyDistance   
        \def\bk{\textvisiblespace} %Coming from Gernot's example with an unbeatable style but proven with definitions
        \def\el#1#2#3{#1\,#2\,#3}   
        \node[state, initial] (Q0) {$q_0$};
        \node[state, above=\MyDistance of Q0] (Q1) {$q_1$};
        \node[state, right=\MyDistance of Q1] (Q2) {$q_2$};
        \node[state, below=\MyDistance of Q0] (Q3) {$q_3$};
        \node[state, right=\MyDistance of Q3] (Q4) {$q_4$};
        \node[state, right=\MyDistance of Q0] (Q5) {$q_5$};
        \node[state, left=2*\MyDistance of Q3] (QACC) {$q_{acc}$};
        \node[state, right=2*\MyDistance of Q5] (QREJ) {$q_{rej}$};
        
        \path
        (Q0) edge[right] node{\el 1\bk R} (Q1)
        (Q0) edge[right] node{\el 0\bk R} (Q3)
        (Q0) edge[left, above=5cm, sloped] node{\el \bk\bk R} (QACC) %Sloped to align the text to the arrow
        (Q1) edge[loop left, align=center] node{\el 11R \\ \el 00R} (Q1) %Using align=center to enable \\ use in node_Text
        (Q1) edge[above] node{\el \bk\bk L} (Q2)
        (Q3) edge[below] node{\el \bk\bk L} (Q4)
        (Q3) edge[loop left,align=center] node{\el 00R \\ \el 11R} (Q3)
        (Q2) edge[right] node{\el 1\bk L} (Q5)
        (Q4) edge[right] node{\el 0\bk L} (Q5)
        (Q5) edge[loop right, align=center] node{\el 00L \\ \el 11L} (Q5)
        (Q5) edge[above] node{\el \bk\bk R} (Q0)
        (Q2) edge[above, sloped] node[label={[label distance=-0.4cm,red]30:\el 00R}]{} (QREJ)
        (Q4) edge[below] node[label={[label distance=-0.4cm,blue]-30:\el 11R}]{} (QREJ);
        
        %Some test using labels to use node distance.
        \draw[red!10!gray]
            node[state,left=2*\MyDistance of Q1](TEST){$test$}
            (TEST) edge[test,sloped] node(NODE-NAME){node \\ sloped} (Q0)
            (TEST) edge[test,pos=0.25,below,sloped] node{node \\ pos=0.25 \\ sloped below} (Q0)
            (TEST) edge[test,pos=0.75,above,sloped] node{node \\ pos=0.75 \\ sloped abobe} (Q0);
        \draw[blue!10!gray]
            node[state,below=1.5*\MyDistance of TEST, font=\scriptsize](TEST2){$test_2$}
            (TEST2) edge[test] node[
                draw,fill, inner sep=0,
                label={[draw,red,label distance=4mm]0:label 0°},
                label={[draw,blue,label distance=4mm]45:label 45°},
                label={[draw,green,label distance=4mm,scale=0.5]90:label 90°},
                label={[draw,red,label distance=4mm]135:label 135°},
                label={[draw,red,label distance=4mm,scale=0.75]180:label 180°},
                label={[draw,circle,orange,label distance=4mm,scale=0.5]-45:label -45°},
                label={[draw,purple,label distance=4mm]-90:label -90°},
                label={-135:label -135°}
                ]{labeled \\ node}(NODE-NAME);
    \end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .