Debian Codegraph @

texmf-dist/doc/latex/dot2texi/dot2texi.tex

raw
1 \documentclass[a4paper]{ltxdoc}
2
3 \usepackage[outputdir={docgraphs/}]{dot2texi}
4 \usepackage{paralist}
5 \usepackage{tikz,hyperref}
6 \usetikzlibrary{shapes,arrows,automata}
7 \usetikzlibrary{decorations.pathmorphing}
8 \usetikzlibrary{decorations.footprints}
9 \newcommand\packagename{\texttt{dot2texi}}
10
11 \title{The \texttt{dot2texi} package}
12 \author{Kjell Magne Fauske\\\footnotesize{\url{http://www.fauskes.net/}}}
13 \date{Version 3.0}
14 \begin{document}
15 \maketitle
16
17 \section{Introduction}
18
19 The \packagename\ package allows you to embed graphs written the DOT description language directly in your document. The dot2tex\footnote{Available from: \url{http://www.fauskes.net/code/dot2tex/}}\footnote{and \url{http://www.ctan.org/tex-archive/help/Catalogue/entries/dot2tex.html}} tool is used to transform the output from Graphviz to \LaTeX\ code using either the TikZ and PGF package, or the PSTricks package. The generated code can then be included directly in you document. This package can automate the process if shell escape is enabled.
20 \\
21
22 \noindent
23 This package is derived from the dottex\footnote{\url{http://www.ctan.org/tex-archive/help/Catalogue/entries/dottex.html}} and gnuplottex\footnote{\url{http://www.ctan.org/tex-archive/help/Catalogue/entries/gnuplottex.html}} packages written by Lars Kotthoff.
24
25 \section{Requirements}
26
27 To run \packagename\ the following packages are required:
28 \begin{itemize}
29 \item \texttt{xkeyval} version 2.3 or later required.
30 \item \texttt{moreverb}
31 \item PGF or PSTricks
32 \end{itemize}
33 In addition, the following external tools are required:
34 \begin{itemize}
35 \item Graphviz
36 \item dot2tex ( Version 2.7.0 or later recommended). The dot2texi package assumes that dot2tex is installed somewhere on the system path.
37 \end{itemize}
38
39
40 \noindent
41 For automatic creating of code, \TeX\ must be configured to allow calling of external programs. This feature is a potential security risk and is therefore usually disabled by default. You can enable this feature by specifying an option to \TeX\ when compiling the document. For \texttt{pdflatex} the option is \verb|--shell-escape|. On some systems the option is called \verb|--enable-write18|.
42
43 \section{Support}
44
45 If you have any questions or comments, send an email to \url{[email protected]} or use the mailing list available at \url{http://groups.google.com/group/dot2tex-users/}.
46
47 \section{Usage}
48
49 The package is loaded by writing \cmd{\usepackage\{dot2texi\}} in the document preamble.
50
51 \section{Package options}
52
53 The following package options are recognized:
54
55 \begin{description}
56 \item[\oarg{shell}] Use shell escape to automatically generate \TeX code using do2tex. This is the default behavior.
57 \item[\oarg{noshell}] Disable shell escape. Note that you can locally enable and disable shell escape for each \texttt{dot2tex} environment.
58 \item[\oarg{forceshell}] Force shell escape. Will run \texttt{dot2tex} on graphs even if they locally use the \oarg{noshell} option.
59 \item[\oarg{miktex}] MikTeX compatibility mode.
60 \item[\oarg{debug}] Invoke \texttt{dot2tex} in debug mode.
61 \item[\oarg{autosize}] Invoke \texttt{dot2tex} with the \texttt{--autosize} option. Will preprocess the graph using the preview package and adjust node and edge label sizes so that they fit the LaTeX output.
62 \item[\oarg{outputdir=dir}] Set a directory where the generated graph code will be stored. The default is to put the files in the current directory. The \emph{outputdir} value should have a trailing slash to ensure that it is interpreted as a directory. Example:
63 \begin{verbatim}
64 \usepackage[outputdir={docgraphs/}]
65 ...
66 \end{verbatim}
67 \end{description}
68
69 \subsection{Output format}
70
71 \begin{description}
72 \item[\oarg{tikz}] Use the tikz output format. This is the default output format.
73 \item[\oarg{pgf}] Use the pgf output format.
74 \item[\oarg{pstricks}] Use the pstricks output format.
75 \end{description}
76
77 \subsection{Graph layout}
78 Set the default graph layout tool
79 \begin{description}
80 \item[\oarg{dot}] Hierarchical layout
81 \item[\oarg{neato}] Spring model layot
82 \item[\oarg{circo}] Circular layout
83 \item[\oarg{fdp}] Spring model layout
84 \item[\oarg{twopi}] Radial layout
85 \end{description}
86
87 \section{Macros}
88
89 \DescribeMacro{\setoutputdir}
90 Set a directory where the generated graph code will be stored. Does the same as the \oarg{outputdir} package option. Useful if you want to organize your graphs in different directories. Example:
91 \begin{verbatim}
92 \documentclass{report}
93 \usepackage{dot2texi}
94 \begin{document}
95 ...
96 \chapter{Chapter A}
97 \setoutputdir{chapA/}
98 ....
99 \chapter{Chapter B}
100 \setoutputdir{chapB/}
101 ...
102 \end{document}
103 \end{verbatim}
104
105
106
107 \section{The \texttt{dot2tex} environment}
108 \DescribeEnv{dot2tex}
109 The \packagename\ package defines the \texttt{dot2tex} environment. The contents of the environment will be written to file during compilation. If shell escape is enabled the \texttt{dot2tex} tool and Graphviz will then be run on the saved file. This process generates \LaTeX\ code that will be included automatically during compilation.
110
111 \subsection{Environment options}
112
113 Most of the package options can also be used in the dot2tex environment. They will then locally override the package options.
114
115 \begin{description}
116 \item[\oarg{shell}] Enable shell escape for the current graph.
117 \item[\oarg{noshell}] Disable shell escape for the current graph.
118 \end{description}
119 Output formats:
120 \begin{description}
121 \item[\oarg{tikz}] Use the tikz output format.
122 \item[\oarg{pgf}] Use the pgf output format.
123 \item[\oarg{pstricks}] Use the pstricks output format.
124 \item[\oarg{format=outputformat}] Set output format. Allowed values are {\verb#tikz|pgf|pstricks#}.
125 \end{description}
126 Graph layout:
127 \begin{description}
128 \item[\oarg{dot}] Hierarchical layout
129 \item[\oarg{neato}] Spring model layot
130 \item[\oarg{circo}] Circular layout
131 \item[\oarg{fdp}] Spring model layout
132 \item[\oarg{twopi}] Radial layout
133 \item[\oarg{prog=layouttool}] Set program to use for graph layout. Allowed values are {\verb#dot|neato|circo|fdp|twopi#}.
134 \end{description}
135 Files:
136 \begin{description}
137 \item[\oarg{outputdir=dir}] Locally override the \oarg{outputdir} package option value.
138 \item[\oarg{file=filename}] Set the base name of the generated \texttt{dot} and \texttt{tex} file. The name is generated automatically, but this option lets you override the default file name. Example:
139 \begin{verbatim}
140 \begin{dot2tex}[file=mygraph]
141 ...
142 \end{dot2tex}
143 \end{verbatim}
144 Compiling the above code will generate the files \texttt{mygraph.dot} and \texttt{mygraph.tex}. Note that the \texttt{dot} and \texttt{tex} extensions are added automatically. Here is another example:
145 \begin{verbatim}
146 \begin{dot2tex}[file=graphs/minimal]
147 ...
148 \end{dot2tex}
149 \end{verbatim}
150 The above code will generate the files \texttt{minimal.dot} and \texttt{minimal.tex} in the \texttt{graphs} directory.
151
152 \emph{Note:} If the file name contains spaces or other special characters use:
153 \begin{verbatim}
154 \begin{dot2tex}[file="name with spaces"]
155 ...
156 \end{verbatim}
157
158 \emph{Note:} If the \oarg{outputdir} option is set, its value will be prepended.
159 \end{description}
160 Options:
161 \begin{description}
162 \item[\oarg{options=opts}] Pass additional command line options to \texttt{dot2tex}. See the dot2tex documentation\footnote{\url{http://www.fauskes.net/code/dot2tex/documentation/}} for available options.
163
164 \emph{Note}. If opts contains an equal sign,=, you have to put opts inside curly brackets. Example:
165 \begin{verbatim}
166 \begin{dot2tex}[options=--graphstyle "scale=0.25"]
167 graph G {
168 ...
169 }
170 \end{dot2tex}
171 \end{verbatim}
172 The above code will not work because opts i parsed incorrectly. Instead you have to write:
173 \begin{verbatim}
174 \begin{dot2tex}[options={--graphstyle "scale=0.25"}]
175 graph G {
176 ...
177 }
178 \end{dot2tex}
179 \end{verbatim}
180 \item[\oarg{autosize}] Run \texttt{dot2tex} with the \texttt{--autosize} option.
181 \item[\oarg{noautosize}] Locally override the package \oarg{autosize} option.
182 \item[\oarg{codeonly}] Run \texttt{dot2tex} with the \texttt{--codeonly} option. The generated code will then not be wrapped inside a \texttt{tikzpicture} or \texttt{pspicture} environment. Note that this option requires \texttt{dot2tex} version 2.7.0 or later.
183 \item[\oarg{graphstyle=style}] Set the \texttt{<<graphstyle>>} template variable. For the default templates the style value will be inserted as:
184 \begin{verbatim}
185 ...
186 \begin{tikzpicture}[<<graphstyle>>]
187 ...
188 \end{tikzpicture}
189 ..
190 \end{verbatim}
191 Use this option to set styles that affect all of the graph. Example:
192 \begin{verbatim}
193 ...
194 \begin{dot2tex}[graphstyle={scale=0.5,transform shape}]
195 ...
196 \end{dot2tex}
197 ...
198 \end{verbatim}
199 The above style value will scale down the graph to half its size.
200
201 \emph{Note:} Use curly braces around the style value to avoid confusing the xkeyval parser.
202
203 \item[\oarg{mathmode}] Run \texttt{dot2tex} with tex mode set to math. This means that labels are interpreted as math.
204
205 \item[\oarg{scale=factor}] Scale the graph by a factor.
206
207 \emph{Note:} This is an experimental feature. Currently writing \texttt{scale=0.5} is equivalent to \texttt{graphstyle=\{scale=0.5,transform shape\}}. Will not work for the pstricks output format or if the \texttt{codeonly} option is used.
208
209 \item[\oarg{straightedges}] Run \texttt{dot2tex} with the \texttt{--straightedges} option.
210 \item[\oarg{styleonly}] Run \texttt{dot2tex} with the \texttt{--styleonly} option. Only works with the \texttt{tikz} output format. Uses only styles when drawing nodes. No draw or shape option is added.
211
212 \item[\oarg{tikzedgelabels}] Run \texttt{dot2tex} with the \texttt{--tikzedgelabels} option.
213
214 \end{description}
215
216 \noindent
217 \emph{Warning:} All of the options are passed to dot2tex as command line options. If an option is given multiple times, the last one will win. One side effect of this is that you can't use both the \texttt{scale} and \texttt{graphstyle} at the same time. Example:
218 \begin{verbatim}
219 ...
220 \begin{dot2tex}[scale=0.5,graphstyle={shorten >=5pt}]
221 ...
222 \end{dot2tex}
223 ...
224 \end{verbatim}
225 In the above code the \texttt{graphstyle} option will win. If you interchange the options, the \texttt{graphstyle} option will have no effect.
226
227 \section{Examples}
228 \begin{center}
229 \begin{dot2tex}[prog=neato,mathmode]
230 digraph G {
231 node [shape="circle"];
232 a_1 -> a_2 -> a_3 -> a_4 -> a_1;
233 }
234 \end{dot2tex}
235 \end{center}
236 \noindent
237 The minimal code required to generate the above graph is:
238
239 \begin{verbatim}
240 \documentclass{article}
241 \usepackage{dot2texi}
242
243 \usepackage{tikz}
244 \usetikzlibrary{shapes,arrows}
245
246 \begin{document}
247 \begin{dot2tex}[neato,mathmode]
248 digraph G {
249 node [shape="circle"];
250 a_1 -> a_2 -> a_3 -> a_4 -> a_1;
251 }
252 \end{dot2tex}
253
254 \end{document}
255 \end{verbatim}
256
257 \noindent
258 Now an example that uses several TikZ features:
259
260 \begin{center}
261 \tikzstyle{ball} = [shape=circle, minimum size=.5cm]
262 \begin{dot2tex}[straightedges,fdp,styleonly]
263 graph G {
264 node [style="ball, ball color =green", label=""];
265 edge [style="decorate,decoration=zigzag, green,thick"];
266 a_1 -- c -- a_2;
267 c [style="ball, ball color=black"];
268 edge [style="decorate,decoration=snake, blue"];
269 node [style="ball, ball color = red", label=""];
270 a_3 -- c -- a_4 --a_3;
271 }
272 \end{dot2tex}
273 \end{center}
274
275
276 \begin{verbatim}
277 \documentclass{article}
278 \usepackage{dot2texi}
279
280 \usepackage{tikz}
281 \usetikzlibrary{shapes,arrows}
282 \usetikzlibrary{decorations.pathmorphing}
283
284 \begin{document}
285 \tikzstyle{ball} = [shape=circle, minimum size=.5cm]
286 \begin{dot2tex}[straightedges,fdp,styleonly]
287 graph G {
288 node [style="ball, ball color =green", label=""];
289 edge [style="decorate,decoration=zigzag, green,thick"];
290 a_1 -- c -- a_2;
291 c [style="ball, ball color=black"];
292 edge [style="decorate,decoration=snake, blue"];
293 node [style="ball, ball color = red", label=""];
294 a_3 -- c -- a_4 --a_3;
295 }
296 \end{dot2tex}
297 \end{document}
298 \end{verbatim}
299
300 \noindent
301 The next example shows that the \oarg{codeonly} environment option is useful when you want to adjust and annotate the generated graph. Note that you manually have to wrap the code inside a figure environment, but this gives you a lot of flexibility. When using the \texttt{tikz} output format, you can later access the nodes.
302
303 % Define layers
304 \pgfdeclarelayer{background}
305 \pgfdeclarelayer{foreground}
306 \pgfsetlayers{background,main,foreground}
307 % The scale option is useful for adjusting spacing between nodes.
308 % Note that this works best when straight lines are used to connect
309 % the nodes.
310 \begin{tikzpicture}[>=latex',scale=0.8]
311 % set node style
312 \tikzstyle{n} = [draw,shape=circle,minimum size=2em,
313 inner sep=0pt,fill=red!20]
314 \begin{dot2tex}[dot,codeonly,styleonly,options=-s -tmath]
315 digraph G {
316 node [style="n"];
317 A_1 -> B_1; A_1 -> B_2; A_1 -> B_3;
318 B_1 -> C_1; B_1 -> C_2;
319 B_2 -> C_2; B_2 -> C_3;
320 B_3 -> C_3; B_3 -> C_4;
321 }
322 \end{dot2tex}
323 % annotations
324 \node[left=1em] at (C_1.west) (l3) {Level 3};
325 \node at (l3 |- B_1) (l2){Level 2};
326 \node at (l3 |- A_1) (l1) {Level 1};
327 % Draw lines to separate the levels. First we need to calculate
328 % where the middle is.
329 \path (l3) -- coordinate (l32) (l2) -- coordinate (l21) (l1);
330 \draw[dashed] (C_1 |- l32) -- (l32 -| C_4);
331 \draw[dashed] (C_1 |- l21) -- (l21 -| C_4);
332 \draw[<->,red] (A_1) to[out=-120,in=90] (C_2);
333 % Highlight the A_1 -> B_1 -> C_2 path. Use layers to draw
334 % behind everything.
335 \begin{pgfonlayer}{background}
336 \draw[rounded corners=2em,line width=3em,blue!20,cap=round]
337 (A_1.center) -- (B_1.west) -- (C_2.center);
338 \end{pgfonlayer}
339 \end{tikzpicture}
340
341 \begin{verbatim}
342 \documentclass{article}
343 \usepackage{tikz}
344 \usetikzlibrary{arrows,shapes}
345 \usepackage{dot2texi}
346 \begin{document}
347 % Define layers
348 \pgfdeclarelayer{background}
349 \pgfdeclarelayer{foreground}
350 \pgfsetlayers{background,main,foreground}
351
352 % The scale option is useful for adjusting spacing between nodes.
353 % Note that this works best when straight lines are used to connect
354 % the nodes.
355 \begin{tikzpicture}[>=latex',scale=0.8]
356 % set node style
357 \tikzstyle{n} = [draw,shape=circle,minimum size=2em,
358 inner sep=0pt,fill=red!20]
359 \begin{dot2tex}[dot,tikz,codeonly,styleonly,options=-s -tmath]
360 digraph G {
361 node [style="n"];
362 A_1 -> B_1; A_1 -> B_2; A_1 -> B_3;
363 B_1 -> C_1; B_1 -> C_2;
364 B_2 -> C_2; B_2 -> C_3;
365 B_3 -> C_3; B_3 -> C_4;
366 }
367 \end{dot2tex}
368 % annotations
369 \node[left=1em] at (C_1.west) (l3) {Level 3};
370 \node at (l3 |- B_1) (l2){Level 2};
371 \node at (l3 |- A_1) (l1) {Level 1};
372 % Draw lines to separate the levels. First we need to calculate
373 % where the middle is.
374 \path (l3) -- coordinate (l32) (l2) -- coordinate (l21) (l1);
375 \draw[dashed] (C_1 |- l32) -- (l32 -| C_4);
376 \draw[dashed] (C_1 |- l21) -- (l21 -| C_4);
377 \draw[<->,red] (A_1) to[out=-120,in=90] (C_2);
378 % Highlight the A_1 -> B_1 -> C_2 path. Use layers to draw
379 % behind everything.
380 \begin{pgfonlayer}{background}
381 \draw[rounded corners=2em,line width=3em,blue!20,cap=round]
382 (A_1.center) -- (B_1.west) -- (C_2.center);
383 \end{pgfonlayer}
384 \end{tikzpicture}
385 \end{document}
386 \end{verbatim}
387
388 \noindent
389 The next example uses the \texttt{automata} library to create a nice looking state machine:
390 \begin{center}
391 \begin{tikzpicture}[
392 every state/.style={draw=blue!50,very thick,fill=blue!20}]
393 \begin{dot2tex}[styleonly,codeonly,neato,tikzedgelabels]
394 digraph G {
395 node [style="state"];
396 edge [lblstyle="auto",topath="bend left"];
397 A [style="state, initial"];
398 A -> B [label=2];
399 A -> D [label=7];
400 B -> A [label=1];
401 B -> B [label=3,topath="loop above"];
402 B -> C [label=4];
403 C -> F [label=5];
404 F -> B [label=8];
405 F -> D [label=7];
406 D -> E [label=2];
407 E -> A [label="1,6"];
408 F [style="state,accepting"];
409 }
410 \end{dot2tex}
411 \end{tikzpicture}
412 \end{center}
413
414 \begin{verbatim}
415 \documentclass{article}
416 \usepackage{dot2texi}
417 \usepackage{tikz}
418 \usetikzlibrary{automata,shapes}
419 \begin{document}
420 \begin{tikzpicture}[
421 every state/.style={draw=blue!50,very thick,fill=blue!20}]
422 \begin{dot2tex}[styleonly,codeonly,neato]
423 digraph G {
424 d2ttikzedgelabels = true;
425 node [style="state"];
426 edge [lblstyle="auto",topath="bend left"];
427 A [style="state, initial"];
428 A -> B [label=2];
429 A -> D [label=7];
430 B -> A [label=1];
431 B -> B [label=3,topath="loop above"];
432 B -> C [label=4];
433 C -> F [label=5];
434 F -> B [label=8];
435 F -> D [label=7];
436 D -> E [label=2];
437 E -> A [label="1,6"];
438 F [style="state,accepting"];
439 }
440 \end{dot2tex}
441 \end{tikzpicture}
442 \end{document}
443 \end{verbatim}
444
445 \noindent
446 Another example of using the special \texttt{topath} attribute. Note the use of the \texttt{graphstyle} option to shorten the edges.
447
448 \begin{center}
449 \begin{dot2tex}[circo,graphstyle={shorten >=1pt,shorten <=1pt}]
450 digraph G {
451 mindist = 0.5;
452 node [shape="circle"];
453 a -> b [topath="bend right"];
454 c -> b [topath="bend left"];
455 c -> a [topath="out=10,in=-90"];
456 b -> b [topath="loop above"];
457 }
458 \end{dot2tex}
459 \end{center}
460 \begin{verbatim}
461 \documentclass{article}
462 \usepackage{dot2texi}
463
464 \usepackage{tikz}
465 \usetikzlibrary{shapes,arrows}
466
467 \begin{document}
468 \begin{dot2tex}[circo,graphstyle={shorten >=1pt,shorten <=1pt}]
469 digraph G {
470 mindist = 0.5;
471 node [shape="circle"];
472 a -> b [topath="bend right"];
473 c -> b [topath="bend left"];
474 c -> a [topath="out=10,in=-90"];
475 b -> b [topath="loop above"];
476 }
477 \end{dot2tex}
478
479 \end{document}
480 \end{verbatim}
481 \section{Tips and tricks}
482
483 \begin{itemize}
484 \item Generating the graphs can be quite time consuming. The \texttt{shell} and \texttt{noshell} environment options are very useful when working with documents with many graphs.
485 \item Use the \texttt{outputdir} package option to organize your files.
486 \end{itemize}
487
488 \subsection{External files}
489
490 Sometimes it is practical to keep a dot graph in a separate file, for instance when the graph is automatically generated. Dot2tex version 2.8 or later supports an inclusion mechanism similar to \LaTeX's. If a graph contains only the line \verb|\input{filename.dot}| it will load \texttt{filename.dot} and convert it. Here is an example:
491
492 \begin{verbatim}
493 ...
494 \begin{dot2tex}
495 \input{externalgraph.dot}
496 \end{dot2tex}
497 ..
498 \end{verbatim}
499
500
501 \section{Changelog}
502
503 \begin{itemize}
504 \item Version 3.0 (2008-05-07)
505 \begin{itemize}
506 \item Updated documentation examples to use PGF 2.00.
507 \item Added the experimental \texttt{scale} environment option.
508 \item Added the \texttt{graphstyle} environment option.
509 \item Added the \texttt{mathmode} environment option.
510 \item Added the \texttt{tikzedgelabels} environment option.
511 \item Added the \texttt{straightedges} environment option.
512 \item Added the \texttt{outputdir} option and \texttt{$\backslash$setoutputdir} macro.
513 \item Added the \texttt{file} environment option.
514 \end{itemize}
515
516 \item Version 2.0 (2007-12-09)
517 \begin{itemize}
518 \item Updated documentation.
519 \item It is now not necessary for Windows users to specify the \texttt{miktex} option. Platform is now detected automatically.
520 \item Added the \texttt{forceshell} package option.
521 \item Added the \texttt{codeonly} environment option.
522 \item Added the \texttt{styleonly} environment option.
523 \item Added the \texttt{autosize} and \texttt{noautosize} option.
524 \end{itemize}
525 \item Version 1.0. Initial release
526 \end{itemize}
527
528 \section*{Acknowledgements}
529
530 Thanks to Lars Kotthoff for writing the excellent dottex and gnuplottex packages. The platform detection code is based on the ifplatform\footnote{\url{http://www.ctan.org/tex-archive/help/Catalogue/entries/ifplatform.html}} package written by Will Robertson and Johannes Gro{\ss}e.
531
532 Thanks to all users for valuable feedback and suggestions! A special thanks to Rolf Niepraschk for pointing me to the dottex and ifplatform packages.
533
534
535 \end{document}