Debian Codegraph @

docs/tipsandtricks.rst

raw
1 Tips and tricks
2 ===============
3
4 Fonts
5 -----
6
7 No font information in the DOT file is preserved by dot2tex. However, there are several ways of modifying the generated LaTeX code to achieve some control of fonts and font sizes.
8
9 - Modifying the templates.
10 - Using the ``d2tdocpreamble`` and ``d2tfigpreamble`` attributes or command line options.
11 - Using the ``lblstyle`` attribute.
12
13 To increase the font size you can for instance insert a ``\Huge`` command in the figure preamble::
14
15 $ dot2tex -tmath --figpreamble="\Huge" ex1.dot > ex1huge.tex
16
17 .. figure:: img/ex1huge.*
18 :alt: Setting label font size to \Huge
19
20 Debugging
21 ---------
22
23 When making your own templates it is easy to make mistakes, and LaTeX markup in graphs may fail to compile. To make it easier to find errors, invoke dot2tex with the ``--debug`` option::
24
25 $ dot2tex --preproc --debug test.dot
26
27 A dot2tex.log file will then be generated with detailed information. In the log file you will find the generated LaTeX code, as well as well as the compilation log.
28
29 Be consistent
30 -------------
31
32 Be aware of differences between the template you use for preprocessing and code used to generate final output. This is especially important if you use the ``--figonly`` option and include the code in a master document. If a 10pt font is used during preprocessing, the result may not be optimal if a 12pt font is used in the final output.
33
34 Example. A graph is generated with::
35
36 $ dot2tex --preproc -tmath --nominsize ex1.dot > ex1tmp.dot
37
38 Running through dot2tex again with::
39
40 $ dot2tex --figpreamble="\Huge" ex1tmp.dot > ex1huge.tex
41
42 gives labels that do not fit properly inside the nodes.
43
44 .. figure:: img/consistent.*
45 :alt: Inconsistence between preproc mode and output mode
46
47 Postprocessing
48 --------------
49
50 The output from Graphviz and dot2tex is not perfect. Manual adjustments are sometimes necessary to get the right results for use in a publication. For final and cosmetic adjustments, it is often easier to edit the generated code than to hack the dot source. This is especially true when using the ``tikz`` output format.
51
52 Use the special graph attributes
53 --------------------------------
54
55 Dot2tex has many options for customizing the output. Sometimes is is impractical or boring to type the various options at the command line each time you want to create the graph. To avoid this, you can use the special graph attributes. The ``d2toptions`` attribute is handy because it is interpreted as command line options.
56
57 So instead of typing::
58
59 $ dot2tex -tikz -tmath --tikzedgelabels ex1.dot
60
61 each time, use ``d2toptions`` like this:
62
63 ::
64
65 digraph G {
66 d2toptions ="-tikz -tmath --tikzedgelabels";
67 ...
68 }
69
70
71
72 .. _use-tikz-format:
73
74 Use the tikz output format for maximum flexibility
75 --------------------------------------------------
76
77 The difference between the ``pgf`` and ``tikz`` output formats is best shown with an example. Consider the following graph:
78
79 ::
80
81 graph G {
82 mindist = 0.5;
83 node [shape=circle];
84 a -- b -- c -- a;
85 }
86
87 Rendering the graph using ``circo`` and the ``pgf`` and ``tikz`` output formats::
88
89 $ circo -Txdot simple.dot | dot2tex -tmath -fpgf -s
90 $ circo -Txdot simple.dot | dot2tex -tmath -ftikz -s
91
92 gives visually different graphs:
93
94 .. figure:: img/pgftikzsimple.*
95 :alt: Difference between pgf and tikz output format
96
97 However, the main difference is in the generated code. Here is the ``pgf`` output:
98
99 .. sourcecode:: latex
100
101 % Edge: a -- b
102 \draw [] (19bp,38bp) -- (19bp,60bp);
103 % Edge: b -- c
104 \draw [] (35bp,70bp) -- (55bp,58bp);
105 % Edge: c -- a
106 \draw [] (55bp,40bp) -- (35bp,28bp);
107 % Node: a
108 \begin{scope}
109 \pgfsetstrokecolor{black}
110 \draw (19bp,19bp) ellipse (18bp and 19bp);
111 \draw (19bp,19bp) node {$a$};
112 \end{scope}
113 % Node: b
114 \begin{scope}
115 \pgfsetstrokecolor{black}
116 \draw (19bp,79bp) ellipse (18bp and 19bp);
117 \draw (19bp,79bp) node {$b$};
118 \end{scope}
119 % Node: c
120 \begin{scope}
121 \pgfsetstrokecolor{black}
122 \draw (71bp,49bp) ellipse (18bp and 19bp);
123 \draw (71bp,49bp) node {$c$};
124 \end{scope}
125
126 Compare the above code with the ``tikz`` output:
127
128 .. sourcecode:: latex
129
130 \node (a) at (19bp,19bp) [draw,circle,] {$a$};
131 \node (b) at (19bp,79bp) [draw,circle,] {$b$};
132 \node (c) at (71bp,49bp) [draw,circle,] {$c$};
133 \draw [] (a) -- (b);
134 \draw [] (b) -- (c);
135 \draw [] (c) -- (a);
136
137 The code is much more compact and it is quite easy to modify the graph.
138
139 .. _dot2texi_package:
140
141 The dot2texi LaTeX package
142 --------------------------
143
144 The dot2texi package allows you to embed DOT graphs directly in you LaTeX document. The package will automatically run ``dot2tex`` for you and include the generated code. Example:
145
146 .. sourcecode:: latex
147
148 \documentclass{article}
149 \usepackage{dot2texi}
150
151 \usepackage{tikz}
152 \usetikzlibrary{shapes,arrows}
153
154 \begin{document}
155 \begin{dot2tex}[neato,options=-tmath]
156 digraph G {
157 node [shape="circle"];
158 a_1 -> a_2 -> a_3 -> a_4 -> a_1;
159 }
160 \end{dot2tex}
161
162 \end{document}
163
164 When the above code is run through LaTeX, the following will happen is shell escape is enabled:
165
166 - The graph is written to file.
167 - ``dot2tex`` is run on the DOT file.
168 - The generated code is included in the document.
169
170 The whole process is completely automated. The generated graph will look like this:
171
172 .. figure:: img/dot2texiex1.*
173 :alt: Graph generated with dot2texi
174
175 The ``codeonly`` option is useful in conjunction with ``dot2texi``, especially when used with the ``tikz`` output format. Here is an example that shows how to annotate a graph:
176
177 .. sourcecode:: latex
178
179 \documentclass{article}
180 \usepackage{tikz}
181 \usetikzlibrary{arrows,shapes}
182 \usepackage{dot2texi}
183 \begin{document}
184 % Define layers
185 \pgfdeclarelayer{background}
186 \pgfdeclarelayer{foreground}
187 \pgfsetlayers{background,main,foreground}
188
189 % The scale option is useful for adjusting spacing between nodes.
190 % Note that this works best when straight lines are used to connect
191 % the nodes.
192 \begin{tikzpicture}[>=latex',scale=0.8]
193 % set node style
194 \tikzstyle{n} = [draw,shape=circle,minimum size=2em,
195 inner sep=0pt,fill=red!20]
196 \begin{dot2tex}[dot,tikz,codeonly,styleonly,options=-s -tmath]
197 digraph G {
198 node [style="n"];
199 A_1 -> B_1; A_1 -> B_2; A_1 -> B_3;
200 B_1 -> C_1; B_1 -> C_2;
201 B_2 -> C_2; B_2 -> C_3;
202 B_3 -> C_3; B_3 -> C_4;
203 }
204 \end{dot2tex}
205 % annotations
206 \node[left=1em] at (C_1.west) (l3) {Level 3};
207 \node at (l3 |- B_1) (l2){Level 2};
208 \node at (l3 |- A_1) (l1) {Level 1};
209 % Draw lines to separate the levels. First we need to calculate
210 % where the middle is.
211 \path (l3) -- coordinate (l32) (l2) -- coordinate (l21) (l1);
212 \draw[dashed] (C_1 |- l32) -- (l32 -| C_4);
213 \draw[dashed] (C_1 |- l21) -- (l21 -| C_4);
214 \draw[<->,red] (A_1) to[out=-120,in=90] (C_2);
215 % Highlight the A_1 -> B_1 -> C_2 path. Use layers to draw
216 % behind everything.
217 \begin{pgfonlayer}{background}
218 \draw[rounded corners=2em,line width=3em,blue!20,cap=round]
219 (A_1.center) -- (B_1.west) -- (C_2.center);
220 \end{pgfonlayer}
221 \end{tikzpicture}
222 \end{document}
223
224 .. figure:: img/dot2texiex2.*
225 :alt: Annotated graph
226
227 .. note::
228
229 If you don't want to include the dot directly in your document, you can use the ``\input{..}`` command. See the section :ref:`external_dot_files` for more details.