# Drawing graph with Tikz: Link it with main text without overlapping with text

I'm trying to draw a graph in LaTeX with Tikz. Unfortunately, it is my first time using this package and I'm not too familiar with it. I've used the example from this website: http://www.texample.net/tikz/examples/state-machine/

But somehow, there are still some things I cannot figure out.

My problem is now:

1. SOLVED I want to remove the arrow going into the first node (A). I have removed the word "start", but I don't know what to do with the arrow itself.

2. SOLVED (Problem was solved by putting the label at either the start or the end of the arrow, otherwise there was an error.) I need two edges between (D) and (G). Somehow, there is always an error.

3. I am trying to put this "picture" into my main text, a continuous text. But, I can't put it into the text, with the text being above and below this picture. I've tried to add the Tikz picture with "figure" and "center", but the Tikz picture was always somewhere floating around.

My code:

\documentclass[12pt,a4paper,twoside]{article}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}

\setlength{\voffset}{-28.4mm}
\setlength{\hoffset}{-1in}
\setlength{\topmargin}{20mm}
\setlength{\oddsidemargin}{25mm}
\setlength{\evensidemargin}{25mm}
\setlength{\textwidth}{160mm}

\setlength{\parindent}{0pt}

\setlength{\textheight}{235mm}
\setlength{\footskip}{20mm}

\begin{document}
\pagestyle{empty}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\begin{figure}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm, semithick,initial text=,overlay,>=latex]
\tikzstyle{every state}=[fill=lightgray,draw=none,text=black]

\node[state](A)                     {$P_{000}$};
\node[state](B)     [right of=A]    {$P_{010}$};
\node[state](C)     [above of=B]    {$P_{001}$};
\node[state](D)     [below of=B]    {$P_{100}$};
\node[state](E)     [right of=B]    {$P_{101}$};
\node[state](F)     [below of=E]    {$P_{110}$};
\node[state](G)     [above of=E]    {$P_{011}$};
\node[state](H)     [right of=E]    {$P_{111}$};

\path (A) edge [bend angle = 10, bend left]   node {$\beta_{A_2}a$} (C)
edge [bend angle = 10, bend left]   node {$\beta_{H}h$}   (B)
edge [bend angle = 10, bend left]   node {$\beta_{A_1}a$} (D)
(B) edge [bend angle = -10, bend right] node {$\alpha_H$} (A)
edge [bend angle = 10, bend left]   node[very near start] {$\beta_{A_2}a$} (G)
edge [bend angle = 10, bend left]   node[very near start] {$\beta_{A_1}a$} (F)
(C) edge [bend angle = -10, bend right] node {$\alpha_{A_2}$}                 (A)
edge [bend angle = 10, bend left]   node {$\beta_{A_1}a$}                 (G)
edge [bend angle = 10, bend left]   node[very near end] {$\beta_{H}h$}    (E)
(D) edge [bend angle = -10, bend right] node[near start] {$\alpha_{A_1}$} (A)
edge [bend angle = 10, bend left]   node {$\beta_{H}h$} (F)
edge [bend angle = 10, bend left]   node[very near end] {$\beta_{A_2}a$} (E)
(E) edge [bend angle = -10, bend right] node[very near start] {$\alpha_H$} (C)
edge [bend angle = -10, bend right] node[very near start] {$\alpha_{A_2}$} (D)
edge [bend angle = 10, bend left]   node {$\beta_{A_1}a$} (H)
(F) edge [bend angle = -10, bend right] node {$\alpha_H$} (D)
edge [bend angle = -10, bend right] node[very near end] {$\alpha_{A_1}$} (B)
edge [bend angle = 10, bend left]   node {$\beta_{A_2}a$} (H)
(G) edge [bend angle = -10, bend right] node {$\alpha_{A_1}$} (C)
edge [bend angle = 10, bend left]   node {$\beta_{H}h$}               (H)
edge [bend angle = -10, bend right] node[very near end] {$\alpha_{A_2}$} (B)
(H) edge [bend angle = -10, bend right] node {$\alpha_{H}$}               (G)
edge [bend angle = -10, bend right] node{$\alpha_{A_1}$} (E)
edge [bend angle = -10, bend right] node{$\alpha_{A_2}$} (F)
;
\end{tikzpicture}
\end{figure}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\end{document}


The first screenshot is with \begin{figure} \end{figure} and the second screenshot is with \begin{center} \end{center}.

• In order to remove the arrow, just remove initial from \node[initial,state] (A) . The other request I don't understand. – marmot Jan 22 '18 at 16:58
• Thank you! The arrow is removed. The second request is that I need another edge between (D) and (G). Somehow, I cannot add one. There is always an error – QuestionCookie Jan 22 '18 at 17:11

I do not get an error but an ugly output.

\documentclass{article}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}

\begin{document}
\begin{tikzpicture}[->,shorten >=1pt,auto,node distance=4cm,
semithick,initial text=,overlay,>=latex]
\tikzstyle{every state}=[fill=lightgray,draw=none,text=black]

\node[state] (A)                      {$P_{000}$};
\node[state]         (B)  [right of=A]        {$P_{100}$};
\node[state]         (C)  [above of=B]        {$P_{001}$};
\node[state]         (D)  [below of=B]        {$P_{010}$};
\node[state]         (E)  [right of=B]        {$P_{101}$};
\node[state]         (F)  [below of=E]        {$P_{110}$};
\node[state]         (G)  [above of=E]        {$P_{011}$};
\node[state]         (H)  [right of=E]        {$P_{111}$};

\path (A)     edge [bend angle = 10, bend left]   node {$\beta_{A_2}a$}(C)
edge [bend angle = 10, bend left]   node {$\beta_{H}h$}  (B)
edge [bend angle = 10, bend left]   node {$\beta_{A_1}a$}(D)
(B)     edge [bend angle = -10, bend right] node {$\alpha_H$}    (A)
edge [bend angle = 10, bend left]   node {$\beta_{A_2}a$}(E)
edge [bend angle = 10, bend left]   node {$\beta_{A_1}a$}(F)
(C)     edge [bend angle = -10, bend right] node {$\alpha_{A_2}$}(A)
edge [bend angle = 10, bend left]   node {$\beta_{A_1}a$}(G)
edge [bend angle = 10, bend left]   node {$\beta_{H}h$}  (E)
(D)     edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(A)
edge [bend angle = 10, bend left]   node {$\beta_{H}h$}  (F)
%edge [bend left, out=180,in=270]   node {$\beta_{A_2}a$}       (G) %ERROR
(E)     edge [bend angle = -10, bend right] node {$\alpha_H$}    (C)
edge [bend angle = -10, bend right] node {$\alpha_{A_2}$}(B)
edge [bend angle = 10, bend left]   node {$\beta_{A_1}a$}(H)
(F)     edge [bend angle = -10, bend right] node {$\alpha_H$}    (D)
edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(B)
edge [bend angle = 10, bend left]   node {$\beta_{A_2}a$}(H)
(G)     edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(C)
edge [bend angle = 10, bend left]   node {$\beta_{H}h$}  (H)
(H)     edge [bend angle = -10, bend right] node {$\alpha_{H}$}  (G)
edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(E)
edge [bend angle = -10, bend right] node {$\alpha_{A_2}$}(F)
;
\path (D)     edge [bend angle = 10, bend left]  (G);
\path (G) edge [bend angle = 10, bend left] (D);
\end{tikzpicture}
\end{document}


Like @marmot, the code compiles for me but does not look great because the missing edges dominate the picture. One way out is to make the missing edges dashed and gray, and playing with the bend angle, so that you get:

I'd make a few other changes:

• you have specified two different arrow heads by giving >=stealth' and >=latex. As the second specification "wins" I have dropped the stealth arrow
• as the edges are all very similar I'd put them in a \foreach loop, where you give the end points and the labels as the "arguments" to the loop. This simplifies the code and makes it easier to read and understand. In addition, if you draw the edges using individual \path statements then they all use bend angle=10, bend left, so less thought is required. The edges from (D) to (G) need to be done separately as they are dashed.

Here's the modified code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}

\begin{document}

\begin{tikzpicture}[->,shorten >=1pt,auto,node distance=4cm,
semithick,initial text=,>=latex]
\tikzstyle{every state}=[fill=lightgray,draw=none,text=black]

\node[initial,state] (A)                      {$P_{000}$};
\node[state]         (B)  [right of=A]        {$P_{100}$};
\node[state]         (C)  [above of=B]        {$P_{001}$};
\node[state]         (D)  [below of=B]        {$P_{010}$};
\node[state]         (E)  [right of=B]        {$P_{101}$};
\node[state]         (F)  [below of=E]        {$P_{110}$};
\node[state]         (G)  [above of=E]        {$P_{011}$};
\node[state]         (H)  [right of=E]        {$P_{111}$};

% loop through and draw the edges
\foreach \X/\Y/\A/\a in {
A/C/A_2/a,%  000 <-> 001
C/G/A_1/a,%  001 <-> 011
G/H/H  /h,%  011 <-> 111
C/E/H  /h,%  001 <-> 101
A/B/H  /h,%  000 <-> 100
B/E/A_2/a,%  100 <-> 101
E/H/A_1/a,%  101 <-> 111
A/D/A_2/a,%  000 <-> 010
B/F/A_1/a,%  100 <-> 110
D/F/H  /h,%  100 <-> 110
F/H/A_2/a%   100 <-> 110
}{
\path (\X) edge[->, bend angle=10, bend left]
node{$\beta_{\A}\a$}(\Y);
\path (\Y) edge[->, bend angle=10, bend left]
node{$\alpha_{\A}$}(\X);
}
\path[dashed,gray] (D) edge[->, bend angle=22, bend left]
node{$\beta_{A_2}a$}(G);
\path[dashed,gray] (G) edge[->, bend angle=22, bend left]
node{$\alpha_{A}$}(D);

\end{tikzpicture}
\end{document}


Unfortunately, it does not seem to be possible to choose a bend angle so that you avoid all of the other labels, but I thought bend angle=22 looked OK. The other option would be to put these edges "outside" of the picture, but I didn't like the look of this either.

• Thank you for your modification, it looks great! I've edited my post, unfortunately, I haven't seen your answer before doing it. I'll probably take your code, because I love the idea of those intersecting edges to be dashed and gray. Do you have some time to help me with me (now added) third problem? – QuestionCookie Jan 22 '18 at 19:02
• @QuestionCookie If you use the tikzpicture environment by itself it will not float anywhere. Probably you want to put it inside of \begin{center}...\end{center}. You may need to adjust the node distance to get the picture to fit and possibly add something like scale=0.7. – Andrew Jan 22 '18 at 19:37
• Thank you, I got it now. Mine was overlapping because of "overlay". I forgot to remove it. – QuestionCookie Jan 22 '18 at 19:52
• @QuestionCookie Ah, yes, I had meant to say that I took out the overlay specification in my post.... Your rearranging of the nodes is a better approach. – Andrew Jan 23 '18 at 8:37