Combining Graphviz (Dot) and TikZ With Dot2tex

| Comments

We all want to create good looking documents and good looking documents need good looking images. Because we want consistency and because we are lazy we want to do this as automatic as possible. That is why we use LaTeX, it creates beautifully typeset documents without much manual effort.

Similarly, we use graphviz to generate our graphs for us. It’s automatic layout is the best in the field and the (declarative) dot language is easy to understand and compact to write. We can either include the PDFs dot generated in our document by using \includegraphics or we could use the latex graphviz package, remember that we are lazy. We can easily get the image in our first example in our PDF.

Image with graphviz
Figure 1. Example of an image generated by Graphviz/dot

There is a shadow side to using Graphviz/dot as well. There are two problems. Firstly, the image just looks a bit out of place around the nicely smoothed text in a PDF. Secondly, we lack the ability to use TeX code in our graph. This means we are limited to the formatting by dot and the graphs could therefore appear out of style with other figures in our document.

No worries, with TikZ it is possible to create very fancy graphs and images in general but you have to all the positioning manually! Imagine inserting a node and having to reorder everything!

Enter dot2tex it brings all the love of graphviz/dot to TeX/TikZ. Using dot2tex has many advantages:

  1. Lets you write your graphs in familiar dot syntax;
  2. Let dot – or whichever layout engine you prefer – determine the placement of your nodes and arrows;
  3. Style your nodes however you want by using TikZ styles;
  4. Optionally, fine-tune the graph by adding extra tikz drawings.

Rather than manually calling dot2tex for every dot file you have please use the dot2texi package. This is the interface to dot2tex and when used as follows generates the image as displayed in Figure 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  \begin{tikzpicture}[>=latex',scale=0.5]
    % set node style

    \begin{dot2tex}[dot,tikz,codeonly,styleonly,options=-s -tmath]
        digraph G  {
            node [style="n"];
            p [label="+"];
            t [texlbl="\LaTeX"];
            6
            8
            10-> p;
            6 -> t;
            8 -> t;
            t -> p;
            {rank=same; 10;6;8}
        }
    \end{dot2tex}.
    \begin{pgfonlayer}{background}
        \draw[rounded corners,fill=blue!20] (6.north west) -- (8.north east) -- (t.south east)--cycle;
    \end{pgfonlayer}
\end{tikzpicture}
Example of TeX typesetting + TikZ background
Figure 2. Example of TeX typesetting + TikZ background

For more TikZ goodness check out the example site.

Happy writing!

Comments