Debian Codegraph @

texmf-dist/source/latex/graphviz/graphviz.dtx

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