How to: Embed dot graphs (graphviz) in LaTeX

Dot graphs (from GraphViz) are known for their easiness in expressing complex graphs in a plain text format and generating those graphs file into static images. For example, look at the following dot graph:

digraph Example {

node [shape="box",color="blue"]; q_0 q_1 q_2 q_3 q_4 q_6 q_7 q_8 q_9 q_10;
node [shape="circle",color="black"]; q_5;

q_0 -> q_1 [label="1"];
q_1 -> q_2 [label="1"];
q_2 -> q_3 [label="1"];
q_3 -> q_4 [label="1"];
q_4 -> q_5 [label="1"];
q_5 -> q_5 [label="0, 1"];

q_0 -> q_6 [label="0"];
q_1 -> q_7 [label="0"];
q_2 -> q_8 [label="0"];
q_3 -> q_9 [label="0"];
q_4 -> q_10 [label="0"];

q_6 -> q_7 [label="0, 1"];
q_7 -> q_8 [label="0, 1"];
q_8 -> q_9 [label="0, 1"];
q_9 -> q_10 [label="0, 1"];

q_10 -> q_0 [label="0, 1"];

A graph starts with “graph” or “digraph”, followed by its name. Properties of the graph are wrapped in a pair of accolades. In this example, the first property is “rankdir=LR“, which sets the orientation of the graph to left-to-right (instead of the default top-to-bottom). Line four and five define the appearance of each node used in the graph. At the end of this dot graph is a list of relations between the defined nodes. Below is the generated image of this graph.

automaton generated by GraphViz' dot graphs

automaton generated by GraphViz' dot graphs

On Debian(-based) distributions, you can install the required packages with the following command:

sudo apt-get install graphviz dot2tex gedit-latex-plugin

Configuring Gedit’s LaTeX plugin

Enable GeditLaTeXPlugin in Gedit using the menu Edit → Preferences. At the Preferences dialog, click on the tab “Plugins”, scroll down to “Gedit LaTeX Plugin” and check its checkbox. Close the dialog and restart Gedit.

GeditLaTeXPlugin does not natively support embedded dot graphs, because rubber is used to process the LaTeX documents instead of pdflatex. To fix this, you should open the following file:

sudo gedit /usr/lib/gedit-2/plugins/GeditLaTeXPlugin/tools.xml

At line number 3 and 7, you will find:

<job mustSucceed="true" postProcessor="RubberPostProcessor">
  rubber --inplace --maxerr -1 --short --force --warn all --pdf "$filename"

Replace the lines at line number 3 and 7 with the following:

<job mustSucceed="true" postProcessor="RubberPostProcessor">
  pdflatex -shell-escape -src -interaction=nonstopmode "$filename"

Fixing dot2tex

At the bottom of Gedit is a so-called “buttom-panel” containing tabs for a python console, terminal and LaTeX output (its tab name is “tools”). The latter one lists errors occurred during the compilation of a LaTeX document. The dot2tex script uses the deprecated module “md5″, which should be replaced by the module “hashlib”. Otherwise, a lot of useless warnings are logged in the LaTeX output tab. Edit the following file to fix these warnings:

sudo gedit /usr/share/pyshared/dot2tex/

Scroll to line 2828 and replace the hashing code with the following lines of code:

import hashlib, cPickle
if len(args) == 1 and options.outputfile:'Caching enabled')
    inputfilename = args[0]
    # calculate hash from command line options and dotdata
    md5 = hashlib.md5()
    md5.update(dotdata + "".join(sys.argv))
    inputhash = md5.digest()

Embedding dot graphs

Finally, it is time to demonstrate embedding dot graphs in a LaTeX document:


% dot graphs

\author{Sander van Veen}
\title{Dot graphs in \LaTeX}



\begin{dot2tex}[dot,options=-tmath --autosize --cache]
digraph G {

node [shape="none"]; start ;

node [shape="box",color="blue"]; q_0;
node [shape="circle",color="black"]; q_1 q_2 q_3;

start -> q_0;

q_0 -> q_1 [label="1"];
q_1 -> q_0 [label="1"];

q_1 -> q_2 [label="0"];
q_2 -> q_1 [label="0"];

q_2 -> q_3 [label="1"];
q_3 -> q_2 [label="1"];

q_3 -> q_0 [label="0"];
q_0 -> q_3 [label="0"];


After “\begin{dot2tex}” are a couple of options shown used to alter the behaviour of dot2tex (-tmath substitutes for example _ with subscript, --autosize will reduce the dimension of the image and --cache will save the converted dot output). There are many possible options for dot2tex, which are documented in its manpage. Please note that dot2tex translates the dot format to tex (pstrikz to be exactly), and therefore does not support all features of dot.

Last but not least the output of the example LaTeX document: output PDF of embedded dot graph.

Tags: , , ,

Leave a Reply