Debian Codegraph @

docs/customization_guide.rst

raw
1 ===================
2 Customization guide
3 ===================
4
5 Customizing the output
6 ======================
7
8 Dot2tex offers a few ways of modifying the generated output.
9
10 Using styles
11 ------------
12
13 The dot language defines the ``style`` attribute that can be used to modify the appearance of graphs, nodes, and edges. The ``style`` attribute is passed to the rendering backend, and is a powerful and flexible way of customizing the look and feel of your graphs. Using styles requires detailed knowledge of the output format.
14
15 The following example shows how interesting visual results can be achieved with the PGF/TikZ output format. The styles are PGF/TikZ specific. See the user guide for details:
16
17 ::
18
19 graph G {
20 node [shape=circle, fixedsize=True, width="0.2",
21 style="ball color =green", label=""];
22 edge [style="snake=zigzag, green"];
23 a_1 -- c -- a_2;
24 c [style="ball color=black"];
25 edge [style="snake=snake, blue"];
26 node [style="ball color = red", label=""];
27 a_3 -- c -- a_4 --a_3;
28 }
29
30 The ``snake`` styles only work on straight lines. We therefore have to use the ``-s`` option. ``fdp`` is used to lay out the graph::
31
32 $ fdp -Txdot ball.dot | dot2tex -ftikz -s > balls.tex
33
34 The resulting graph is shown below.
35
36 .. figure:: img/balls.*
37 :alt: Using styles to style the PGF/TikZ output.
38
39 .. note:: Use the straight edge option ``-s`` to force the use of straight lines. Otherwise curves will be used to draw even straight lines.
40
41
42 Changing arrow types
43 ~~~~~~~~~~~~~~~~~~~~
44
45 The style attribute can be used to change arrow types. A PGF/TikZ example:
46
47 ::
48
49 digraph G {
50 graph [mindist=0.5];
51 node [fixedsize=true, shape=circle, width=0.4, style="fill=green!20"];
52 c -> n_1 [style="-stealth"];
53 c -> n_2 [style="-to"];
54 c -> n_3 [style="-latex"];
55 c -> n_4 [style="-diamond"];
56 c -> n_5 [style="-o"];
57 c -> n_6 [style="{-]}"];
58 c -> n_7 [style="-triangle 90"];
59 c -> n_8 [style="-hooks"];
60 c -> n_9 [style="->>"];
61 c [style="fill=red!80"];
62 }
63
64 Rendered with::
65
66 $ circo -Txdot pgfarrows.dot | dot2tex -tmath > pgfarrows.tex
67
68 .. figure:: img/pgfarrows.*
69 :alt: PGF/TikZ style arrows.
70
71 You can also set the default arrow style by using the ``--graphstyle`` option or ``d2tgraphstyle`` attribute::
72
73 $ dot2tex -tmath --graphstyle=">=diamond" ex1.dot > ex1gstyle.tex
74
75 .. figure:: img/ex1gstyle.*
76 :alt: Setting default PGF/TikZ arrows.
77
78 A PSTricks example:
79
80 ::
81
82 digraph G {
83 d2tdocpreamble="\usepackage{pstricks-add}";
84 graph [mindist=0.5];
85 node [texmode="math", fixedsize=true, shape=circle, width=0.4];
86 c -> n_1 [style="arrows=->"];
87 c -> n_2 [style="arrows=->>"];
88 c -> n_3 [style="arrows=-<"];
89 c -> n_4 [style="arrows=-*"];
90 c -> n_5 [style="arrows=-{]}"];
91 c -> n_6 [style="arrows=-o"];
92 c -> n_7 [style="arrows=-H"];
93 }
94
95 Rendered with::
96
97 $ circo -Txdot pstarrows.dot | dot2tex -fpst > pstarrows.tex
98
99
100 .. figure:: img/pstarrows.*
101 :alt: PSTricks style arrows
102
103 The above example shows how the ``d2tdocpreamble`` attribute can be used to load additional LaTeX packages. You could also use the ```--docpreamble`` option::
104
105 $ ... | dot2tex -fpst --docpreamble="\usepackage{pstricks-add}" ...
106
107
108 Label styles
109 ~~~~~~~~~~~~
110
111 Node, edge and graph labels can be styled using the special ``lblstyle`` attribute. However, this only works for the ``pgf`` and ``tikz`` output formats.
112
113 Labels are drawn using code like:
114
115 .. sourcecode:: latex
116
117 \draw (157bp,52bp) node {label};
118
119 When you specify a ``lblstyle`` attribute, the style will be given as a parameter to the node like this:
120
121 .. sourcecode:: latex
122
123 \draw (157bp,52bp) node[lblstyle] {label};
124
125 Example:
126
127 ::
128
129 digraph G {
130 node [shape=circle];
131 a -> b [label="label",lblstyle="draw=red,cross out"];
132 b -> c [label="test",lblstyle="below=0.5cm,rotate=20,fill=blue!20"];
133 a [label="aa",lblstyle="blue"];
134 b [lblstyle="font=\Huge"];
135 c [label="ccc", lblstyle="red,rotate=90"];
136 label="Graph label";
137 lblstyle="draw,fill=red!20";
138 rankdir=LR;
139 }
140
141
142 .. figure:: img/lblstyle.*
143 :alt: Label styles
144
145 See the PGF and TikZ documentation for more information about styles.
146
147 .. note:: You can use the ``exstyle`` attribute in addition to ``lblstyle``. The difference is that ``exstyle`` is ignored in preprocessing mode. Useful when using TikZ' ``pin`` and ``label`` options and you do not want them to influence the graph layout.
148
149
150 Node and edge options
151 ~~~~~~~~~~~~~~~~~~~~~
152
153 The ``tikz`` output format offers an additional way of customizing the output by using the ``--nodeoptions`` and ``--edgeoptions`` options, or the ``d2tnodeoptions`` and ``d2tedgeoptions`` graph attributes. The code for generating nodes and edges will then be wrapped in a ``scope`` environment like this:
154
155 .. sourcecode:: latex
156
157 ...
158 \begin{scope}[nodeoptions]
159 % code for drawing nodes
160 \end{scope}
161 \begin{scope}[edgeoptions]
162 % code for drawing edges
163 \end{scope}
164 ...
165
166 Customizing edges
167 =================
168
169 The ``tikz`` and ``pgf`` output formats offers a few additional ways of customizing how edges are drawn and how edge edge labels are placed. These features are tightly integrated with TikZ and detailed knowledge of the output format is therefore necessary.
170
171 TikZ edge labels
172 ----------------
173
174 With the ``--tikzedgelabel`` option you can bypass the XDOT edge label placement and let PGF and TikZ do the job instead. This can be useful in some cases. However, this only works properly for straight edges and ``to`` paths.
175
176 Example:
177
178 ::
179
180 graph G {
181 mindist = 0.5;
182 node [shape="circle"];
183 edge [lblstyle="mystyle"];
184 a -- b [label="ab"];
185 b -- c [label="bc"];
186 c -- a [label="ca"];
187 }
188
189 Without the ``--tikzedgelabel`` option the code for placing edges will look something like this:
190
191 .. sourcecode:: latex
192
193 % Edge: a -- b
194 \draw [] (28bp,55bp) -- (28bp,75bp);
195 \draw (40bp,65bp) node[mystyle] {ab};
196 % Edge: b -- c
197 \draw [] (51bp,88bp) -- (68bp,78bp);
198 \draw (66bp,96bp) node[mystyle] {bc};
199 % Edge: c -- a
200 \draw [] (69bp,51bp) -- (52bp,41bp);
201 \draw (53bp,57bp) node[mystyle] {ca};
202
203 With the ``tikzedgelabels`` option the output is simply:
204
205 .. sourcecode:: latex
206
207 \draw [] (a) -- node[mystyle] {ab} (b);
208 \draw [] (b) -- node[mystyle] {bc} (c);
209 \draw [] (c) -- node[mystyle] {ca} (a);
210
211 The placement of the edge labels depends on the options passed to the edge label node (in this case ``mystyle``), and the curve used to connect the nodes. Some examples of ``mystyle`` values are shown in the figure below. The leftmost graph is rendered without the ``tikzedgelabels`` option.
212
213 .. figure:: img/tikzedgelabels.*
214 :alt: blabla
215
216 Limitations:
217
218 - Works best with straight edges and ``to`` paths
219 - The ``headlabel`` and ``taillabel`` attributes are currently not affected by the ``tikzedgelabels`` option.
220
221 To paths
222 --------
223
224 The ``topath`` edge attribute offers a way to override the edges drawn by Graphviz. When a ``topath`` attribute is encountered, dot2tex inserts a so called ``to`` path operation to connect the nodes. A number of predefined to paths are defined by TikZ, and you can create your own.
225
226 Example:
227
228 ::
229
230 digraph G {
231 mindist = 0.5;
232 node [shape="circle"];
233 a -> b [topath="bend right"];
234 c -> b [topath="bend left"];
235 c -> a [topath="out=10,in=-90"];
236 b -> b [topath="loop above"];
237 }
238
239 Generating the graph with::
240
241 $ circo -Txdot topaths1.dot | dot2tex -ftikz > topaths1.tex
242
243 yields:
244
245 .. figure:: img/topaths1.*
246
247
248 The generated edge drawing code is:
249
250 .. sourcecode:: latex
251
252 \draw [->] (a) to[bend right] (b);
253 \draw [->] (c) to[bend left] (b);
254 \draw [->] (c) to[out=10,in=-90] (a);
255 \draw [->] (b) to[loop above] (b);
256
257 .. note:: To paths works best with layout tools that generate straight edges (neato, fdp, circo, twopi). The ``topath`` attribute overrides the edge routing done by Graphviz. You may therefore end up with overlapping edges.
258
259 Here is a larger example that uses the ``automata`` library:
260
261 ::
262
263 digraph G {
264 d2tdocpreamble = "\usetikzlibrary{automata}";
265 d2tfigpreamble = "\tikzstyle{every state}= \
266 [draw=blue!50,very thick,fill=blue!20]";
267 node [style="state"];
268 edge [lblstyle="auto",topath="bend left"];
269 A [style="state, initial"];
270 A -> B [label=2];
271 A -> D [label=7];
272 B -> A [label=1];
273 B -> B [label=3,topath="loop above"];
274 B -> C [label=4];
275 C -> F [label=5];
276 F -> B [label=8];
277 F -> D [label=7];
278 D -> E [label=2];
279 E -> A [label="1,6"];
280 F [style="state,accepting"];
281 }
282
283 Generated with::
284
285 neato -Txdot fsm1.dot | dot2tex -ftikz --tikzedgelabels --styleonly
286
287 .. figure:: img/fsm1.*
288 :alt: blabla
289
290
291
292 Color support
293 =============
294
295 All Graphviz `color formats`_ are supported, including the RGBA format. Transparency will however only work when using the PGF/TikZ output format.
296
297 .. _color formats: http://www.graphviz.org/doc/info/attrs.html#k:color
298
299 Named colors are supported, but you have to ensure that the colors are defined in the resulting LaTeX file. The default PSTricks and PGF/TikZ templates load the ``X11names`` color scheme defined in the xcolor_ package. Note that color names in the xcolor_ package are case sensitive. This is not the case with Graphviz's `color names`_. Use CamelCase_ names in your graphs to ensure compatibility with xcolor_.
300
301 .. _CamelCase: http://en.wikipedia.org/wiki/CamelCase
302 .. _xcolor: http://www.ctan.org/pkg/xcolor
303 .. _color names: http://www.graphviz.org/doc/info/colors.html
304
305 For convenience, a color definition file ``gcols.tex`` is distributed with dot2tex. You can find it in the ``examples`` directory. This file defines most of Graphviz's named colors as lower case. Include this file in the preamble if you need it.
306
307 .. _templates:
308
309 Templates
310 =========
311
312 The output from dot2tex is a list of drawing commands. To render the graphics with LaTeX there's a need for some boiling plate code. This code can be customized using simple templates. If no template is specified with the ``--template`` option, a default template will be used.
313
314 The following template tags are available:
315
316 ``<<drawcommands>>``
317 The actual list of drawing commands.
318
319 ``<<figcode>>``
320 Drawing commands wrapped in a figure environment. Note that several important style options are set in the figure environment.
321
322 ``<<bbox>>``
323 Bounding box. Example: ``(0bp,0bp)(100bp,100bp)``
324 The individual parts of the bounding box are available with the tags:
325
326 - ``<<bbox.x0>>``
327 - ``<<bbox.y0>>``
328 - ``<<bbox.x1>>``
329 - ``<<bbox.y1>>``
330
331 Note that the bounding box parts are given without any units.
332
333 ``<<textencoding>>``
334 The text encoding used for the output file. Current values are:
335 - ``utf8``
336 - ``latin1``
337
338 ``<<docpreamble>>``
339 Document preamble. The content of this tag is set by the ``--docpreamble`` option or ``d2tdocpreamble`` graph attribute. Useful for including packages and such.
340
341 ``<<figpreamble>>``
342 Figure preamble. The content of this tag is set by the ``--figpreamble`` option or ``d2tfigpreamble`` graph attribute. Useful for setting font sizes and such.
343
344 ``<<preproccode>>``
345 Code generated for preprocessing labels.
346
347 Three different templates are used by dot2tex for the preprocessing mode, output mode and figure only mode respectively. The following template tags make it possible to use the same template file for all modes.
348
349 ``<<startoutputsection>>`` and ``<<endoutputsection>>``
350 Code between these tags is ignored in preprocessing mode.
351
352
353 ``<<startpreprocsection>>`` and ``<<endpreprocsection>>``
354 Code between these tags is ignored in output mode.
355
356 ``<<startfigonlysection>>`` and ``<<endfigonlysection>>``
357 Code between these tags is used as a template when using the ``--figonly`` option. Ignored in preprocessing and output mode.
358
359
360 .. note:: Tags that have no value are replaced with an empty string. Insert a ``%`` character after a template tag to avoid unwanted line breaks.
361
362 Default PGF/TikZ template
363 -------------------------
364
365 .. sourcecode:: latex
366
367 \documentclass{article}
368 \usepackage[x11names, rgb]{xcolor}
369 \usepackage[<<textencoding>>]{inputenc}
370 \usepackage{tikz}
371 \usetikzlibrary{snakes,arrows,shapes}
372 \usepackage{amsmath}
373 <<startpreprocsection>>%
374 \usepackage[active,auctex]{preview}
375 <<endpreprocsection>>%
376 <<gvcols>>%
377 <<cropcode>>%
378 <<docpreamble>>%
379
380 \begin{document}
381 \pagestyle{empty}
382 %
383 <<startpreprocsection>>%
384 <<preproccode>>
385 <<endpreprocsection>>%
386 %
387 <<startoutputsection>>
388 \enlargethispage{100cm}
389 % Start of code
390 % \begin{tikzpicture}[anchor=mid,>=latex',join=bevel,<<graphstyle>>]
391 \begin{tikzpicture}[>=latex',join=bevel,<<graphstyle>>]
392 \pgfsetlinewidth{1bp}
393 <<figpreamble>>%
394 <<drawcommands>>
395 <<figpostamble>>%
396 \end{tikzpicture}
397 % End of code
398 <<endoutputsection>>
399 %
400 \end{document}
401 %
402 <<startfigonlysection>>
403 \begin{tikzpicture}[>=latex,join=bevel,<<graphstyle>>]
404 \pgfsetlinewidth{1bp}
405 <<figpreamble>>%
406 <<drawcommands>>
407 <<figpostamble>>%
408 \end{tikzpicture}
409 <<endfigonlysection>>
410
411
412 The ``<<cropcode>>`` template tag is available when the ``--preview`` option is used. The contents will then be:
413
414 .. sourcecode:: latex
415
416 \usepackage[active,tightpage]{preview}
417 \PreviewEnvironment{tikzpicture}
418 \setlength\PreviewBorder{<<margin>>}
419
420
421
422 Default pstricks template
423 -------------------------
424
425 .. sourcecode:: latex
426
427 \documentclass{article}
428 % <<bbox>>
429 \usepackage[x11names]{xcolor}
430 \usepackage[<<textencoding>>]{inputenc}
431 \usepackage{graphicx}
432 \usepackage{pstricks}
433 \usepackage{amsmath}
434 <<startpreprocsection>>%
435 \usepackage[active,auctex]{preview}
436 <<endpreprocsection>>%
437 <<gvcols>>%
438 <<docpreamble>>%
439
440
441 \begin{document}
442 \pagestyle{empty}
443 <<startpreprocsection>>%
444 <<preproccode>>%
445 <<endpreprocsection>>%
446 <<startoutputsection>>%
447 \enlargethispage{100cm}
448
449 % Start of code
450 \begin{pspicture}[linewidth=1bp<<graphstyle>>]<<bbox>>
451 \pstVerb{2 setlinejoin} % set line join style to 'mitre'
452 <<figpreamble>>%
453 <<drawcommands>>
454 <<figpostamble>>%
455 \end{pspicture}
456 % End of code
457 <<endoutputsection>>%
458 \end{document}
459 %
460 <<startfigonlysection>>
461 \begin{pspicture}[linewidth=1bp<<graphstyle>>]<<bbox>>
462 \pstVerb{2 setlinejoin} % set line join style to 'mitre'
463 <<figpreamble>>%
464 <<drawcommands>>
465 <<figpostamble>>%
466 \end{pspicture}
467 <<endfigonlysection>>
468
469
470 Special attributes
471 ==================
472
473 Dot2tex defines several special graph, node and edge attributes. Most of them are not part of the DOT language.
474
475 ``texmode``
476 Changes locally how :ref:`labels` are interpreted.
477
478 ``texlbl``
479 Overrides the current node or edge label.
480
481 ``d2tdocpreamble``
482 Sets the ``<<docpreamble>>`` tag.
483
484 ``d2tfigpreamble``
485 Sets the ``<<figpreamble>>`` tag.
486
487 ``d2tfigpostamble``
488 Sets the ``<<figpostable>>`` tag.
489
490 ``d2tgraphstyle``
491 Sets the ``<<graphstyle>>`` tag.
492
493 ``d2ttikzedgelabels``
494 Sets the ``--tikzedgelabels`` option.
495
496 ``d2tnodeoptions``
497 Sets the ``--nodeoptions`` option.
498
499 ``d2tedgeoptions``
500 Sets the ``--edgeoptions`` option.
501
502 ``style``
503 Used to pass styles to the backend. Styles are output format specific, with the exception of the styles defined by the DOT language.
504
505 ``lblstyle``
506 Used to set styles for drawing graph, node and edge labels. Only works for the ``pgf`` and ``tikz`` output formats.
507
508 ``exstyle``
509 The same as ``lblstyle``, except that ``exstyle`` is ignored in preprocessing mode.
510
511 ``topath``
512 Used to set a ``to`` path operation for connecting nodes. Only works for the ``tikz`` output format.
513
514 ``d2talignstr``
515 Used to pass a default alignment string to the PSTricks ``\rput`` command::
516
517 \rput[d2talignstr] ...
518
519 ``d2toptions``
520 Allows you to pass options to dot2tex in the same format as from the command line. The ``d2toptions`` value is parsed in the same way as ordinary command line options.
521
522
523 .. _external_dot_files:
524
525 Including external dot files
526 ============================
527
528 If your input file contains the single line
529
530 .. sourcecode:: latex
531
532 \input{filename.dot}
533
534 dot2tex will load the ``filename.dot`` file and convert it. This feature is useful when you want to use :ref:`the dot2texi package <dot2texi_package>`, but don't want to include your dot code directly in your document.