Begin main content

Embedding GraphViz in LaTeX documents

I have to say I'm loving using LaTeX for my Uni papers. It is so delightfully straightforward and powerful.

I am embedding some GraphViz diagrams in my documents, but I didn't want a round trip to a different editor buffer and the shell to edit/compile/insert the diagram.

Based on graphviz.tex code by Derek Rayside I have created a custom LaTeX function that will automatically compile and embed inline GraphViz code (man those wacky capitilisations are getting boring!)

You embed a graph like so:

   \digraph[scale=0.5]{MyGraph}{rankdir=LR; a->b; b->c}
Be careful not to have any double newlines or your dot file will have some bogus \par comments in it (which I should replace with newlines in the function...). Another possible enhancement would be to add a dynamic dot command to set the maximum diagram height & width to the current page/column dimensions.

You need my graphviz.sty and you need the following in your document preamble:

Remember to run texhash if you install the style in a system-wide location. Note this assumes pdflatex. Modifying the command for regular latex is an exercise left for the reader. Update: Derek now has an updated version available on GitHub (which didn't exist in 2007 :).

08:09 AM, 25 Mar 2007 by Mark Aufflick Permalink


Change this style to use the excellent dot2tex:

by Mark Aufflick on 05/21/07


Awesome! This was very useful for me.

by Unregistered Visitor on 02/13/09

Thank you

I really like this, and I fiddled around with dot2tex as suggested in your comment to yourself. Here are my results. I've never written a latex sty file before, so please feel free to improve it.

% graphviz.sty
% by Mark Aufflick
% 2006-03-25
% based on graphviz.tex by Derek Rayside 2003

% add the following lines to your preamble:

% \usepackage[pdftex]{graphicx}
% \usepackage{graphviz}

% parameters to \digraph:
% 1 - parameters for \includegraphics (optional; default value is "scale=1")
% 2 - name of the digraph
% 3 - body of the digraph

% assumes pdflatex. to modify this command for regular latex,
% replace all .pdf with .ps, and the command becomes simply:
% dot -Tps


  \immediate\write\dotfile{digraph #1 {\string#2}} 
  \write18{bash -c "`which neato` -Txdot | `which dot2tex` -tmath --figonly --usepdflatex -ftikz --tikzedgelabels --styleonly -o #1tikz.tex"}  
  % the tex exists: include it 
  { \include{#1tikz} } 
  % the tex was not created - show a hint
  { \fbox{ \begin{tabular}{l} 
        The file \texttt{#1tikz.tex} hasn't been created from 
        \texttt{} yet. \\
        We attempted to create it with:\\
        `\texttt{neato -Txdot | dot2tex  --figonly --usepdflatex -ftikz --tikzedgelabels --styleonly -o #1tikz.tex}` \\
        but that seems not to have worked. You need to execute `\texttt{pdflatex}` with \\
        the `\texttt{-shell-escape}` option.

by Unregistered Visitor on 04/02/09

Something to be wary of

You should not have the \newwrite directive in your \newcommand. If you do and use it a lot you will exhaust the write buffers available. Just move that command out of the \newcommand block. Like: \newwrite\dotfile \newcommand{\digraph}[4][scale=1]{ \immediate\openout\ etc.

by Unregistered Visitor on 10/06/10

Updated version on Github

For an updated version of GraphViz.sty see Mike Prentice's Github version:

by Mark Aufflick on 02/23/11

Filenames with hyphens

If you want filenames with hyphens in them, put quotes around #2 in the following line: \immediate\write\dotfile{digraph #2 {\string#3}}

by Unregistered Visitor on 04/02/13

Accents and labels with spaces

Very nice functionality! But how do you prevent PDFLaTeX from introducing strange codes when using spaces and accented characters in, e.g. label? Example: try the following \digraph[scale=0.5]{MyGraph}{rankdir=LR; a [label="This is A"]; a->b; b->c} If I drop the double quotes there will be three nodes instead of one, and any replacement of the quotes by single quotes or braces, will result in strange nodes labeled with LaTeX items from when it processes the box; just like when the label string contains accented characters. Solving this would, for my use, make graphviz.sty full featured. Best regards, Morten Lind.

by Unregistered Visitor on 06/27/13

Updated version on Github

I just uploaded a newer version of this to Github: cheers, Derek.

by Unregistered Visitor on 08/15/13


I will be embedding a number of GraphViz diagrams during my papers, however We don't wish some sort of round excursion to another editor buffer and the shell to help revise compile put your diagram. Excellent important articles you're revealing for all of us. custom dissertation writing services

by Unregistered Visitor on 04/25/14


Dot graphs are known for its easiness in expressing a complex and difficult graphs by using only a plain text to generate codes. In order to generate codes, first use the GeditLaTeXPlugin in Gedit. Fixing comes after it and later on the embedding has to be accomplished. for more details

by Unregistered Visitor on 05/10/14

First of all the photo widget is dynammically written by a javascript ajax callback

First of all the photo widget is dynammically written by a javascript ajax callback.

by Unregistered Visitor on 05/21/14

Great article

Thanks for such an awesome post. I have found it on this website and now I am just loving it here! Your site is full of awesome stuff. Thanks for all your efforts!

by Unregistered Visitor on 06/04/14

Great post

Thanks for this publication. I have found it on this web site and now I am loving it here. You are awesome. Keep them coming in the future mate!

by Unregistered Visitor on 06/23/14

Daily Updated News

That time i am going to introduce you with the network of online news and social media. I think its really awesome for all of us to work on that category because we have to provide all breaking news today and other latest news to our valued customer from all over the world. For that purpose we have to do something special and we need a well trained and highly committed team to bring positive results.

by Unregistered Visitor on 07/15/14

Casio montre calculatrice n'est pas conçu pour contrôler votre conjoint n'a pas été appelé à la même fonction, différentes personnes prennent un autre emploi ou de participer à diverses activités, et presque chacun d'entre eux pour ceux qui aiment courir, faire du sport ou des sports nautiques, vous voulez regarder, vous pouvez utiliser pendant une longue période, de sorte qu'ils puissent faire le porteur de prendre une variété d'activités, ce qui en fait bénéfique pour regarder les gens ont beaucoup de breitling montres, qui ont leurs dessins animés pour attirer les enfants sur le marché

by marsiagarita marsiagarita on 07/19/14

Add comment