88

I'm looking for a LaTeX package which helps drawing automata problems. I googled and found A package for drawing automata and graphs (Version 0.4). But I'm not sure is this package a standard one, or there are some better alternatives. Any suggestion?

roxrook
  • 9,907

3 Answers3

114

TiKZ has a whole library for drawing automata:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto] 
   \node[state,initial] (q_0)   {$q_0$}; 
   \node[state] (q_1) [above right=of q_0] {$q_1$}; 
   \node[state] (q_2) [below right=of q_0] {$q_2$}; 
   \node[state,accepting](q_3) [below right=of q_1] {$q_3$};
    \path[->] 
    (q_0) edge  node {0} (q_1)
          edge  node [swap] {1} (q_2)
    (q_1) edge  node  {1} (q_3)
          edge [loop above] node {0} ()
    (q_2) edge  node [swap] {0} (q_3) 
          edge [loop below] node {1} ();
\end{tikzpicture}
\end{document}  

output of code

Alan Munn
  • 218,180
  • 2
    I love that picture ^_^! Very nice and clear, thanks a lot. – roxrook Jun 15 '11 at 10:24
  • 3
    I second @Alan on Tikz. I used vaucanson-g for some time and IMHO it lacks lots of features in which Tikz is quite trivial to achieve. The Tikz manual has great examples on how to tweak automata drawings. – Paulo Cereda Jun 15 '11 at 11:02
  • I tried this example, and in the output file there isn't the automata. What i have in the output document is the string start, where letters s and t are covered with a scribble. Why i get this result? – optimusfrenk Mar 21 '14 at 08:46
  • @frenk I would suspect this is due to using a DVI previewer. Does the PDF file created look the same way if you open it in a PDF previewer? – Alan Munn Mar 21 '14 at 22:19
9

using xy-pic package available at ctan for automata diagrams. enter image description here

\documentclass[12pt]{article}
\usepackage[all]{xy}
\begin{document}
\xymatrix@ur@!R=2pc{%
*+<1pc>[o][F-]{q_0}  \ar@(l,l)[]^<<<<{start} \ar@/^/[r]^0  \ar@/_/[d]_1 
& *+<1pc>[o][F-]{q_1} \ar@(ul,ur)[]^{0}  \ar@/^/[d]^1 \\
*+<1pc>[o][F-]{q_2} \ar@(dr,dl)[]^{1} \ar@/_/[r]_0 
& *+<1pc>[o][F=]{q_3} }
\end{document}
Moriambar
  • 11,466
  • 9
    I've got to be honest: that is the most Perl-y syntax I've ever seen from TeX. I know this is an old post, but could you explain the syntax a little bit if you have time? :) – Sean Allred Jun 26 '14 at 01:11
  • 5
    @SeanAllred To be frank with you, even i don't remember the syntax, it's highly abstract unlike tikz, I was trying to practice at that time of posting answer. For a quick learning you may refer to this nice presentation. http://math.arizona.edu/~swig/documentation/xypic/Xypic.pdf and more detailed at userguide – texenthusiast Jun 30 '14 at 00:32
8

If you want great diagrams without having to write the code for it, check out this tool:

http://madebyevan.com/fsm/.

You can make your diagram by clicking and dragging, then export it as LaTeX (it uses TiKZ, just like the answer by @Alan Munn).

  • I like this tool, and would use it, but it doesn't seem to be able to write the text/input in the middle of the transition arrow? – delrocco Feb 03 '19 at 05:37