[Tex/LaTex] Change bend angle and swap node position on arrow

automatatikz-pgf

I'm trying to draw an automata as follows:

\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=2.8cm]
  \node[initial,state] (q1)      {$1$};
  \node[state]         (q2) [right of=q1]  {$2$};
  \node[state] (q4) [below right of=q2] {$4$};
  \node[state, accepting]         (q3) [above right of=q4] {$3$};

  \path[->]          (q1)  edge                 node {a} (q2);
  \path[->]          (q2)  edge   [bend left]   node {b} (q3);
  \path[->]          (q3)  edge   [bend left]   node {a} (q2);
  \path[->]          (q2)  edge                 node {b} (q4);
  \path[->]          (q4)  edge                 node {a} (q3);
\end{tikzpicture}

However, the bending seems to be "too much", and it interferes with the two arrows to/from q4. In addition, I'd like the labels on those two arrows to be on the other side of the arrow. Sadly I couldn't find how to do it online so I turned to this page.

Best Answer

You can influence the degree of bending using the optional parameter of bend left and bend right. The default is bend left=30, which means that the connecting line leaves and enters the nodes at an angle of 30 degrees to the direct connecting line. bend left=20 would be a more gentle curve. If you want all your curves to curve by the same amount without explicitly specifying it each time, you can add bend angle=20 to your tikzpicture options.

To swap the nodes on the lines to the other side, simply add the key [swap] to the node options.

\documentclass{article}

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


\begin{document}

\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=2.8cm]
  \node[initial,state] (q1)      {$1$};
  \node[state]         (q2) [right of=q1]  {$2$};
  \node[state] (q4) [below right of=q2] {$4$};
  \node[state, accepting]         (q3) [above right of=q4] {$3$};

  \path[->]          (q1)  edge                 node {a} (q2);
  \path[->]          (q2)  edge   [bend left=20]   node {b} (q3);
  \path[->]          (q3)  edge   [bend left=20]   node {a} (q2);
  \path[->]          (q2)  edge                 node [swap] {b} (q4);
  \path[->]          (q4)  edge                 node [swap] {a} (q3);
\end{tikzpicture}


\end{document}