[Tex/LaTex] How to add arrow from text to node

automatatikz-pgf

I am currently using Tikz in order to draw a finite automata. I would need to add an arrow from a bit of text to one of the nodes of the finite automata, as shown in the example.

I have tried created a node with "draw=none". However, this raises two problems. I am unable to write the text desired on two lines and further more, the bounding box for the node is created, although not displayed and thus, the arrow looks really bad, since the bounding box for such a long text is huge.

\documentclass[11pt]{report}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows, automata}

\begin{document}

(not important stuff)

\begin{tikzpicture}[->,>=stealth', shorten >=1pt, auto, node distance=2.8cm, semithick]
\node[state] (A)                    {$Alarm$};
\node[state] (B) [above of=A]       {$Reset$};
\node[state] (C) [above right of=A] {$S1$};
\node[state] (D) [right of=A]       {$S2$};
\node[state] (E) [below right of=A] {$S3$};
\node[state] (F) [below of=A]       {$S4$};
\node[state] (G) [left of=A]        {$Open$};
\node[statem, draw=none] (H) [above left of=B] {Reset input(from any state)};

\path (A) edge [loop left]  node {0, 1} (A)
      (B) edge [bend left]  node {1}    (C)
          edge              node {0}    (A)
      (C) edge [bend left]  node {0}    (D)
          edge              node {1}    (A)
      (D) edge [bend left]  node {1}    (E)
          edge              node {0}    (A)
      (E) edge [bend left]  node {1}    (F)
          edge              node {0}    (A)
      (F) edge [bend left]  node {0}    (G)
          edge              node {1}    (A)
      (G) edge [bend left]  node {0}    (B)
          edge [loop left]  node {1}    (G)
      (H) edge              node {}     (B);
\end{tikzpicture}

(more not important stuff)

\end{document}

PS Knowing how to move the arrows to the middle of the edge would also be appreciated(at the present time they are at the end of the edge).

example

Best Answer

Here's one possibility using the automata library and a decoration to place the arrows in the middle of the edges:

enter image description here

The code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,decorations.markings,positioning,arrows}

\begin{document}

\begin{tikzpicture}[
midarr/.style 2 args={
  decoration={
    markings,
    mark=at position 0.5 with {\arrow[xshift=3.333pt]{triangle 45} \node[#1] {#2};},
  },
    postaction=decorate,
},
state/.append style={minimum size=35pt},
node distance=1.3cm and 1.3cm
]
\node[state]
  (open) {Open};
\node[state,right=of open]
  (alarm) {Alarm};
\node[state,right=of alarm]
  (s1) {S1};
\node[state,above=of alarm,xshift=10pt]
  (reset) {Reset};
\node[state,below=of alarm,xshift=10pt]
  (s2) {S2};
\node[left=of reset,align=right,yshift=20pt]
  (resetany) {Reset input\\ (from any state)};
\draw[-triangle 45]
  (resetany.east) -- (reset);

\draw[midarr={right=2pt}{1}]
  (s2) -- (alarm);  
\draw[midarr={right=2pt}{0}]
  (reset) -- (alarm);  
\draw[midarr={below=1pt}{0}]
  (s1) -- (alarm);  
\draw[midarr={below left}{0}]
  (s2) to[bend left] (open);  
\draw[midarr={above left}{0}]
  (open) to[bend left] (reset);  
\draw[midarr={above right}{1}]
  (reset) to[bend left] (s1);  
\draw[midarr={below right}{1}]
  (s1) to[bend left] (s2);
\draw[midarr={left}{$0,1$}]
  (alarm) to[out=230,in=130,looseness=2.5] (alarm);
\draw[midarr={left}{1}]
  (open) to[out=230,in=130,looseness=2.5] (open);
\end{tikzpicture}

\end{document}

There's an initial style (refer to the documentation), but I opted for a simple \node with some attributes to place the "Reset input (from any state)" text.

And here's a modified version for the code in the question, using the same ideas as before:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,decorations.markings,positioning,arrows}

\begin{document}

\begin{tikzpicture}[
midarr/.style 2 args={
  decoration={
    markings,
    mark=at position 0.5 with {\arrow[xshift=3.333pt]{stealth'} \node[#1] {#2};},
  },
    postaction=decorate,
},
state/.append style={minimum size=35pt},
node distance=1.6cm and 1.6cm,
semithick,
>=stealth',
shorten >=1pt
]
\node[state]
  at (0,0)
  (alarm) {Alarm};
\node[state,left=of alarm]
  (open) {Open};
\node[state]
  at (45:2.8cm)
  (s1) {S1};
\node[state,right=of alarm]
  (s2) {S2};
\node[state,above=of alarm]
  (reset) {Reset};
\node[state]
  at (-45:2.8cm)
  (s3) {S3};
\node[state,below=of alarm]
  (s4) {S4};

\draw[midarr={right=2pt}{0}]
  (reset) -- (alarm);  
\draw[midarr={below right=1pt}{1}]
  (s1) -- (alarm);  
\draw[midarr={below=1pt}{0}]
  (s2) -- (alarm);  
\draw[midarr={below left}{0}]
  (s3) -- (alarm);  
\draw[midarr={left=2pt}{1}]
  (s4) -- (alarm);  
\draw[midarr={below left}{0}]
  (s4) to[bend left] (open);  
\draw[midarr={above left}{0}]
  (open) to[bend left] (reset);  
\draw[midarr={above right}{1}]
  (reset) to[out=0,in=132] (s1);  
\draw[midarr={right=2pt}{0}]
  (s1) to[out=-45,in=90] (s2);  
\draw[midarr={right=2pt}{1}]
  (s2) to[out=-90,in=45] (s3);  
\draw[midarr={below right}{1}]
  (s3) to[out=-135,in=0] (s4);  
\draw[midarr={left}{$0,1$}]
  (alarm) to[out=230,in=130,looseness=2.5] (alarm);
\draw[midarr={left}{1}]
  (open) to[out=230,in=130,looseness=2.5] (open);

\node[left=of reset,align=right,yshift=30pt,xshift=10pt]
  (resetany) {Reset input\\ (from any state)};
\draw[-stealth']
  (resetany.east) -- (reset);
\end{tikzpicture}

\end{document}

enter image description here