25

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?

  • Its easy with the graphviz package. See some working examples here. – user1491229 Jan 20 '18 at 18:08
14

You can use dot2tex, which is available from CTAN.

| improve this answer | |
  • 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. – Tiago Veloso Mar 17 '11 at 9:10
  • How? The examples look wrong. They are putting latex in dot not dot in latex. – mathtick Aug 31 '19 at 13:27
13

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.

| improve this answer | |
  • 1
    The package has not bee updated in 5 years. Are you sure it's the better one to use? – einpoklum Aug 24 '18 at 20:54
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.

| improve this answer | |
  • 2
    Thanks. :) I'm a long time lurker. TeX.SE basically taught me LaTeX. – Adam J Richardson Oct 10 '17 at 15:57
  • How to fix ! LaTeX Error: Environment gvdot undefined. ? – Jeff Tian May 17 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? – Adam J Richardson May 21 at 15:28
  • Oh no I don't think I installed gvdot. Thanks for the information. – Jeff Tian May 22 at 1:42
4

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).

| improve this answer | |
2

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

| improve this answer | |
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.