Debian Codegraph @

docs/usage_guide.rst

raw
1 ===========
2 Usage guide
3 ===========
4
5 .. _invoking-dot2tex:
6
7 Invoking dot2tex from the command line
8 ======================================
9
10 Syntax::
11
12 dot2tex [options] [inputfile]
13
14
15 Input data is read from standard input if no input file is specified. Output is written to standard output unless a destination file is set with the ``-o`` option.
16
17 Dot2tex can also be loaded as a module for use in other Python program. See the section :ref:`using-dot2tex-as-a-module` for more details.
18
19 Dot2tex relies on the `xdot format`_ generated by Graphviz. Dot2tex will automatically run ``dot`` on the input data if it is in the plain dot format. If you want to use other layout tools like ``neato`` and ``circo``, use the ``--prog`` option. You can pass options to the layout program with the ``--progoptions`` option.
20
21 .. _xdot format: http://www.graphviz.org/doc/info/output.html#d:xdot
22
23 A few examples on how to invoke dot2tex:
24
25 Read a file from standard input and write the result to the file ``test.tex``::
26
27 $ dot -Txdot test.dot | dot2tex > test.tex
28 $ neato -Txdot -Gstart=rand test.dot | dot2tex > test.tex
29
30 Load ``test.dot``, convert it to xdot format and output the resulting graph using the ``tikz`` output format to ``testpgf.tex``::
31
32 $ dot2tex -ftikz test.dot > testtikz.tex
33
34 The same as above, but use neato for graph layout::
35
36 $ dot2tex --prog=neato -ftikz test.dot > testneato.tex
37
38
39 .. admonition:: Invoking dot2tex
40
41 If you are on Windows and have installed dot2tex from source, you have to type ``python dot2tex`` to invoke the program.
42
43
44
45
46
47 .. _command-line-options:
48
49 Command line options
50 ====================
51
52 The following options are available:
53
54 -h, --help
55 Display help message.
56 -f fmt, --format fmt
57 Set output format. The following values of `fmt` are supported:
58
59 ``pgf``
60 PGF/TikZ. Default.
61 ``pstricks`` or ``pst``
62 Use PSTricks.
63 ``tikz``
64 TikZ format.
65
66 -t mode, --texmode mode
67 Text mode. Specify how text is converted.
68
69 ``verbatim``
70 Text is displayed with all special TeX chars escaped (default).
71 ``math``
72 Output all text in math mode $$.
73 ``raw``
74 Output text without any processing.
75
76 Note that you can locally override the text mode by assigning a special ``texlbl`` attribute to a graph element, or by using the ``texmode`` attribute. See :ref:`labels` for details.
77
78 -s, --straightedges
79 Draw edges using straight lines. Graphviz uses bezier curves to draw straight edges. Use this option to force the use of line to operations instead of curves. Does not work in ``duplicate`` mode.
80
81 -o filename, --output filename
82 Write output to file.
83
84 -d, --duplicate
85 Duplicate the xdot output. Uses the drawing information embedded in the xdot output to draw nodes and edges.
86
87 --template filename
88 Use template from file. See the :ref:`templates` section for more details.
89
90 -V, --version
91 Print version information and exit.
92
93 -w, --switchdraworder
94 Switch drawing order of nodes and edges. By default edges are drawn before nodes.
95
96 -c, --crop
97 Use ``preview.sty`` to crop the graph. Currently only implemented for the ``pgf`` and ``tikz`` output format.
98
99 --figonly
100 Output the graph without a document preamble. Useful if the graph is to be included in a master document.
101
102 --codeonly
103 Output only the drawing commands, without wrapping it in a ``tikzpicture`` or ``pspicture`` environment. Useful when used with the dot2texi package.
104
105 --preproc
106 Preprocess the graph through LaTeX using the preview_ package. Will generate a new dot file where the height and widths of nodes and edge labels are set based on the results from preview_.
107
108 --autosize
109 Preprocess the graph and run Graphviz on the output. Equivalent to::
110
111 $ dot2tex --preproc ex1.dot | dot2tex
112
113 --prog program
114 Set graph layout program to use when the input is in plain dot format. Allowed values:
115
116 - ``dot`` (default)
117 - ``neato``
118 - ``circo``
119 - ``fdp``
120 - ``twopi``
121
122 --progoptions options
123 Pass options to graph layout program.
124
125 --usepdflatex
126 Use pdflatex instead of latex for preprocessing the graph.
127
128 --nominsize
129 Ignore minimum node sizes during preprocessing.
130
131 --valignmode mode
132 Vertical alignment of node labels, where ``mode`` can have the values:
133
134 ``center``
135 Labels are placed in the middle of the node (default).
136 ``dot``
137 Use the coordinate given by the xdot output from Graphviz.
138
139 (``pgf`` and ``pstricks`` only)
140
141 --alignstr
142 Used to pass a default alignment string to the PSTricks ``\rput`` command::
143
144 \rput[alignstr] ...
145
146 Only works for the PSTricks format. PGF/TikZ users can instead pass an ``anchor=...`` style using the ``graphstyle`` option.
147
148 --tikzedgelabels
149 Bypass Graphviz' edge label placement and use PGF/TikZ instead (``tikz`` and ``pgf`` formats only).
150
151 --styleonly
152 Use TikZ only styles when drawing nodes. No ``draw`` or ``shape`` option is added (``tikz`` format only).
153
154 --nodeoptions tikzoptions
155 Wrap node code in a ``scope`` environment with ``tikzoptions`` as parameter (``tikz`` format only).
156
157 --edgeoptions tikzoptions
158 Wrap edge code in a ``scope`` environment with ``tikzoptions`` as parameter (``tikz`` format only).
159
160 --debug
161 Write detailed debug information to the file dot2tex.log in the current directory.
162
163 --pgf118
164 Generate code compatible with PGF 1.18 and earlier.
165
166 --pgf210
167 Generate code compatible with PGF 2.10.
168
169 The following options are used by the output :ref:`templates <templates>`.
170
171 -e encoding, --encoding encoding
172 Set text encoding. Supported encodings are:
173
174 - ``utf8``
175 - ``latin1``
176
177 --docpreamble TeXcode
178 Insert TeX code in the document preamble.
179
180 --figpreamble TeXcode
181 Insert TeX code in the figure preamble.
182
183 --figpostamble TeXcode
184 Insert TeX code in the figure postamble.
185
186 --graphstyle style
187 Sets the ``<<graphstyle>>`` tag.
188
189 --margin margin
190 Set margin around the graph when using ``preview.sty``. ``margin`` must be a valid TeX unit. By default ``margin`` is set to ``0pt``.
191
192 .. _output-formats:
193
194 Output formats
195 ==============
196
197 The output format is specified with the ``-f fmt`` or ``--format fmt`` command line option. The following output formats
198 are available form the command line. Additionally there is a special :ref:`positions output format <positions-output-format>` only available when
199 using dot2tex as Python module.
200
201 .. _output-fomat-pgf:
202
203 PGF
204 ---
205
206 This is the default output format. Generates code for the `Portable Graphics Format`_ (PGF) package . Mixes both PGF and TikZ commands.
207
208 .. _Portable Graphics Format: http://www.ctan.org/pkg/pgf
209
210 .. _output-fomat-pstricks:
211
212 PSTricks
213 --------
214
215 Generates code for the PSTricks_ package.
216
217 .. _output-fomat-tikz:
218
219 TikZ
220 ----
221
222 The ``tikz`` output format also uses the PGF and TikZ package. However, it relies on TikZ node and edge mechanisms to draw nodes and edges, instead of using the drawing information provided by Graphviz. This allows much tighter integration with TikZ and in some cases prettier results.
223
224 Advantages of the ``tikz`` format:
225
226 - The generated code is very compact and clean.
227 - Easy to modify the output.
228 - Labels will fit inside nodes without preprocessing.
229 - Full access to the power of PGF and TikZ.
230
231 You can find more details in the section: :ref:`use-tikz-format`.
232
233 .. note:: The ``tikz`` output format requires detailed knowledge of the PGF and TikZ package. Some of Graphviz' features will not work with this output format.
234
235 .. _labels:
236
237 Labels
238 ======
239
240 The main purpose of dot2tex is to allow text and labels to be typeset by LaTeX. Labels are treated differently according to the current TeX mode:
241
242 ``verbatim``
243 Text is displayed with all special TeX chars escaped (default).
244 ``math``
245 Output all text in math mode $$.
246 ``raw``
247 Output text without any processing.
248
249 The TeX mode can be set on the command line using the ``-t`` option. It can also be set locally in a graph by using the special ``texmode`` attribute.
250
251 You can also use the special ``texlbl`` attribute on a graph element, which is interpreted as ``raw`` TeX string. If a ``texlbl`` attribute is found, it will be used regardless of the current TeX mode. It also has precedence over the ``label`` attribute.
252
253 .. note:: The ``\`` character needs to be escaped with ``\\`` if used in the ``label`` attribute.
254
255 Note that only position and alignment information is converted. Any font information is lost. This may result in some odd behavior. Some tweaking may be necessary to get it right. See the section :ref:`vertical-label-alignment` for tips.
256
257 .. note:: If you use ``texlbl`` for edges, you have to provide a dummy ``label`` attribute. Otherwise Graphviz will not generate the necessary code for placing edge labels.
258
259 Label examples
260 --------------
261
262 Consider the following graph:
263
264 ::
265
266 digraph G {
267 a_1-> a_2 -> a_3 -> a_1;
268 }
269
270 Converting the graph using::
271
272 $ dot2tex -tmath ex1.dot > ex1.tex
273
274 gives the result shown in the left hand side of the figure below. The default rendering is shown to the right. Using the ``raw`` mode will result in a compilation error because of the underscore character.
275
276 .. figure:: img/ex1comb.*
277 :alt: Difference between math and verbatim mode
278
279 Example of using ``texlbl``:
280
281 ::
282
283 digraph G {
284 a_1 [texlbl="$\frac{\gamma}{x^2}$"];
285 a_1-> a_2 -> a_3 -> a_1;
286 }
287
288 .. figure:: img/ex2.*
289 :alt: Using the special texlbl attribute
290
291 Example of using the ``texmode`` attribute:
292
293 ::
294
295 digraph G {
296 a_1 [texlbl="$\frac{\gamma}{2x^2+y^3}$"];
297 a_1 -> a_2 -> a_3 -> a_1
298 node [texmode="math"];
299 a_1 -> b_1 -> b_2 -> a_3;
300 b_1 [label="\\frac{\\gamma}{x^2}"];
301 node [texmode="verbatim"]
302 b_4 [label="\\beta"]
303 a_3 -> b_4 -> a_1;
304 }
305
306 .. figure:: img/texmode.*
307 :alt: Using the special texmode attribute
308
309 The above example shows two important things:
310
311 - The backslash ``\`` character needs to be written as ``\\`` in the ``label`` attribute.
312 - Using LaTeX markup in the ``label`` attribute gives oversized nodes. A workaround is to use the ``texlbl`` attribute, and manually pad the ``label`` attribute to an appropriate length. A much better solution is to use the ``--preproc`` option.
313
314 Preprocessing the above graph with::
315
316 $ dot2tex --preproc ex4.dot | dot2tex > ex4.tex
317
318 gives correctly sized nodes:
319
320 .. figure:: img/texmodeb.*
321 :alt: Preprocessing the graph to get correct node sizes.
322
323 Read more about preprocessing in the `Preprocessing graphs`_ section.
324
325 .. _vertical-label-alignment:
326
327 Vertical label alignment
328 ------------------------
329
330 Dot2tex relies on the xdot format for drawing nodes and placing node labels. The fonts that Graphviz and LaTeX use are different, so using the label coordinates from Graphviz does not always give good results. Dot2tex's default behavior is to place node labels in the middle of the node. However, you can change this behavior by setting the ``valignmode`` option to ``dot``. Labels will then be placed using the coordinates supplied by Graphviz.
331
332 Here is an example graph where it is necessary to use the ``valignmode`` option:
333
334 ::
335
336 digraph G {
337 node0 [label="{left|right}", shape=record];
338 node1 [shape=rectangle, label="node 1"];
339 node0 -> node1;
340 rankdir=LR;
341 }
342
343 For record nodes dot2tex has to use Graphviz coordinates. This is shown in the following figure rendered with::
344
345 $ dot2tex valign.dot
346
347 .. figure:: img/valignmode0.*
348 :alt: Vertical alignment for record shapes
349
350 To get the same vertical alignment for both nodes, you can use::
351
352 $ dot2tex --valignmode=dot valign.dot
353
354 .. figure:: img/valignmode1.*
355 :alt: Vertical alignment with --valignmode=dot
356
357 Now the labels are aligned, but the labels are still placed too low. The reason for this is that both PSTricks and PGF by default centers text vertically on the current coordinate. The alignment point should in this case be set to the baseline. For PGF/TikZ you can use the ``--graphstyle`` option like this::
358
359 $ dot2tex --valignmode=dot --graphstyle="anchor=base" valign.dot
360
361 PSTricks users have to use the ``--alingstr`` option::
362
363 $ dot2tex --valignmode=dot --alignstr=B valign.dot
364
365 .. figure:: img/valignmode2.*
366 :alt: blabla
367
368 The result is better, but to get even better alignment you have to change the node font size. Graphviz' default font size is 14pt, which is larger than the typical 10pt or 11pt used in LaTeX documents. By changing the node font size to 10pt we can trick Graphviz to give us a better alignment:
369
370 ::
371
372 digraph G {
373 node [fontsize=10];
374 node0 [label="{left|right}", shape=record];
375 node1 [shape=rectangle, label="node 1"];
376 node0 -> node1;
377 rankdir=LR;
378 }
379
380 .. figure:: img/valignmode3.*
381 :alt: blabla
382
383
384 .. _preprocessing-graphs:
385
386 Preprocessing graphs
387 ====================
388
389 A problem with using LaTeX for typesetting node and edge labels, is that Graphviz does not know the sizes of the resulting labels. To circumvent this problem, you can use the ``--preproc`` or ``--autosize`` option. The following will then happen:
390
391 1. Node and edge labels are extracted and the corresponding LaTeX markup is saved to a temporary file.
392 2. The file is typeset with LaTeX and information about sizes is extracted using the preview_ package.
393 3. A new dot file is created where node and edge label sizes are set using the dot language's ``width`` and ``height`` attributes.
394 4. The generated graph can now be processed using Graphviz and dot2tex. Label sizes will now correspond with the output from LaTeX.
395
396 Widths and heights of nodes are handled the in same way as Graphviz does it. The ``width`` and ``height`` attributes set the minimum size of the node. If label size + margins is larger that the minimum size, the node size will grow accordingly. Default values are width=0.75in and height=0.5in.
397
398 Node margins are set using the `margin`_ attribute. This also works for edge labels. ``margin==value`` sets both the horizontal and vertical margin to ``value``, ``margin=="hvalue,vvalue"`` sets the horizontal and vertical margins respectively.
399
400 .. note:: All sizes are given in inches.
401
402 If you do not want a minimum node size, you can use the '--nominsize' option. Dot2tex will then use size of label + margins as node size.
403
404 Nodes with ``fixedsize=True`` attributes are not processed.
405
406 Limitations:
407
408 - Does not work for HTML-labels
409 - Does not work for record-based nodes
410
411 .. _preprocessing-examples:
412
413 Examples
414 --------
415
416 Consider the following graph:
417
418 ::
419
420 digraph G {
421 node [shape=circle];
422 a_1 [texlbl="$x^2+\frac{\sin y}{y^2+\cos \beta}+\gamma_3$"];
423 a_1 -> a_2 [label=" ", texlbl="$x_1+x_3^2+z+c+v~~$"];
424 a_2 -> a_1;
425 }
426
427 Rendered with::
428
429 $ dot2tex -tmath example.dot > example.tex
430
431 the graph will look like this:
432
433 .. figure:: img/preproc1a.*
434 :alt: Graph with oversized edge and node labels
435
436 You could improve the result by adding a longer ``label`` attribute or setting a fixed width. A better solution is to preprocess the graph like this::
437
438 $ dot2tex -tmath --preproc example.dot > exampletmp.dot
439 $ dot2tex exampletmp.dot > example.tex
440
441 You can also chain the commands::
442
443 $ dot2tex -tmath --preproc example.dot | dot2tex > example.tex
444
445 A shorter alternative is::
446
447 $ dot2tex -tmath --autosize example.dot > example.tex
448
449 The resulting graph now has correctly sized nodes and edge labels:
450
451 .. figure:: img/preproc1b.*
452 :alt: Graph with preprocessed labels
453
454 Modifying node sizes using the ``widht/height`` and ``margin`` attributes can be a bit counterintuitive. A few examples will hopefully make it clearer:
455
456 ::
457
458 digraph G {
459 node [shape=rectangle];
460 a_1 [margin="0"];
461 a_2 [margin="0.7,0.4"];
462 a_3 [width="2",height="1"];
463 a_1-> a_2 -> a_3 -> a_1;
464 }
465
466 Processing the graph with::
467
468 $ dot2tex -tmath --preproc example.dot | dot2tex > example.tex
469
470 gives
471
472 .. figure:: img/nodewidth1.*
473 :alt: Graph with preprocessed labels
474
475 Setting the margin of ``a_1`` to 0 has no effect because of the minimum node width. Processing the graph with::
476
477 $ dot2tex -tmath --preproc --nominsize example.dot | dot2tex > example.tex
478
479 gives a different graph, where only label widths and margins affect the node sizes:
480
481 .. figure:: img/nodewidth2.*
482 :alt: Graph with preprocessed labels
483
484
485 .. _margin: http://graphviz.org/doc/info/attrs.html#d:margin
486
487
488 .. include:: common.inc
489