roff-documentation/Drawing Graphs with gpic - ...

316 lines
12 KiB
HTML

<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<title>Drawing Graphs with gpic - Douglas B. West</title>
</head>
<body><h1>Drawing Graphs with gpic - Douglas B. West</h1>
Various people over the years have asked me how I draw the figures
for my books, such as
<a href="https://faculty.math.illinois.edu/~west/igt"><i>Introduction to Graph Theory</i></a>,
<a href="https://faculty.math.illinois.edu/~west/mt"><i>Mathematical Thinking: Problem-Solving and
Proofs</i></a> (with John D'Angelo),
and the forthcoming
<a href="https://faculty.math.illinois.edu/~west/0cont.ps"><i>Combinatorial Mathematics</i></a>.
All these figures are drawn using gpic, the GNU (Free Software Foundation)
version of the program pic developed originally by AT&amp;T. gpic is part of
the freely available groff document formatting system.
<p>
The program pic was developed for drawing pictures in troff documents,
I believe in 1984. The original version of gpic was intended as a
preprocessor for groff. Sometime in the late 1990s, a command-line option
was added to gpic to allow it to serve as a preprocessor for TeX.
This allowed me to migrate my books from troff to TeX.
</p><p>
Since mostly I want to draw graphs, I wrote macros that facilitate drawing and
labeling graphs of various sorts in a few lines of gpic input.
I provide the <a href="https://faculty.math.illinois.edu/~west/tpics.txt">macro package</a> as a text file with
essentially no documentation. On this page I give a brief summary of how to
use it, plus a few examples at the end.
More extensive <a href="http://www.kohala.com/start/troff/troff.html">reference
materials</a> on pic and gpic themselves, including old user manuals, have been
gathered by Richard Stevens.
</p><p>
My macro package is inelegant and could be made cleaner and more sophisticated.
However, it does enable me to draw the pictures I want to draw rather quickly
and with very little code.
</p><p>
Please send feedback on how this page can be improved to
west @ math . uiuc . edu.
Also, please let me know if you use these macros.
</p><h2>Basics</h2>
<b>Locations and arguments</b><br>
gpic is coordinate-based. Locations are described as ordered pairs "(a,b)"
in the coordinate plane. Locations (or objects) can be named using colons, as
in "A:(a,b)". Names remain in effect until reassigned; in particular, they
persist into later pictures. To put multiple commands on a single line, end
each with a semicolon.
<p>
In order to facilitate placing vertices at grid points, I have defined
names for these locations. Available are "<b>p00</b>" through "<b>p99</b>"
and "<b>p100</b>" through "<b>p155</b>", where "<b>p</b>ij" denotes "(i,j)",
and the other locations are from (10,0) through (15,5). Offset locations
are also available, using "<b>op00</b>" through "<b>op99</b>"
and "<b>op100</b>" through "<b>op155</b>". Here "<b>op</b>ij" is defined
as "O+(i,j)" for the particular location named as "O", which can be viewed
as an "origin". A picture with several copies of the same structure can
be produced by using the same commands with a change in the specification of
"O".
</p><p>
Lines and arrows are drawn from location to location. gpic macros have a fixed
list of up to nine arguments, invoked as <b>macro</b>(arg1,arg2,...,argN).
Therefore, many of my macros come in sets indexed up to 9, depending
on how many points are being placed or connected by lines.
The syntax for macro definitions is
<br>
"define macroname #command1; command2; etc#"
</p><p>
<b> "spot", "spot2", . . ., "spot9"</b><br>
The arguments for these macros are locations in the plane. "<b>spotN</b>"
takes N such locations and puts bullets there as vertices. It also names those
locations in order, A, B, C, . . . Those names can be used as arguments
in later macros. This enables editing of pictures by moving vertices
around without retyping lots of locations. If more than nine vertices are
needed, than one can save the locations with other names, as in "A1:A;";
each use of a "<b>spotN</b>" macro assigns names starting from "A".
</p><p>
<b> "poly2", . . ., "poly20" </b><br>
"<b>polyN</b>(loc,rad)" places N equally spaced vertices around an invisible
circle with center "loc" and radius "rad", naming the locations beginning with
A at the top (as in <b>spotN</b>) and moving clockwise. This is done via a
small table of sines and cosines present in the macro package.
For odd N, only up to N=13 is provided.
When N is even, the (invisible) polygon sits on a horizontal base;
the version "<b>polyNv</b>" (and maybe also "<b>polyNh</b>" for N=12
and N=16") puts a point at the top and bottom.
</p><p>
<b> "edge", "degr", "matc", "path", "cycl"</b><br>
"<b>edge</b>(A,B)" draws a line segment connecting two locations A and B.
<br>
"<b>degrN</b>" draws edges from its first argument to the other N arguments,
up to N=8.
<br>
"<b>matcN</b>" draws a matching of size N on its 2N arguments, for N\in{2,3,4}.
For example, "<b>matc4</b>(A,B,C,D,E,F,G,H)" draws the edges AB, CD, EF, and GH.
<br>
"<b>pathN</b>" draws a path through the locations of its N arguments in order.
<br>
"<b>cyclN</b>" draws a cycle through the locations of its N arguments in order.
<br>
The gpic primitive for these macros is "line", as in "line from A to B to C
to D to E".
</p><p>
<b> "call", "call2", "call3"</b><br>
These place text at a specified location.
In "<b>call</b>(A,"text $v_1$",ne);" the text inside quotation marks is
placed near location A, in the northeast direction away from A.
There are eight compass directions plus "c" for center.
Note that text is passed through unchanged, to be processed by TeX (or troff),
and it will be produced in the fonts used by the subsequent program. Thus
notation is placed within dollar signs.
To place text at locations A,B or A,B,C, use
"<b>call2</b>(A,B,"text1","text2",direc1,direc2);" or
"<b>call3</b>(A,B,C,"text1","text2","text3",direc1,direc2,direc3);".
</p><p>
<b> "circ", "arch", "elli"</b><br>
"<b>circ</b>(A,rad)" draws a circle of radius "rad" centered at location A.
<br>
"<b>arch</b>(A,B,rad)" draws a circular arc of radius "rad" counterclockwise
from location A to location B.
<br>
"<b>elli</b>(A,ht,wid)" draws an ellips of height "ht" and width "wid"
centered at location A.
<br>
Circless and ellipse are objects with centers and directional corners.
The objects can be named, as in "C:circ(p00,2);" and then "C.ne" subsequently
refers to the northeast point on C.
</p><p>
<b>"spli3", . . ., "spli9"</b><br>
"<b>spliN</b>" draws a curved line (via splines) from
A to B to C, etc., those being its N arguments in order.
Only the first and last locations are actually touched by the curve.
Try it.
</p><p>
<b> Arrow, Bold, Dashed, Dotted</b><br>
Generally speaking, many of the macros for edge, path, cycl, degr, matc, spli,
can be modified by prepending "a", "b", "f", "d", "t" to obtain lines that
are arrows (directed edges), bold (thicker), fat (much thicker), dashed, or
dotted, respectively. In combination, <b>daedge</b>, <b>baedge</b>, and
<b>dapath</b> are also available.
<br>
The size of arrowheads is constant, but the extent of coordinates in pictures
is not. As a result, arrowheads may appear too large or too small. To fix
this, use "<b>sethead</b>(param)" to multiply the scale of arrowheads by
"param". At the end of the picture, use "<b>resethead</b>" to restore the
original scale; otherwise the new size arrowheads will be used in succeeding
pictures.
</p><h2>Invocation in TeX Documents</h2>
Within a TeX document, the source for a gpic picture appears as follows:
<br>\gpic{
<br>.PS x
<br>gpic commands
<br>.PE
<br>}
<p>
The "\gpic" macro is defined within the macro package. The "x" is the
desired width of the picture in inches. When the file is run through the
gpic program before sending it to tex or latex, the portion from
.PS to .PE will be replaced with the "specials" that direct tex to draw the
lines and other elements of the picture. Under tex, the macro "\gpic" then
creates a centered box with the picture inside it.
</p><p>
The command-line option "-t" instructs gpic to prepare its output for
TeX rather than for troff. To run the preprocessor and then tex, one therefore
uses something like the following script:
<br>gpic -t gpicmacros texsource &gt; temp.tex
<br>tex temp
</p><p>
To get pdf output, the resulting dvi file should be hit with
<br>
dvips -Ppdf | ps2pdf
</p><h2>Further Macros and Comments</h2>
<b>Filled and Invisible</b><br>
"<b>icirc</b>(A,rad)" draws an invisible circle, which may be useful when
named in order to refer to directional locations.
<br>
"<b>fcirc</b>(A,rad,fill)" draws a circle of radius "rad" centered at location
A with the interior filled to density "fill", between 0 and 1. The use of
"fcirc" will overwrite anything that was previous put in the interior of the
circle.
<br>
"<b>ficirc</b>(A,rad,fill)" draw a filled interior without the boundary.
<br>
"<b>felli, fielli</b>" are the analogues for ellipses.
<p>
<b>"h1,...,h9", "v1,...,v9"</b><br>
Like "<b>p</b>ij", these are shorthand for coordinate pairs, with
"<b>h</b>j" being (.j,0) and "<b>v</b>j" being (0,.j).
They can be used for making slight adjustments to locations for placing
text labels.
</p><p>
"<b>spoto</b>(A)" puts an open circle (instead of a bullet) at location A.
<br>
"<b>mark</b>(A)" puts a square at location A (to distinguish a vertex).
<br>
"<b>cliqN</b>" draws a complete graph with vertices at its N arguments,
for N\in{4,5,6}.
<br>
<b>"k23", "k33", "k34", "k44"</b> draw complete bipartite graphs.
</p><h2>Examples</h2>
Below is the gpic code using these macros to produce several of the
figures in Section 1.1 of <i>Introduction to Graph Theory</i>,
second edition.
<p>
Petersen graph, page 13:
<br>
\gpic{
<br>
.PS 3.8
<br>
O:p00; poly5(O,2); cycl5(A,B,C,D,E); A1:A; B1:B; C1:C; D1:D; E1:E;
<br>
poly5(O,1); cycl5(A,C,E,B,D); matc3(A,A1,B,B1,C,C1); matc2(D,D1,E,E1);
<br>
call3(A1,B1+h1,C1+h1,"12","34","51",n,ne,se); call2(D1-h1,E1-h1,"23","45",sw,nw)
<br>
call3(A+h1,B-v1,C-h1,"35","52","24",e,s,sw); call2(D,E,"41","13",nw,n);
<br>
O:(5.5,0)+v1; poly3(op00,-.8); degr3(O,A,B,C); A1:A; B1:B; C1:C; spot(O);
<br>
poly6(op00,2); cycl6(A,B,C,D,E,F); path3(A,B1,D); path3(B,A1,E); path3(C,C1,F);
<br>
O:p110+v1; poly9(O,2); cycl9(A,B,C,D,E,F,G,H,I); spot(O); degr3(O,A,D,G);
<br>
matc3(B,F,E,I,H,C);
<br>
.PE
<br>
}
</p><p>
Decomposition of <i>K<sub>5</sub></i> and <i>K<sub>4</sub></i>, page 11:
<br>
\gpic{
<br>
.PS 2.6
<br>
poly5((0,.85),1.15); bcycl5(A,B,C,D,E); cycl5(A,C,E,B,D);
<br>
spot4(p30,p50,p52,p32); path3(A,B,D); dpath3(B,C,A); bpath3(C,D,A);
<br>
.PE
<br>
}
</p><p>K\"onigsberg Bridge Problem, page 2:
<br>
\gpic{
<br>
.PS 4.0
<br>
elli(p64,4,6); call3(p64,p96+h2,p92+h2,"$W$","$X$","$Z$",c,ne,se);
<br>
call3(p124+h2,p55-v3,p75-v3,"$Y$","1","2",e,nw,ne);
<br>
call3(p53+v1,p73+v1,p84-h2,"3","4","5",sw,se,e);
<br>
call3(p115+h2,p113+h2,p114,"6","7","$~$",s,n,w);
<br>
spli7((14,6.4),(12,5.4),(9.8,5.0),(9.2,4),(9.8,3.0),(12,2.6),(14,1.6));
<br>
spli7((1.0,3.6),(2.6,3.4),(4,2),(6,1.4),(9.2,2.6),(12,2),(14,.8));
<br>
spli7((1.0,4.4),(2.6,4.6),(4,6),(6,6.6),(9.2,5.4),(12,6),(14,7.2));
<br>
matc2((8.7,4.1),(9.6,4.1),(8.7,3.9),(9.6,3.9));
<br>
matc4((4.7,1.4),(5.0,2.4),(4.5,1.5),(4.8,2.5),(4.7,6.6),(5.0,5.6),(4.5,6.5),(4.8,5.5));
<br>
matc4((7.3,1.4),(7.0,2.4),(7.5,1.5),(7.2,2.5),(7.3,6.6),(7.0,5.6),(7.5,6.5),(7.2,5.5));
<br>
matc4((11.0,1.8),(11.5,3.1),(11.2,1.7),(11.7,3.0),(11.0,6.2),(11.5,4.9),(11.2,6.3),(11.7,5.0));
<br>
O:(17,1);
<br>
spot4(op30,op03,op36,op63); degr3(D,A,B,C); dbledge(A,B,6.5); dbledge(B,C,6.5);
<br>
call3(C+v1,D,A-v1,"$x$","$y$","$z$",n,e,s); E1:.5&lt; B,C&gt;; E3:.5&lt; A,B &gt;;
<br>
E5:.5&lt; B,D&gt;; E6:.5&lt; C,D&gt;; E7:.5&lt; D,A&gt;;
<br>
call3(B-h2,E1-h3+v3,E1+h2-v2,"$w$","$e_1$","$e_2$",w,nw,se);
<br>
call3(E3-h2-v2,E3+h2+v2,E5,"$e_3$","$e_4$","$e_5$",sw,ne,n);
<br>
call3(E6,E7,E5,"$e_6$","$e_7$","$~$",ne,se,s);
<br>
.PE
<br>
}
</p><div id="grammalecte_menu_main_button_shadow_host" style="width: 0px; height: 0px;"></div></body><script src="Drawing%20Graphs%20with%20gpic%20-%20Douglas%20B.%20West_files/api.js"></script></html>