# Trying to draw a picture of a Turing Machine in TexMaker using Tikz

Again I have to call on your expertise to help me draw a picture. This time it's for a Turing Machine. I have two problems with the self loops and one with the general spacing.

The first problem concerning self loops is the fact that I want to replicate this way of having multiple lines of input on one self loop:

I already tried using \\ and /newline but it doesn't work. Having multiple inputs on one self loop would also fix the problem of overlapping at q5.

The second problem concerning self loops is that at q2 the self loop goes straight through the connection to q6.

The problem I have with general spacing is that I would like to have q5 one space more to the right, but using right right as its position relative to q2 doesn't work.

MWE:

\documentclass[a4paper,twoside,11pt]{article}
\usepackage{tikz, wasysym}
\usetikzlibrary{automata,positioning}

\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=4cm,on grid,auto]
\node[state,initial] (0) {$q_0$};
\node[state] (1) [right=of 0] {$q_1$};
\node[state] (2) [right=of 1] {$q_2$};
\node[state] (3) [above=of 2] {$q_3$};
\node[state] (4) [right=of 3] {$q_4$};
\node[state] (5) [right=of 2] {$q_5$};
\node[state] (6) [below=of 2] {$q_6$};
\node[state] (7) [right=of 6] {$q_7$};
\path[->]
(0) edge                    node {$B\:B\:R$} (1)
(1) edge [loop above]       node {$0\:0\:R$} (1)
(1) edge [loop below]       node {$1\:1\:R$} (1)
(1) edge                    node {$E\:E\:L$} (2)
(2) edge [loop below]       node {$*\:*\:L$} (2)
(2) edge                    node {$1\:*\:R$} (3)
(2) edge                    node {$0\:*\:R$} (6)
(3) edge [loop above]       node {$*\:*\:R$} (3)
(3) edge                    node {$E\:E\:R$} (4)
(4) edge [loop above]       node {$0\:0\:R$} (4)
(4) edge [loop right]       node {$1\:1\:R$} (4)
(4) edge                    node {$\Box\:1\:L$} (5)
(6) edge [loop below]       node {$*\:*\:R$} (6)
(6) edge                    node {$E\:E\:R$} (7)
(7) edge [loop below]       node {$0\:0\:R$} (7)
(7) edge [loop right]       node {$1\:1\:R$} (7)
(7) edge                    node {$\Box\:0\:L$} (5)
(5) edge [loop right]       node {$0\:0\:L$} (5)
(5) edge [loop right]       node {$1\:1\:L$} (5)
(5) edge                    node {$E\:E\:L$} (2);
\end{tikzpicture}

\end{document}

• 1) How is the first image related to your question? 2) That you use TexMaker is not relevant 3) Is that question, regarding line break in tikz nodes, helpful? It basically amount to write node[text width=1cm]{$\Box\:0\:L$\\$\Box\:1\:R$} to get two lines in the same node. – Clément Nov 20 '15 at 21:49
• The first image is my current output. I refer to the image in the text. I tried using "\\" to get multiple lines of input on one loop but that didn't work. – Carst Nov 20 '15 at 21:51
• Add [text width=1cm] as a parameter of your node. – Clément Nov 20 '15 at 21:52

First, you can stack two lines of text onto a loop's label in a few ways. If you have package amsmath loaded, you can write node {$\substack{line 1\\line 2}$}, which typesets them both smaller. If you want your label to be the same size as normal text, use node {$\begin{matrix} line 1 \\ line 2 \end{matrix}$}.

To get node q_5 farther to the right, you can specify its position as \node[state] (5) [right=6cm of 2] {$q_5$};.

Finally, to avoid the self-loop from clashing with an arrow, specify its direction as follows: edge [in=30, out=60, loop] (the in and out values represent degrees around the circle).

Here's the final code.

\documentclass[a4paper,twoside,11pt]{article}
% needed for the matrix envirionment
\usepackage{amsmath}
\usepackage{tikz, wasysym}
\usetikzlibrary{automata,positioning}

\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=4cm,on grid,auto]
\node[state,initial] (0) {$q_0$};
\node[state] (1) [right=of 0] {$q_1$};
\node[state] (2) [right=of 1] {$q_2$};
\node[state] (3) [above=of 2] {$q_3$};
\node[state] (4) [right=of 3] {$q_4$};
% push this node further to the right
\node[state] (5) [right=6cm of 2] {$q_5$};
\node[state] (6) [below=of 2] {$q_6$};
\node[state] (7) [right=of 6] {$q_7$};
\path[->]
(0) edge                    node {$B\:B\:R$} (1)
(1) edge [loop above]       node {$\begin{matrix}0\:0\:R\\1\:1\:R\end{matrix}$} (1)
(1) edge [loop below]       node {$1\:1\:R$} (1)
(1) edge                    node {$E\:E\:L$} (2)
(2) edge [in=30, out=60, loop]       node {$*\:*\:L$} (2)
(2) edge                    node {$1\:*\:R$} (3)
(2) edge                    node {$0\:*\:R$} (6)
(3) edge [loop above]       node {$*\:*\:R$} (3)
(3) edge                    node {$E\:E\:R$} (4)
(4) edge [loop above]       node {$0\:0\:R$} (4)
(4) edge [loop right]       node {$1\:1\:R$} (4)
(4) edge                    node {$\Box\:1\:L$} (5)
(6) edge [loop below]       node {$*\:*\:R$} (6)
(6) edge                    node {$E\:E\:R$} (7)
(7) edge [loop below]       node {$0\:0\:R$} (7)
(7) edge [loop right]       node {$1\:1\:R$} (7)
(7) edge                    node {$\Box\:0\:L$} (5)
(5) edge [loop right]       node {$\begin{matrix}0\:0\:L\\1\:1\:L\end{matrix}$} (5)
(5) edge                    node {$E\:E\:L$} (2);
\end{tikzpicture}

\end{document}


• I only cut-off white space around image in wish to better appear in its beauty. Nice solution! – Zarko Nov 20 '15 at 22:25
• @Zarko: thanks a lot! That's quite helpful. – Arun Debray Nov 21 '15 at 0:26