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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
You can use dot2tex, which is available from CTAN.
dot2texi
and it developed by the same author.
Mar 17, 2011 at 9:10
You can use the graphviz package.
\documentclass{standalone}
\usepackage[pdf]{graphviz}
\begin{document}
\digraph{abc}{
rankdir=LR;
a -> b -> c;
}
\end{document}
More examples here.
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).
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.
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)
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.
% !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:
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
.
% !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}
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.