20

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
13

You can use dot2tex, which is available from CTAN.

  • 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
8

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.

  • 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

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

3

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.

  • 2
    Thanks. :) I'm a long time lurker. TeX.SE basically taught me LaTeX. – Adam J Richardson Oct 10 '17 at 15:57
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.

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.