summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/graphviz/graphviz.dtx
blob: 5380bb67e54e5a2b520b46cb1d47657584cb1d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
% \iffalse meta-comment
%
% Copyright (C) 2003-2013 by Derek Rayside <drayside@uwaterloo.ca>
% --------------------------------------------------
% 
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3 of this license
% or (at your option) any later version.  The latest version of this
% license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.3 or later is part of all distributions of LaTeX
% version 1999/12/01 or later.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{graphviz} 
%<package>   [2013/08/15 v0.94 .dtx graphviz file] 
%
%<*driver>
\documentclass{ltxdoc} 
\RequirePackage[psfrag]{graphviz} 
\RequirePackage{url}
\RequirePackage{calc}
\EnableCrossrefs
\CodelineIndex 
\RecordChanges 
\begin{document} 
  \DocInput{graphviz.dtx}
\end{document}
%</driver> \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
%
%
% \changes{v0.1}{2003/11/24}{Initial version}
% \changes{v0.4}{2004/05/14}{converted to dtx format}
% \changes{v0.5}{2004/05/19}{renamed package to \textsf{dotla}}
% \changes{v0.7}{2005/12/29}{renamed package back to \textsf{graphviz}}
%
% \GetFileInfo{graphviz.sty}
%
% \DoNotIndex{\newcommand,\newenvironment}
% 
%
% \title{\vspace*{-0.5in}The \textsf{graphviz} package\thanks{This document
% corresponds to \textsf{graphviz}~\fileversion, dated \filedate.}}
% \author{Derek Rayside $\langle$\texttt{drayside@uwaterloo.ca}$\rangle$ \\
% with contributions from 
% Ralf Hemmecke $\langle$\texttt{ralf@hemmecke.de}$\rangle$  }
%
% \maketitle
% 
% \setlength{\parskip}{1ex}
% \setlength{\parindent}{0ex}
%
% \section{Introduction}
%
% \textsf{graphviz.sty} is a \LaTeX\ package for writing
% \textsf{graphviz/dot/neato} graphs inside of \LaTeX\ documents.
% \textsf{graphviz.sty} was inspired by a feature that Daniel Jackson added
% to his \textsf{tagger} text markup tool.
%
% \textsf{graphviz} is a freely available package for doing automated
% graph layout from AT\&T Research, distributed under the Common
% Public License (CPL).  
% \textsf{graphviz}
% includes the \textsf{dot} and \textsf{neato} programs, which read a textual
% description of a graph and produces a graphical rendering of it.
% Many different graphics formats, include PostScript, are supported.
%
% There are two main web pages for the \textsf{graphviz} project:
% 
% \begin{itemize}
%
%     \item  \url{http://www.graphviz.org}
%
%     \item  \url{http://www.research.att.com/sw/tools/graphviz/}
% 
% \end{itemize}
%
% \textsf{graphviz.sty} is provided as-is, with no warranty or claim to
% fitness for any purpose, use at your own risk, etc.
% \textsf{graphviz.sty} 
% is distributed under the \LaTeX\ Project Public License.
%
% \section{Example}
%
% Put this in your document:
%
%   \hspace*{2em}|\digraph[scale=0.5]{abc}{rankdir=LR; a->b->c;}|
% 
% Run these commands (only the first run needs \texttt{-shell-escape}):
%
%   \hspace*{2em}|latex -shell-escape main.tex| \\
%   \hspace*{2em}|latex main.tex|
%
%
% And here's what you get:
% 
% \vspace{1ex}
% 
%   \hspace*{2em}\digraph[scale=0.5]{abc}{rankdir=LR; a->b->c;}
%
% \vspace{1ex}
%
% 
% \newpage
% \section{Usage}
%
% \DescribeMacro{\digraph\oarg{i}\marg{n}\marg{g}}
% The |\digraph| (\textsf{dot}) and |\neatograph| (\textsf{neato}) 
% commands take three arguments:
% 
% \begin{enumerate}
% 
%     \item[\oarg{i}]  parameters to the |\includegraphics|
%     command that will include the PostScript file of the graph
%     [this is optional]: eg, `\texttt{scale=0.5}'
% 
%     \item[\marg{n}]  the name of the graph; a file \texttt{name.dot} 
%     is created,
%     and a file \texttt{name.ps} is expected to be produced from
%     \textsf{dot}: eg, `\texttt{MyGraph}' \\
%     \marg{n} has to be a valid file name and a valid identifier name.
% 
%     \item[\marg{g}]  the graph, specified in the \textsf{dot/graphviz} 
%     language:
%     \\ eg, `\texttt{rankdir=LR; a->b->c;}'
% 
% \end{enumerate}
% 
% \section{Options}
%
%
% \begin{macro}{singlefile}
% \changes{v0.6}{2005/12/03}{added singlefile option}
% \changes{v0.7}{2005/12/29}{now using gvpr instead of gawk to break out individual digraphs from master.graphviz}
% \changes{v0.94}{2013/08/15}{writing gvpr commands to separate script to be executed when master.graphviz is closed}
%
% \LaTeX\ has a small number of file handles (about 16 or so).  So if
% you can't have too many digraphs in your tex file before you run out
% of file handles.  The \texttt{singlefile} option is a work-around:
% it writes all of your digraphs to a single file
% (\texttt{tmpmaster.graphviz}), and then uses \textsf{gvpr} to split that 
% file into individual dot files for processing by \textsf{dot}.
%
% The \textsc{gvpr} commands are all written to a second file 
% (\texttt{tmpmaster.gvpr}), which
% is executed once the \texttt{tmpmaster.graphviz} file has been closed.
%
% \textsf{gvpr} does not seem to be packaged with the Windows version
% of \textsf{dot}.
% 
%    \begin{macrocode}
\newif\ifsinglefile
\DeclareOption{singlefile}{
    \singlefiletrue
    \AtBeginDocument{% open a new file handle
        \newwrite\masterdotfile%
        \immediate\openout\masterdotfile=\@tmpdir tmpmaster.graphviz%
        \newwrite\mastergvprfile%
        \immediate\openout\mastergvprfile=\@tmpdir tmpmaster.gvpr}
    \AtEndDocument{% close the file
        % close the dot file and the gvpr file
        \immediate\closeout\masterdotfile%
        \immediate\closeout\mastergvprfile%
        % execute the gvpr file
        \immediate\write18{gvpr -f \@tmpdir tmpmaster.gvpr \@tmpdir tmpmaster.graphviz}%
        }}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{psfrag}
% \changes{v0.8}{2005/12/30}{added psfrag option}
%
% The \texttt{psfrag} option uses the \textsf{psfrag} package to
% enable you to overlay \TeX\ fragments over included postscript
% files, such as those generated via the |\digraph| command.
%
% The \textsf{ladot} script from Brighten Godfrey uses Perl to extend
% the syntax of the graphviz language with \TeX\ fragments, and 
% \textsf{psfrag} to super-impose those fragments.  
%
% The \texttt{psfrag} option requires sed.
% \textsf{psfrag} seems to only work with \textsf{dvips}: ie, it is
% not compatible with \textsf{pdflatex} or \textsf{dvipdfm}.
% The PDF files produced by \LaTeX/\textsf{psfrag/ps2pdf} seem to view 
% ok with \textsf{Acrobat}, but not with \textsf{gv}.  
% Oddly, the PS files produced this way work in \textsf{gv}.
%
% Put this in your document:
%
%   \hspace*{2em}|\psfrag{x2}[cc][cc]{$x^2$}| \\
%   \hspace*{2em}|\digraph{xy}{rankdir=LR; x2->y;}|
% 
% And here's what you get:
% 
% \vspace{1ex}
% 
%   \hspace*{2em}\psfrag{x2}[cc][cc]{$x^2$} \digraph[scale=0.5]{xy}{rankdir=LR; x2->y;}
%
% \vspace{1ex}
%
% 
%
%    \begin{macrocode}
\newif\ifpsfrag
\DeclareOption{psfrag}{ \psfragtrue }
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{ps}
% \changes{v0.92}{2013/08/15}{added ps option (previously default behaviour)}
%
% Tell Graphviz to generate Postscript files as output.
% 
%    \begin{macrocode}
\newcommand{\@outext}{ps}
\newcommand{\@outextspace}{ps }
\DeclareOption{ps}{
    \renewcommand{\@outext}{ps}
    \renewcommand{\@outextspace}{ps }}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{pdf}
% \changes{v0.92}{2013/08/15}{added pdf option}
%
% Tell Graphviz to generate PDF files as output.
% 
%    \begin{macrocode}
\DeclareOption{pdf}{%
    \renewcommand{\@outext}{pdf}%
    \renewcommand{\@outextspace}{pdf }}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{tmpdir}
% \changes{v0.93}{2013/08/15}{added tmpdir option}
%
% Write all generated files in |./tmp/|
% 
%    \begin{macrocode}
\newcommand{\@tmpdir}{}
\DeclareOption{tmpdir}{%
    \immediate\write18{mkdir ./tmp/}%
    \renewcommand{\@tmpdir}{./tmp/}}
%    \end{macrocode}
% \end{macro}
%
%
%
% \paragraph{Set the default options}
%
%    \begin{macrocode}
\ExecuteOptions{ps}
\ProcessOptions\relax % LaTeX class guide says it is wise to relax
%    \end{macrocode}
%
%
%
% \StopEventually{\PrintChanges\PrintIndex}
%
% \section{Implementation}
%
% \subsection{Required Packages}
% This package requires \textsf{graphicx} to include PostScript
% renderings of graphs.
%    \begin{macrocode}
\RequirePackage{graphicx}
\ifpsfrag \RequirePackage{psfrag} \fi
%    \end{macrocode}
%
% \subsection{Command Implementation}
%
% \begin{macro}{\digraph}
% \changes{v0.2}{2003/12/24}{minor adjustments}
% \changes{v0.4}{2004/05/14}{new comments}
% \changes{v0.5}{2004/05/19}{added automatic invocation of dot}
% \changes{v0.6}{2005/12/03}{added singlefile option}
% \changes{v0.7}{2005/12/29}{added backslash-hyphen line breaks by Ralf Hemmecke}
% \changes{v0.7}{2005/12/29}{now using gvpr instead of gawk to break out individual digraphs from master.graphviz}
% \changes{v0.7}{2005/12/29}{removed redundant invocation of dot from digraph; only inputdigraph needs to invoke dot}
% \changes{v0.9}{2006/01/08}{refactored for control-M by Ralf Hemmecke}
% \changes{v0.91}{2013/04/25}{a bit of cleanup and modernization}
%
% This is the command the user uses for \textsf{dot}.
%
% It is very important that this command is not defined with 3
% parameters although it will be used with 3 parameters in the
% form |\digraph[OPTIONS]{FILENAME}{GRAPH}|.
% The reason is that the catcode for |^^M|
% must be changed \emph{before} \TeX\ 
% reads the GRAPH argument.
%
% The order of the command (first |\inputdigraph| then |\@digraph|)
% may look a bit odd, but it simplifies the code. In order to include
% the digraph, \LaTeX\ has to be run at least two times anyway. In the
% first run the file \textsf{dot} will be generated and only
% the second run the digraph will be included.
% 
%    \begin{macrocode}
\newcommand{\digraph}[2][scale=1]{
  \inputdigraph[#1]{#2}{dot}%       % Include the generated ps/pdf.
  \@digraph{digraph}{#2}%           % Generate the .dot file.
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\neatograph}
% \changes{v0.9}{2006/01/11}{added support for neato}
%
% This is the command the user uses for \textsf{neato}. The syntax is
% the same as for |\digraph|.
%
%    \begin{macrocode}
\newcommand{\neatograph}[2][scale=1]{
  \inputdigraph[#1]{#2}{neato}%     % Include the generated ps/pdf.
  \@digraph{graph}{#2}%             % Generate the .dot file.
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@digraph}
% Internal implementation.
%
% The macro |\@digraph| prepares the actual output of the digraph to a
% file (which is done by |\@@digraph|) by a special treatment of the
% newline character. 
% Before entering |\@@digraph|, the input newline character (|^^M|)
% is made active, and redefined to expand to |^^J|.
% Note that |\@digraph| has a |\begingroup| that is closed in
% |\@@digraph|.
%
% The purpose of this is to preserve line breaks in the digraph.
%
%    \begin{macrocode}
\begingroup
  \catcode`\^^M=\active%
  \gdef\@digraph{\begingroup\catcode`\^^M=\active\def^^M{^^J}\@@digraph}%
\endgroup
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@digraph}
% Internal implementation.
%
% The parameters of the macro |\@@digraph| are the TYPE, FILENAME and GRAPH
% of the initial |\digraph[OPTIONS]{FILENAME}{GRAPH}|.
% Note that if |\@@digraph| is entered the |^^M| character is active.
% Thus every newline character (|^^M|) in the following macro is
% hidden through a |%| sign at the end of line.
%    \begin{macrocode}
\def\@@digraph#1#2#3{%
    \ifsinglefile% write the graph to the master file
        \expandafter\def\csname -\endcsname{\string\n}%
        \immediate\write\masterdotfile{#1 #2 {#3}}%
        \immediate\write\mastergvprfile{BEG_G { if ($.name == "#2") {writeG($G,"\@tmpdir#2.dot");} }}%
    \else% open a new file handle
        \newwrite\dotfile%
        \immediate\openout\dotfile=\@tmpdir#2.dot%
        \expandafter\def\csname -\endcsname{\string\n}%
        \immediate\write\dotfile{#1 #2 {#3}}%
        \immediate\closeout\dotfile%
    \fi%
% Here comes the closing \endgroup that closes the group opened in \@digraph.
    \endgroup}%
% Now ^^M is no longer active.
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\inputdigraph}
% \changes{v0.2}{2003/12/24}{minor adjustments}
% \changes{v0.8}{2005/12/30}{added psfrag support}
%
% This is usually only called by |\digraph|,
% but may be called by the user.
%
% The purpose is to include the ps/pdf rendering of the graph if
% it exists, or to give instructions on how to generate it.
%
%    \begin{macrocode}
\newcommand{\inputdigraph}[3][scale=1]{
    % execute dot or neato (nb: requires latex -shell-escape)
    \immediate\write18{#3 -T\@outextspace -o \@tmpdir#2.\@outextspace \@tmpdir#2.dot}
    \IfFileExists{\@tmpdir#2.\@outext}{ % the postscript/pdf exists: include it
            \ifpsfrag
                % per the ladot 2.2 source code, psfrag has a problem with
                % graphviz 2.2, and some sed hackery is necessary to work around
                \write18{sed -ibackup -e "s/xshow/pop show/g" \@tmpdir#2.ps}
            \fi
            \includegraphics[#1]{\@tmpdir#2.\@outext} 
        }
        % else: the postscript/pdf doesn't exist: tell the user how to create it
        { 
            \fbox{ \begin{tabular}{l}
                The file \texttt{#2.\@outext} hasn't been created from 
                \texttt{\@tmpdir#2.dot} yet. \\ 
                Run `\texttt{dot -T\@outextspace -o \@tmpdir#2.\@outextspace \@tmpdir#2.dot}' 
                to create it. \\
                Or invoke \LaTeX\ with the \texttt{-shell-escape} option
                to have this done automatically. \\
                \end{tabular}}
        }
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Process}
% 
% |\digraph| writes out a \textsf{dot} file, and then invokes
% \textsf{dot} on it.  
% 
% Note:  |\digraph| can only invoke \textsf{dot} if the \LaTeX\ was
% invoked with the \texttt{-shell-escape} option, to enable execution of
% external programs.  If you do not want to allow \LaTeX\ to execute
% external programs, then you will have to invoke \textsf{dot} yourself.
% \textsf{graphviz} will also need to execute \textsf{gvpr} if the 
% \texttt{singlefile} option has been selected, and \textsf{sed} if
% the \texttt{psfrag} option has been selected.
% 
% Here's a picture of the process (drawn with \textsf{dot},
% naturally). The picture shows the process using dvips, but pdflatex
% is now also supported with the pdf option.
%
% \begin{center}
% 
% \digraph[height=\textheight-2in]{process}{
%     /* PROGRAMS */
%     latex1 [label="latex -shell-escape x.tex", shape=hexagon, fontname=Courier];
%     latex2 [label="latex x.tex", shape=hexagon, fontname=Courier];
%     dvips [label="dvips -o x.ps x.dvi", shape=hexagon, fontname=Courier];
%     /* FILES */
%     xTex [label="x.tex", fontname=Helvetica, shape=ellipse];
%     xDvi [label="x.dvi", fontname=Helvetica, shape=ellipse];
%     xPS [label="x.ps", fontname=Helvetica, shape=ellipse];
%     subgraph cluster0 {
%         label="graphviz.sty magic";
%         dot [label="dot -Tps -o a.ps a.dot", shape=hexagon, fontname=Courier];
%         gvpr [label="gvpr master.graphviz", shape=hexagon, fontname=Courier];
%         aPS [label="a.ps", fontname=Helvetica, shape=ellipse];
%         sed [label="sed s/xshow/pop show/g a.ps", shape=hexagon, fontname=Courier];
%         aDot [label="a.dot", fontname=Helvetica, shape=ellipse];
%         master [label="master.graphviz", fontname=Helvetica, shape=ellipse];
%     };
%     /* COMMON EDGES */
%     xTex -> latex1;
%     xTex -> latex2;
%     latex2 -> xDvi;
%     xDvi -> dvips;
%     dvips -> xPS;
%     latex1 -> master [style=dotted, label=singlefile];
%     latex1 -> aDot [style=dotted, label="multiple files"];
%     master -> gvpr;
%     gvpr -> aDot;
%     aDot -> dot;
%     dot -> aPS;
%     aPS -> sed;
%     sed -> aPS;
%     aPS -> dvips;
%     aPS -> latex2;
% }
% \end{center}
%
% \newpage
%
%
%
% \Finale
\endinput