39

I am trying to generate a a graph with dot/graphviz. Is it possible to include it within *TeX and be compiled at *TeX compile time?

I've read about TikZ, but it's not the same syntax, is it possible to translate a dot file to tikz?

1
  • Its easy with the graphviz package. See some working examples here. Jan 20, 2018 at 18:08

6 Answers 6

19

You can use dot2tex, which is available from CTAN.

3
  • 1
    I wil just add that I have found a package that interfaces with that the package is dot2texi and it developed by the same author. Mar 17, 2011 at 9:10
  • How? The examples look wrong. They are putting latex in dot not dot in latex.
    – mathtick
    Aug 31, 2019 at 13:27
  • how to do edges labels?
    – mathtick
    Aug 31 at 13:34
25

You can use the graphviz package.

\documentclass{standalone}
\usepackage[pdf]{graphviz}

\begin{document}
   \digraph{abc}{
      rankdir=LR;
      a -> b -> c;
   }
\end{document}

enter image description here

More examples here.

3
  • 4
    The package has not bee updated in 5 years. Are you sure it's the better one to use?
    – einpoklum
    Aug 24, 2018 at 20:54
  • 1
    @einpoklum Has graphviz updated with some important features? Apr 19, 2021 at 16:09
  • @asuka: I have no idea.
    – einpoklum
    Apr 19, 2021 at 16:17
6

Here's how you can do it using the dot2tex package:

\documentclass{minimal}
\usepackage[autosize]{dot2texi}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}
\begin{dot2tex}[neato,mathmode]
digraph G {
node [shape="circle"];
a_1 -> a_2 -> a_3 -> a_4 -> a_1;
}
\end{dot2tex}
\end{document}

However, this is an old package, and it's possible that other options are better (e.g. @user1491229's suggestion of the graphviz package).

1
  • At time of writing, the CTAN version is pretty out of date (2007), but the github and readthedocs versions got to 2.11.3 with a 2009 release, is python 3 compatible, etc.
    – Adam Burke
    Jun 14, 2022 at 1:31
4

The dot2tex package(s) seem(s) to be quite out of date now. I couldn't get it (them?) working on either of my Linux/TexLive or Windows/MikTeX build systems, so I rolled my own homebrew "gvdot" environment which is fairly simple and works on both. I think.

\RequirePackage{expl3}
\RequirePackage{moreverb}

% get title from tex filename
\typeout{Job name: \jobname}
\ExplSyntaxOn
\tl_set:NV \xjb \c_sys_jobname_str
\regex_replace_all:nnN{ ["] } { } \xjb
\regex_replace_all:nnN{ [\w\s]+- } { } \xjb
\ExplSyntaxOff
\typeout{Title: \xjb}

% strip spaces from title
\ExplSyntaxOn
\tl_set:NV \nsxjb \xjb
\tl_replace_all:Nnn \nsxjb { ~ } {  }
\ExplSyntaxOff
\typeout{Image Prefix: \nsxjb}

\NewDocumentEnvironment{gvdot}{ m m }{
    \noindent\ignorespaces{}
    % 1. write content to file
    \typeout{Writing file \nsxjb-#1.dot}
    % tab bug fixed with ``-8bit'' argument to xe
    % https://tex.stackexchange.com/questions/264461/xelatex-minted-code-block-represents-tabs-as-i?noredirect=1
    \immediate\verbatimwrite{\nsxjb-#1.dot}
}{
    \par\noindent\ignorespacesafterend{}
    \endverbatimwrite{}
    % 2. compile file with dot
    \typeout{This requires the --shell-escape option.}
    \immediate\write18{dot -Gdpi=288 -Tpng \nsxjb-#1.dot >\nsxjb-#1.png}
    % 288 is 72*4, 300 is not
    % 3. include compiled file here
    \typeout{Including generated image.}
    \includegraphics[scale=0.25,#2]{\nsxjb-#1}
}

Notice there are two arguments. #1 is the unique name of the graph. #2 is any other arguments you want to specify to includegraphics.

I'm using RequirePackage instead of usepackage because it's in a class file, I just copied it straight out with all the requisite bits (hopefully).

The moreverb package is for NewDocumentEnvironment. I needed that for its behaviour.

The jobname stuff could be stripped out if you think it's too complicated. I've got it in there for reasons but you could just go with argument #1.

You must specify the "-8bit" flag for xelatex because if you don't it puts strange "^I" characters in the saved dot files, which causes strange errors from the dot program. Well, either that or don't use tabs, I guess that works too. The other flag is "--shell-escape" to enable executing dot. If you're using lualatex or pdflatex you'll have to look up the right flags.

Of course you don't have to use dot, it's fairly trivial to specify neato or circo or any of the GraphViz programs.

I use it like this:

\begin{gvdot}{opinion}{width=100pt}
digraph {
    graphviz->is->cool->sorta;
}
\end{gvdot}

I think that's everything.

4
  • 2
    Thanks. :) I'm a long time lurker. TeX.SE basically taught me LaTeX. Oct 10, 2017 at 15:57
  • How to fix ! LaTeX Error: Environment gvdot undefined. ?
    – Jeff Tian
    May 17, 2020 at 11:04
  • It looks like your LaTeX engine isn't picking up the gvdot environment. Do you have the gvdot NewDocumentEnvironment in your preamble? May 21, 2020 at 15:28
  • Oh no I don't think I installed gvdot. Thanks for the information.
    – Jeff Tian
    May 22, 2020 at 1:42
3

The TexStudio variant

Yes, you can use Graphviz syntax in LaTeX! Here I show you how to do it in TexStudio.

I give you two possibilities, depending on whether you want to use LaTeX code in the Graphviz (more complicated) or not (easiest)

Easiest: no LaTeX code in Graphs

First, you need to allow for pdflatex to run the option --shell-scape. The simplest way is adding this comment at the beginning of your .tex file. (For alternative methods see post invoke --shell-scape in TexStudio)

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]

Second, load the packages graphviz with the option pdf (obviously ;))

Finally, you can run Build & View in TexStudio to get your graph.

Minimal Code Example:

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]

\documentclass{article}

\usepackage[pdf]{graphviz} %needed package with pdf option

\begin{document}

My graph:

\digraph{abc}{
  rankdir=LR;
  a -> b -> c -> d;
  a -> a;
}

\end{document}

The result should be this output:

enter image description here

LaTeX code in Graphs

In that case you want to use LaTeX code in the Graphviz graphs, for example to label a node $x^2$ or to label an edge $\int$.

First, just the same as above, you need to allow for pdflatex to run the option --shell-scape. The simplest way is adding this comment at the beginning of your .tex file. (For alternative methods see post invoke --shell-scape in TexStudio)

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]

Second, load the packages graphviz with the option psfrag, the packages auto-pst-pdf and psfrag

Finally, you can run Build & View in TexStudio to get your graph. Very probably, you need to run it twice until you get the .pdf.

Minimal Code Example:

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]

\documentclass{article}

\usepackage[psfrag]{graphviz} %psfrag option
\usepackage{auto-pst-pdf} %to process .ps into .pdf
\usepackage{psfrag} %provides \psfrag

\begin{document}

My graph:

%LaTex code used at the graph
\psfrag{x2}[cc][cc]{$x^2$}
\psfrag{intx2}[cc][cc]{\(\int x^2dx\)}
\psfrag{intsym}[cc][cc]{$\int\,$}

\digraph{abc}{
  rankdir=LR;

  %nodes label
  a [label= x2]
  b [label = intx2]

  %edges
  a -> a;
  a -> b [label=intsym] %edge label
  b -> c -> d;
}

\end{document}

The result should be this output: enter image description here

1

If you are an Emacs user, you can edit the graphviz code in a comment environment, select it and execute the dot2tex command with M'-x shell-command-on-region' (M-| ).

as an example, for a dot code, the command can be

dot2tex –autosize –figonly -s -t math -f tikz'

Depends your needs. Then, you can insert the tikz translated code from the command

'M-x insert-buffer RET'

You have to add manually the header commands to execute the tikz code:

\usepackage[x11names, rgb]{xcolor}
\usepackage{tikz}
\usetikzlibrary{snakes,arrows,shapes}

You can register a macro or compose a elisp function interactive to automatise.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .