groff before CVS: release 1.11
[s-roff.git] / doc / pic.ms
blob59dcc08cf8adb03b48bb24f1226badb5cc80fef0
1 .\" For tolerably obvious reason, this needs to be processed through PIC.
2 .\" It also needs to be processed through TBL and EQN.  Use "groff -p -e -t".
3 .\" There is no hope that this will ever look right under nroff.
4 .\"
5 .\" Comments beginning with %% are cut lines so portions of this
6  \" document can be automatically extracted.  %%TUTORIAL%% begins the
7 .\" tutorial part; %%REFERENCE%% the reference part.  %%POSTLUDE%% the
8  \" bibliography and end matter after the reference part.  
9 .\"
10 .\" This document was written for free use and redistribution by
11 .\" Eric S. Raymond <esr@thyrsus.com> in August 1995.
12 .\"
13 .\"     $Id: pic.ms,v 1.27 1997/07/03 17:35:39 esr Exp $        
14 .\"
15 .\" Set a proper TeX
16 .ie t .ds tx T\h'-.1667m'\v'.224m'E\v'-.224m'\h'-.125m'X
17 .el .ds tx TeX
18 .\"
19 .de CE\"        Centered caption for figure.  Assumes previous .KS
20 .ce 1
21 Figure \\n(H1-\\$1
22 .sp 1
23 .KE
25 .\" Definitions end here
26 .TL
27 Making Pictures With GNU PIC
28 .AU
29 Eric S. Raymond
30 .AI
31 <esr@snark.thyrsus.com>
32 .AB
33 The \fBpic\fP language is a \fBtroff\fP extension that makes it easy
34 to create and alter box-and-arrow diagrams of the kind frequently used
35 in technical papers and textbooks.  This paper is both an introduction
36 to and reference for \fIgpic\fP(1), the implementation distributed by
37 the Free Software Foundation for use with \fIgroff\fP(1).
38 .AE
39 .\"%%TUTORIAL%%
40 .NH 1
41 Introduction to PIC
42 .NH 2
43 Why PIC?
44 .PP
45 The \fBpic\fP language provides an easy way to write procedural
46 box-and-arrow diagrams to be included in \fBtroff\fP documents.  The
47 language is sufficiently flexible to be quite useful for state charts,
48 Petri-net diagrams, flow charts, simple circuit schematics, jumper
49 layouts, and other kinds of illustration involving repetitive uses of
50 simple geometric forms and splines.  Because these descriptions are
51 procedural and object-based, they are both compact and easy to modify.
52 .PP
53 The \fIgpic\fP(1) implementation of \fBpic\fP is distributed by the
54 Free Software Foundation for use with their \fIgroff\fP(1)
55 implementation of \fBtroff\fP.  Because both implementations are
56 widely available in source form for free, they are good bets for
57 writing very portable documentation.
58 .NH 2
59 PIC Versions
60 .PP
61 The original 1984 pre-\fIditroff\fP(1) version of \fBpic\fP is long
62 obsolete.  The rewritten 1991 version is still available as part of
63 the Documenter's Work Bench module of System V.
64 .PP
65 Where differences between Documenter's Work Bench (1991) \fBpic\fP and GNU
66 \fBpic\fP need to be described, original \fBpic\fP is referred to as
67 "DWB pic".  Details on the history of the program are given at the end
68 of this document.
69 .PP
70 In this document, the \fIgpic\fP(1) extensions will be marked as such.
71 .NH 1
72 Invoking PIC
73 .PP
74 Every \fBpic\fP description is a little program, which gets compiled
75 by \fIpic\fP(1) into \fIgtroff\fP(1) macros.  Programs that process or
76 display \fIgtroff\fP(1) output need not know or care that parts of the
77 image began life as \fBpic\fP descriptions.
78 .PP
79 The \fIpic\fP(1) program tries to translate anything between \fB.PS\fP
80 and \fB.PE\fP markers, and passes through everything else.  The normal
81 definitions of \fB.PS\fP and \fB.PE\fP in the \fIms\fP macro package
82 and elsewhere have also the side-effect of centering the \fBpic\fP output
83 on the page.
84 .PP
85 Other details of the \fI[gt]roff\fP(1) interface
86 .NH 2
87 PIC Error Messages
88 .PP
89 If you make a \fBpic\fP syntax error, \fIgpic\fP(1) will issue an
90 error message in the standard \fIgcc\fP(1)-like syntax.  A typical
91 error message looks like this,
92 .KS
93 .DS
94 .CW
95 pic:pic.ms:<nnn>: parse error before `<token>'
96 pic:pic.ms:<nnn>: giving up on this picture
97 .DE
99 .KE
101 where <nnn> is a line number, and <token> is a token near (usually
102 just after) the error location.
103 .NH 1
104 Basic PIC Concepts
106 Pictures are described procedurally, as collections of objects
107 connected by motions.  Normally, \fBpic\fP tries to string together
108 objects left-to-right in the sequence they are described, joining them
109 at visually natural points.  Here is an example illustrating the
110 flow of data in \fBpic\fP processing:
113 ellipse "document";
114 arrow;
115 box "\fIgpic\fP(1)"
116 arrow;
117 box width 1.2 "\fIgtbl\fP(1) or \fIgeqn\fP(1)" "(optional)" dashed;
118 arrow;
119 box "\fIgtroff\fP(1)";
120 arrow;
121 ellipse "PostScript"
123 .CE "1: Flow of \fBpic\fP data"
125 This was produced from the following \fBpic\fP program:
129 \&.PS
130 \&ellipse "document";
131 \&arrow;
132 \&box "\\fIpic\\fP(1)"
133 \&arrow;
134 \&box width 1.2 "\\fIgtbl\\fP(1) or \\fIgeqn\\fP(1)" "(optional)" dashed;
135 \&arrow;
136 \&box "\\fIgtroff\\fP(1)";
137 \&arrow;
138 \&ellipse "PostScript"
139 \&.PE
144 This little program illustrates several \fBpic\fP basics.  Firstly, we
145 see how to invoke three object types; ellipses, arrows, and boxes.  We
146 see how to declare text lines to go within an object (and that text
147 can have font changes in it).  We see how to change the line style of
148 an object from solid to dashed.  And we see that a box can be made
149 wider than its default size to accommodate more text (we'll discuss
150 this facility in detail in the next section).
152 We also get to see \fBpic\fP's simple syntax.  Statements are ended by
153 newlines or semicolons.  String quotes are required around all text
154 arguments, whether or not they contain spaces.  In general, the order
155 of command arguments and modifiers like "width 1.2" or "dashed" doesn't
156 matter, except that the order of text arguments is significant.
158 Here are all but one of the basic \fBpic\fP objects at their default sizes:
161 box "box";
162 move;
163 line "line" "";
164 move;
165 arrow "arrow" "";
166 move;
167 circle "circle";
168 move;
169 ellipse "ellipse";
170 move;
171 arc; down; move; "arc"
173 .CE "2: Basic \fBpic\fP objects"
175 The missing simple object type is a \fIspline\fP.  There is also a way
176 to collect objects into \fIblock composites\fP which allows you to
177 treat the whole group as a single object (resembling a box) for many
178 purposes.  We'll describe both of these later on.
180 The box, ellipse, circle, and block composite objects are \fIclosed\fR;
181 lines, arrows, arcs and splines are \fIopen\fP.  This distinction
182 will often be important in explaining command modifiers.
184 Figure \n[H1]-2 was produced by the following \fBpic\fP program,
185 which introduces some more basic concepts:
189 \&.PS
190 \&box "box";
191 \&move;
192 \&line "line" "";
193 \&move;
194 \&arrow "arrow" "";
195 \&move;
196 \&circle "circle";
197 \&move;
198 \&ellipse "ellipse";
199 \&move;
200 \&arc; down; move; "arc"
201 \&.PE
203 .ft R
206 The first thing to notice is the \fImove\fP command, which moves a
207 default distance (1/2 inch) in the current movement direction.
209 Secondly, see how we can also decorate lines and arrows with text.
210 The line and arrow commands each take two arguments here, specifying
211 text to go above and below the object.  If you wonder why one argument
212 would not do, contemplate the output of \fBarrow "ow!"\fP:
215 arrow "ow!"
217 .CE "3: Text centered on an arrow"
219 When a command takes one text string, \fBpic\fP tries to place it at
220 the object's geometric center.  As you add more strings, \fBpic\fP
221 treats them as a vertical block to be centered.  The program
225 line "1";
226 line "1" "2";
227 line "1" "2" "3";
228 line "1" "2" "3" "4";
229 line "1" "2" "3" "4" "5";
231 .ft R
234 for example, gives you this:
236 .sp 2
238 line "1";
239 line "1" "2";
240 line "1" "2" "3";
241 line "1" "2" "3" "4";
242 line "1" "2" "3" "4" "5";
244 .sp 2
245 .CE "4: Effects of multiple text arguments"
247 The last line of Figure 3.2's program, `\fBarc; down; move;
248 "arc"\fP', describing the captioned arc, introduces several new ideas.
249 Firstly, we see how to change the direction in which objects are
250 joined.  Had we written \fBarc; move; "arc"\fP,
251 omitting \fBdown\fP the caption would have been joined to the top
252 of the arc, like this:
255 arc; move; "arc";
257 .CE "5: Result of \fBarc; move; \"arc\"\fP"
259 This is because drawing an arc changes the default direction to the
260 one its exit end points at.  To reinforce this point, consider:
263 arc cw; move; "arc";
265 .CE "6: Result of \fBarc cw; move; \"arc\"\fP"
267 All we've done differently here is specify "cw" for a clockwise arc.
268 Observe how it changes the default direction to down, rather than up.
270 Another good way to see this via with the following program:
274 line; arc; arc cw; line
276 .ft R
279 which yields:
282 line; arc; arc cw; line;
284 .CE "7: Result of \fBline; arc; arc cw; line\fP"
286 Notice that we did not have to specify "up" for the second arc to be
287 joined to the end of the first.
289 Finally, observe that a string, alone, is treated as text to be
290 surrounded by an invisible box of a size either specified by width
291 and height attributes or by the defaults \fBtextwid\fR and
292 \fBtextht\fR.  Both are initially zero (because we don't know the
293 default font size).
294 .NH 1 
295 Sizes and Spacing
297 Sizes are specified in inches.  If you don't like inches, it's
298 possible to set a global style variable \fBscale\fP that changes the
299 unit.  Setting \fBscale = 2.54\fP will effectively change the internal
300 unit to centimeters (all other size variable valuess will be scaled
301 correspondingly).
302 .NH 2 Default Sizes of Objects
304 Here are the default sizes for \fBpic\fP objects:
308 tab(@), linesize(2);
309 lb | lb
310 l | l.
311 .sp 2p
312 Object@Default Size
313 .sp 2p
315 .sp 2p
316 box@0.75" wide by 0.5" high
317 circle@0.5" diameter
318 ellipse@0.75" wide by 0.5" high
319 arc@0.5" radius
320 line@0.5" long
321 arrow@0.5" long
322 .sp 5p
328 The simplest way to think about these defaults is that they make the
329 other basic objects fit snugly into a default-sized box.
330 .NH 2
331 Objects Do Not Stretch!
333 Text is rendered in the current font with normal troff line spacing.
334 Boxes, circles, and ellipses do \fInot\fP automatically resize to fit
335 enclosed text.  Thus, if you say \fBbox "text far too long"\fP
336 you'll get this:
339 box "this text is far too long for a default box"
341 .CE "1: Boxes do not automatically resize"
343 which is probably not the effect you want.
344 .NH 2
345 Resizing Boxes
347 To change the box size, you can specify a box width with the "width"
348 modifier:
351 box width 3 "this text is far too long for a default box"
353 .CE "2: Result of \fBbox width 3 \"text far too long\"\fP"
355 This modifier takes a dimension in inches.  There is also a "height"
356 modifier that will change a box's height.  The \fBwidth\fP keyword may
357 be abbreviated to \fBewid\fP; the \fBheight\fP keyword to
358 \fBheight\fP.
359 .NH 2
360 Resizing Other Object Types
362 To change the size of a circle, give it a \fBrad\fP or \fBdiam\fP
363 modifier; this changes the radius or diameter of the circle, according
364 to the numeric argument that follows.
367 {circle rad 0.1; move down 0.2 from last circle .s; "0.1"};
368 move; circle rad 0.2 "0.2"; move; circle rad 0.3 "0.3";
370 .CE "3: Circles with increasing radii"
372 The \fBmove\fP command can also take a dimension, which just tells
373 it how many inches to move in the current direction.
375 Ellipses are sized to fit in the rectangular box defined by their
376 axes, and can be resized with \fBwidth\fP and \fBheight\fP like boxes.
378 You can also change the radius of curvature of an arc with \fBrad\fP
379 (which specifies the radius of the circle of which the arc is a segnmment).
380 Larger values yield flatter arcs.
383 {arc rad 0.1; move down 0.3 from last arc .center; "0.1"};
384 move;
385 {arc rad 0.2; move down 0.4 from last arc .center; "0.2"};
386 move;
387 {arc rad 0.3; move down 0.5 from last arc .center; "0.3"};
389 .CE "4: \fBarc rad\fP with increasing radii"
391 Observe that because an arc is defined as a quarter circle, increasing
392 the radius also increases the size of the arc's bounding box.
393 .NH 2
394 The `same' Keyword
396 In place of a dimension specification, you can use the keyword
397 \fBsame\fR.  This gives the object the same size as the previous one
398 of its type.  As an example, the program
402 \&.PS
403 \&box; box wid 1 ht 1; box same; box
404 \&.PE
409 gives you
412 box; box wid 1 ht 1; box same; box
414 .CE "5: The \fBsame\fP keyword"
416 .NH 1
417 Generalized Lines and Splines
418 .NH 2
419 Diagonal Lines
421 It is possible to specify diagonal lines or arrows by adding multiple \fBup\fP,
422 \fBdown\fP, \fBleft\fP, and \fBright\fP modifiers to the line object.
423 Any of these can have a multiplier.  To understand the effects, think
424 of the drawing area as being gridded with standard-sized boxes.
427 # Draw a demonstration up left arrow with grid box overlay
428 define gridarrow
430         [
431                 {arrow up left $1;}
432                 box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
433                 for i = 2 to ($1 / 0.5) do
434                 {
435                         box wid 0.5 ht 0.5 dotted with .sw at last box .se;
436                 }
437                 move down from last arrow .center;
438                 [
439                         if ( $1 == boxht ) then { "\fBline up left\fP" } else { sprintf("\fBarrow up left %g\fP", $1) };
440                 ]
441         ] 
442         move right from last [] .e;
444 gridarrow(0.5);
445 gridarrow(1);
446 gridarrow(1.5);
447 gridarrow(2);
448 undef gridarrow
450 .CE "1: Diagonal arrows (dotted boxes show the implied 0.5-inch grid)"
451 .NH 2
452 Multi-Segment Line Objects
454 A "line" or "arrow" object may actually be a path consisting of any number of
455 segments of varying lengths and directions.  To describe a path,
456 connect several line or arrow commands with the keyword \fBthen\fP.
459 define zigzag { $1 right 1 then down .5 left 1 then right 1 }
460 zigzag(line); 
462 .CE "2: \fBline right 1 then down .5 left 1 then right 1\fP"
463 .NH 2
464 Spline Objects
466 If you start a path with the \fBspline\fP keyword, the path vertices
467 are treated as control points for a spline curve fit.
470 [zigzag(spline);]
471 move down 0.2 from last [] .s;
472 "The spline curve..."
473 move right from last [] .e;
475         zigzag(line dashed);
476         spline from start of last line right 1 then down .5 left 1 then right 1;
477         "1" at last spline .start + (-0.1, 0);
478         "2" at last spline .start + (1.1, 0);
479         "3" at last spline .end + (-1.1, 0);
480         "4" at last spline .end + (0.1, 0);
482 move down 0.2 from last [] .s;
483 "...with tangents displayed"
484 undef zigzag;
486 .CE "3: \fBspline right 1 then down .5 left 1 then right 1\fP"
488 You can describe many natural-looking but irregular curves this
489 way. For example: 
492 [spline right then up then left then down ->;]
493 move down 0.2 from last [] .s;
494 ["\fBspline right then up then left then down ->;\fP"]
495 move right 3 from last [] .se;
496 "\fBspline left then up right then down right ->;\fP"
497 move up 0.2;
498 [spline left then up right then down right ->;]
500 .CE "4: Two more spline examples"
502 Note the arrow decorations.  Arrowheads can be applied naturally to
503 any path-based object, line or spline.  We'll see how in the next
504 section.
505 .NH 1
506 Decorating Objects.
507 .NH 2
508 Dashed Objects
510 We've already seen that the modifier \fBdashed\fP can change the line
511 style of an object from solid to dashed.  GNU \fBgpic\fP permits you to
512 dot or dash ellipses, circles, and arcs (and splines in \(*tx mode
513 only); some versions of DWB may only permit dashing of lines and
514 boxes.  It's possible to change the dash interval by specifying a
515 number after the modifier.
519 box dashed "default";
520 move;
521 box dashed 0.05 "0.05";
522 move;
523 box dashed 0.1 "0.1";
524 move;
525 box dashed 0.15 "0.15";
526 move;
527 box dashed 0.2 "0.2";
529 .CE "1: Dashed objects"
530 .NH 2
531 Dotted Objects
533 Another available qualifier is \fBdotted\fP.  GNU \fBgpic\fP permits
534 you to dot or dash ellipses, circles, and arcs (and splines in \(*tx
535 mode only); some versions of DWB may only permit dashing of lines and
536 boxes.  It too can be suffixed with a number to specify the interval
537 between dots:
540 box dotted "default";
541 move;
542 box dotted 0.05 "0.05";
543 move;
544 box dotted 0.1 "0.1";
545 move;
546 box dotted 0.15 "0.15";
547 move;
548 box dotted 0.2 "0.2";
550 .CE "2: Dotted objects"
551 .NH 2
552 Rounding Box Corners
554 It is also possible, in GNU \fBgpic\fP only, to modify a box so it has
555 rounded corners
558 box rad 0.05 "rad 0.05";
559 move;
560 box rad 0.1 "rad 0.1";
561 move;
562 box rad 0.15 "rad=0.15"; 
563 move;
564 box rad 0.2 "rad=0.2"; 
565 move;
566 box rad 0.25 "rad=0.25"; 
568 .CE "3: \fBbox rad\fP with increasing radius values;"
570 Radius values higher than half the minimum box dimension are silently
571 truncated to that value.
572 .NH 2
573 Arrowheads
575 Lines and arcs can be decorated as well.  Any line or arc (and any
576 spline as well) can be decorated with arrowheads by adding one or more
577 as modifiers:
580 line <- ->
582 .CE "4: Double-headed line made with  \fBline <- ->\fP"
584 In fact, the \fBarrow\fP command is just shorthand for \fBline ->\fP.  And
585 there is a double-head modifier <->, so the figure above could have been made
586 with \fCWline <->\fP.
588 Arrowheads have a \fBwidth\fP attribute, the distance across the rear;
589 and a \fBheight\fP attribute, the length of the arrowhead along the shaft.
591 Arrowhead style is controlled by the style variable \fBarrowhead\fP.
592 The DWB and GNU versions interpret it differently.  DWB defaults to
593 open arrowheads and an \fBarrowhead\fP value of 2; the Kernighan
594 paper says a value of 7 will make solid arrowheads.  GNU \fBgpic\fP
595 defaults to solid arrowheads and an \fBarrowhead\fP value of 1; a
596 value of 0 will produce open arrowheads.
597 .NH 2
598 Line Thickness
600 It's also possible to change the line thickness of an object (this is
601 a GNU extension, DWB \fBpic\fP doesn't support it.).  
602 The default thickness of the lines used to draw objects is controlled by the
603 .B linethick
604 variable.
605 This gives the thickness of lines in points.
606 A negative value means use the default thickness:
607 in \(*tx output mode, this means use a thickness of 8 milliinches;
608 in \(*tx output mode with the
609 .B -c
610 option, this means use the line thickness specified by
611 .B .ps
612 lines; in troff output mode, this means use a thickness proportional
613 to the pointsize.  A zero value means draw the thinnest possible line
614 supported by the output device.  Initially it has a value of -1.
615 There is also a \fBthickness\fP attribute (which can be abbreviated to
616 \fBthick\fP).  For example, \fBcircle thickness 1.5\fP would draw a
617 circle using a line with a thickness of 1.5 points.  The thickness of
618 lines is not affected by the value of the
619 .B scale
620 variable, nor by any width or height given in the
621 .B .PS
622 line.
623 .NH 2
624 Invisible Objects
626 The modifier \fBinvis\fP makes an object entirely invisible.  This
627 used to be useful for positioning text in an invisible object that is
628 properly joined to neighboring ones.  Newer DWB versions and GNU
629 \fBpic\fP treat standalone text in exactly this way.
630 .NH 2
631 Filled Objects
633 It is possible to fill boxes, circles, and ellipses.  The
634 modifier \fBfill[ed]\fP accomplishes this.  You can suffix it with a fill
635 value; the default is given by the stule variable \fBfillval\fP.
637 DWB \fBpic\fP and \fBgpic\fP have opposite conventions for fill values
638 and different defaults.  DWB \fBfillval\fP defaults to 0.3 and smaller
639 values are darker; GNU \fBfillval\fP uses 0 for white and 1 for black.
642 circle fill; move; circle fill 0.4; move; circle fill 0.2;  
644 .CE "5: \fBcircle fill; move; circle fill 0.4; move; circle fill 0.9;\fB"
646 GNU \fBgpic\fP makes some additional guarantees.  A fill value greater
647 than 1 can also be used: this means fill with the shade of gray that
648 is currently being used for text and lines.  Normally this will be
649 black, but output devices may provide a mechanism for changing this.
650 The invisible attribute does not affect the filling of objects.  Any
651 text associated with a filled object will be added after the object
652 has been filled, so that the text will not be obscured by the filling.
654 The closed-object modifier \fBsolid\fR is equivalent to \fBfill\fR 
655 with the darkest fill value (DWB \fBpic\fR had this capability but
656 mentioned it only in a reference opinion).  
657 .NH 1
658 More About Text Placement
660 By default, text is centered at the geometric center of the object it is
661 associated with.  The modifier \fBljust\fR causes the left end to be
662 at the specified point (which means that the text lies to the right of
663 the specified place!), The modifier \fBrjust\fP puts the right end at
664 the place.  The modifiers \fBabove\fP and \fBbelow\fP center the text
665 one half line space in the given direction.
667 Text attributes can be combined:
670 [line up "ljust text" ljust;] 
671 move 1.5;
672 [line up "rjust text" rjust;] 
673 move;
674 [arrow 1 "ljust above" ljust above;]
675 move;
676 [arrow 1 "rjust below" rjust below;]
678 .CE "1: Text attributes"
680 What actually happens is that n text strings are centered in a box
681 that is \fBtextwid\fP wide by \fBtextht\fP high.  Both these variables
682 are initially zero (that is \fBpic\fR's way of not making assumptions
683 about \fI[tg]roff\fP(1)'s default point size).
685 In GNU \fBgpic\fR, objects can have an
686 .B aligned
687 attribute.
688 This will only work when the postprocessor is
689 \fBgrops\fP.
690 Any text associated with an object having the
691 .B aligned
692 attribute will be rotated about the center of the object
693 so that it is aligned in the direction from the start point
694 to the end point of the object.
695 Note that this attribute will have no effect for objects whose start and
696 end points are coincident.
697 .NH 1
698 More About Direction Changes
700 We've already seen how to change the direction in which objects are
701 composed from rightwards to downwards.  Here are some more
702 illustrative examples:
705 down;
707         "\fBright; box; arrow; circle; arrow; ellipse\fP";
708         move 0.2;
709         [right; box; arrow; circle; arrow; ellipse;]
711 move down 0.3 from last [] .s;
713         "\fBleft; box; arrow; circle; arrow; ellipse\fP"
714         move 0.2;
715         [left; box; arrow; circle; arrow; ellipse;]
717 # move down 0.3 from last [] .sw;
718 # To re-join this illustrations, delete everything from here down to
719 # the next #-comment, and uncomment the move line above
721 .CE "1: Effects of different motion directions (right and left)"
724 # To re-join this illustrations, delete everything down to here, then
725 # comment out the next `down' line.
726 # Don't forget to re-number the figures following!
727 down;
729         "\fBdown; box; arrow; circle; arrow; ellipse;\fP"
730         move 0.2;
731         box; arrow; circle; arrow; ellipse;
733 move right 2 from last [] .e;
735         up; box; arrow; circle; arrow; ellipse;
736         move 0.2;
737         "\fBup; box; arrow; circle; arrow; ellipse;\fP"
740 .CE "2: Effects of different motion directions (up and down)"
742 Something that may appear surprising happens if you change directions 
743 in the obvious way:
746 box; arrow; circle; down; arrow; ellipse
748 .CE "3: \fBbox; arrow; circle; down; arrow; ellipse\fP
750 You might have expected that program to yield this:
753 box; arrow; circle; move to last circle .s; down; arrow; ellipse
755 .CE "4: More intuitive?
757 But, in fact, to get Figure \*[SN]3 you have to do this:
761 \&.PS
762 box;
763 arrow;
764 circle;
765 move to last circle .s;
766 down;
767 arrow;
768 ellipse
769 \&.PE
773 Why is this?  Because the exit point for the current direction is
774 already set when you draw the object.  The second arrow in Figure
775 \*[SN]2 dropped downwards from the circle's attachment point for an
776 object to be joined to the right.
778 The meaning of the command \fBmove to last circle .s\fP should be obvious.
779 In order to see how it generalizes, we'll need to go into detail on two
780 important topics; locations and object names.
781 .NH 1
782 Naming Objects
784 The most natural way to name locations in \fBpic\fP is relative to
785 objects.  In order to do this, you have to be able you have to be able
786 to name objects.  The \fBpic\fP language has rich facilities for this
787 that try to emulate the syntax of English.
788 .NH 2
789 Naming Objects By Order Of Drawing
791 The simplest (and generally the most useful) way to name an object is
792 with a \fBlast\fP clause.  It needs to be followed by an object type
793 name; \fBbox\fP, \fBcircle\fP, \fBellipse\fP, \fBline\fP, \fBarrow\fP,
794 \fBspline\fP or \fB[]\fP (the last type refers to a \fIcomposite
795 object\fP which we'll discuss later).  So, for example, the \fBlast
796 circle\fP clause in the program attached to Figure \*[SN]3 refers to the
797 last circle drawn.
799 More generally, objects of a given type are implicitly numbered
800 (starting from 1).  You can refer to (say) the third ellipse in the
801 current picture with \fB3rd ellipse\fP, or to the first box as \fB1st
802 box\fP, or to the fifth line as \fB5th line\fP.
804 Objects are also numbered backwards by type from the last one of 
805 You can say \fB2nd last box\fP to get the second-to-last box, or
806 \fB3rd last ellipse\fP to get the third-to-last box.
808 In places where \fIn\fBth\fP is allowed, \fB`\fIexpr\fB'th\fP is also allowed.
809 Note that
810 .B 'th
811 is a single token: no space is allowed between the
812 .B '
813 and the \fBth\fP.
814 For example,
819 for i = 1 to 4 do {
820    line from `i'th box.nw to `i+1'th box.se
825 .NH 2
826 Naming Objects With Labels
828 You can also specify an object by referring to a label.  A label is a
829 word (which must begin with a capital letter) followed by a colon;
830 you declare it by placing it immediately before the object drawing command.
831 For example, the program
835 \&.PS
836 A: box "first" "object"
837 move;
838 B: ellipse "second" "object"
839 move;
840 arrow left at A;
841 \&.PE
846 declares labels \fBA\fP and \fBB\fP for its first and second objects.
847 Here's what that looks like:
850 A: box "first" "object"
851 move;
852 B: ellipse "second" "object"
853 move;
854 arrow left at A .l;
856 .CE "1: Example of label use"
857 The \fBat\fP statement in the fourth line uses the label \fBA\fP (the
858 behavior of \fBat\fP will be explained in the next section).  We'll
859 see later on that labels are most useful for referring to block composite
860 objects.  
862 Labels are not constants but variables (you can view colon as a sort
863 of assignment).  You can say something like \fBA: A + (1,0);\fP
864 and the effect will be to reassign the label \fBA\fR to designate a
865 position one inch to the right of its old value. 
866 .NH 1
867 Describing locations
869 The location of points can be described in many different ways.  All these
870 forms are interchangeable as for as the \fBpic\fP language syntax is
871 concerned; where you can use one, any of the others that would make 
872 semantic sense are allowed.
874 The special label \fBHere\fR always refers to the current position.
875 .NH 2
876 Absolute Coordinates
878 The simplest is absolute coordinates in inches; \fBpic\fP uses a
879 Cartesian system with (0, 0) at the lower left corner of the virtual
880 drawing surface for each picture (that is, X increases to the right
881 and Y increases upwards).  An absolute location may always be written in the
882 conventional form as two comma-separated numbers surrounded by
883 parentheses (and this is recommended for clarity).  In contexts where
884 it creates no ambiguity, the pair of X and Y coordinates suffices
885 without punctuation.
887 It is a good idea to avoid absolute coordinates, however.  They tend
888 to make picture descriptions difficult to understand and modify.
889 Instead, there are quite a number of ways to specify locations
890 relative to \fBpic\fP objects and previous locations.
892 .NH 2
893 Locations Relative to Objects
895 The symbol \fBhere\fP always refers to the position of the last object
896 drawn or the destination of the last \fBmove\fP.
898 Alone and unqualified, a \fBlast circle\fP or any other way of
899 specifying a closed-object or arc location refers as a position to the
900 geometric center of the object.  Unqualified, the name of a line or
901 spline object refers to the position of the object start.
903 Also, \fBpic\fP objects have quite a few named locations
904 associated with them.  One of these is the object center, which can be
905 indicated (redundantly) with the suffix \fB.center\fP (or just \fB.c\fP).
906 Thus, \fBlast circle \&.center\fP is equivalent to \fBlast
907 circle\fP.
908 .NH 3
909 Locations Relative to Closed Objects
911 Every closed object (box, circle, ellipse, or block composite) also
912 has eight compass points associated with it;
915 define dot {circle fill rad 0.02 at $1}
917 define compass { [
918         ME: $1;
919         dot(ME.c);  "\fB .c\fP"  at ME .c ljust;
920         dot(ME.n);  "\fB.n\fP"   at ME .n above
921         dot(ME.ne); "\fB .ne\fP" at ME .ne above
922         dot(ME.e);  "\fB .e\fP"  at ME .e ljust
923         dot(ME.se); "\fB .se\fP" at ME .se below
924         dot(ME.s);  "\fB.s\fP"   at ME .s below
925         dot(ME.sw); "\fB.sw \fP" at ME .sw below
926         dot(ME.w);  "\fB.w \fP"  at ME .w rjust
927         dot(ME.nw); "\fB.nw \fP" at ME .nw above
928 ] }
929 compass(box wid 1.5 ht 1);
930 move right from last [] .e;
931 compass(circle diam 1);
932 move right from last [] .e;
933 compass(ellipse wid 1.5 ht 1);
935 .CE "1: Compass points"
937 these are the locations where eight compass rays from the geometric center
938 would intersect the figure.  So when we say \fBlast circle .s\fP we are
939 referring to the south compass point of the last circle drawn.  The
940 explanation of Figure 7.3's program is now complete.
942 (In case you dislike compass points, the names \fB.top\fP,
943 \&\fB.bottom\fP, \fB.left\fP and \fB.right\fP are synonyms for \fB.n\fP,
944 \&\fB.s\fP, \fB.e\fP, and \fB.w\fP respectively; they can even be
945 abbreviated to \fB.t\fP, \fB.b\fP, \fB.l\fP and \fB.r\fP).
947 The names \fBcenter\fP, \fBtop\fP, \fBbottom\fP, \fBleft\fP and
948 \fBright\fP can also be used (without the leading dot) in a prefix
949 form marked by \fBof\fP; thus, \fPcenter of last circle\fB and
950 \fBtop of 2nd last ellipse\fP are both valid object references.
952 Arc objects also have compass point; they are the compass points of
953 the implied circle.
954 .NH 3
955 Locations Relative to Open Objects
957 Every open object (line, arrow, arc, or spline) has three named
958 points; \fB.start\fP, \fB.center\fP, and \fB.end\fP.  They can
959 also be used without leading dots in the \fBof\fP prefix form.
960 The center of an arc is the center of its circle, but the center of
961 a line, path, or spline is halfway between its endpoints.
964 define critical {
965         [ ME: $1;
966                 dot(ME.c);     ".center" rjust at ME.center + (-0.1, 0.1)
967                 dot(ME.start); ".start"  rjust at ME.start  + (-0.1, 0.1)
968                 dot(ME.end);   ".end"    rjust at ME.end    + (-0.1, 0.1)
969         ]
970         move down 0.2 from last [] .s;
972 critical(line up right 1);
973 move right from last [] .e;
974 critical(arc rad 0.5 cw);
975 move right from last [] .e;
976 critical(line right 1 then down .5 left 1 then right 1);
977 move right from last [] .e;
978 critical(spline right 1 then up right then left then left 1);
980 .CE "2: Special points on open objects"
982 .NH 2
983 Ways of Composing Positions
985 Once you have two positions to work with, there are several ways to
986 combine them to specify new positions.
987 .NH 3
988 Vector Sums and Displacements
990 Any two positions may be added or subtracted to yield a new position.
991 The result is the conventional vector sum or difference of coordinates.
992 For example, \fBlast box .ne + (0.1, 0)\fP is a valid position.  This 
993 example illustrates a common use, to define a position slightly offset
994 from a named one (say, for captioning purposes).
995 .NH 3
996 Interpolation Between Positions
998 A position may be interpolated between any two positions.  The syntax
999 is `\fIfraction\fP \fBof the way between\fP \fIposition1\fP \fBand\fP
1000 \fIposition2\fP.'  For example, you can say \fB1/3 of the way between
1001 here and last ellipse .ne\fP.  The fraction may be in
1002 numerator/denominator form or may be an ordinary number (values are
1003 \&\fInot\fP restricted to [0,1]).  As an alternative to this verbose
1004 syntax, you can say `\fIfraction\fP  \fB<\fP\fIposition1\fP \fB,\fP
1005 \fIposition2\fP\fB>\fP.'; thus, the example could also be written
1006 \fB1/3\fP <here, last ellipse>\fP.
1009 arrow up right;
1010 P: 1/3 of the way between last arrow .start and last arrow .end;
1011 dot(P); move right 0.1; "P";
1013 .CE "3: \fBP: 1/3 of the way between last arrow .start and last arrow .end\fP"
1015 This facility can be used, for example, to double connections.
1018 A: box "yin"; move; B: box "yang";
1019 arrow right at 1/4 <A.e,A.ne>;
1020 arrow left  at 1/4 <B.w,B.sw>;
1022 .CE "4: Doubled arrows"
1024 You can get Figure \n[H1]-4 from the following program:
1028 \&.PS
1029 A: box "yin"; move;
1030 B: box "yang";
1031 arrow right at 1/4 <A.e,A.ne>;
1032 arrow left  at 1/4 <B.w,B.sw>;
1033 \&.PE
1038 Note the use of the short form for interpolating points.
1039 .NH 3
1040 Projections of Points
1042 Given two positions \fIp\fP and \fIq\fP, the position
1043 \fB(\fP\fIp\fP\fB,\fP \fIq\fP\fB)\fP has the X coordinate of \fIp\fP
1044 and the Y coordinate of \fIq\fP.  This can be helpful in placing an
1045 object at one of the corners of the virtual box defined by two other
1046 objects.
1049 box invis wid 2 height 1;
1050 dot(last box .ne); "\fB(B,A)\fB is here" ljust at last circle + (0.1, 0.1);
1051 dot(last box .se); "B" ljust at last circle + (0.1, -0.1)
1052 dot(last box .sw); "\fB(A,B)\fB is here" rjust at last circle + (-0.1, -0.1);
1053 dot(last box .nw); "A" ljust at last circle + (-0.1, 0.1)
1055 .CE "5: Using (\fIx\fP, \fIy\fP) composition"
1056 .NH 2
1057 Using Locations
1059 There are four ways to use locations; \fBat\fP, \fBfrom\fP, \fBto\fP,
1060 and \fBwith\fP.  All three are object modifiers; that is, you use them
1061 as suffixes to a drawing command.
1063 The \fBat\fP modifier says to draw a closed object or arc with its
1064 center at the following location, or to draw a line/spline/arrow
1065 starting at the following location.
1067 The \fBto\fP modifier can be used alone to specify a move destination.
1068 The \fBfrom\fP modifier can be used alone in the same way as \fBat\fP.
1070 The \fBfrom\fP and \fBto\fP modifiers can be used with a \fBline\fR or
1071 \fBarc\fR command to specify start and end points of the object.  In
1072 conjunction with named locations, this offers a very flexible
1073 mechanism for connecting objects.  For example, the following program
1077 \&.PS
1078 box "from"
1079 move 0.75;
1080 ellipse "to"
1081 arc cw from 1/3 of the way \\
1082     between last box .n and last box .ne to last ellipse .n;
1083 \&.PE
1088 yields:
1091 box "from"
1092 move 0.75;
1093 ellipse "to"
1094 arc cw from 1/3 of the way between last box .n and last box .ne to last ellipse .n;
1096 .CE "6: A tricky connection specified with English-like syntax"
1098 The \fBwith\fP modifier allows you to identify a named attachment
1099 point of an object with another point.  This is very useful for connecting
1100 objects in a natural way.  For an example, consider these two programs:
1104         box wid 0.5 ht 0.5; box wid 0.75 ht 0.75;
1105         move down from last box .s 0.1;
1106         "\fBbox wid 0.5 ht 0.5; box wid 0.75 ht 0.75\fP"
1108 move from last [].e 2
1110         box wid 0.5 ht 0.5; box wid 0.75 ht 0.75 with .sw at last box .se;
1111         move down from last box .s 0.1;
1112         "\fBbox wid 0.5 ht 0.5; box wid 0.75 ht 0.75 with .sw at last box .se;\fP"
1115 .CE "7: Using the \fBwith\fP modifier for attachments"
1116 .NH 2
1117 The chop modifier
1119 When drawing lines between circles that don't intersect them at a
1120 compass point, it is useful to be able to shorten a line by the radius 
1121 of the circle at either or both ends.  Consider the following program:
1124 \&.PS
1125 circle "x"
1126 circle "y" at 1st circle - (0.4, 0.6) 
1127 circle "z" at 1st circle + (0.4, -0.6) 
1128 arrow from 1st circle to 2nd circle chop 
1129 arrow from 2nd circle to 3rd circle chop 
1130 arrow from 3rd circle to 1st circle chop 
1131 \&.PE
1135 It yields the following:
1138 circle "x"
1139 circle "y" at 1st circle - (0.4, 0.6) 
1140 circle "z" at 1st circle + (0.4, -0.6) 
1141 arrow from 1st circle to 2nd circle chop 
1142 arrow from 2nd circle to 3rd circle chop 
1143 arrow from 3rd circle to 1st circle chop 
1145 .CE "8: The \fBchop\fR modifier"
1147 Notice that the \fBchop\fR attribute moves arrowheads rather than
1148 stepping on them. By default, the \fBchop\fR modifier shortens both
1149 ends of the line by \fBcirclerad\fR.  By suffixing it with a number
1150 you can change the amount of chopping.
1152 If you say \fBline ... chop \fIr1\fP chop \fIr2\fP\fR with \fIr1\fP
1153 and \fIr2\fP both numbers, you can vary the amount of chopping at both
1154 ends.  You can use this in combination with trigonometric functions
1155 to write code that will deal with more complex intersections.
1156 .NH 1
1157 Object Groups
1159 There are two different ways to group objects in \fBpic\fP; \fIbrace
1160 grouping\fP and \fIblock composites\fP.
1161 .NH 2
1162 Brace Grouping
1164 The simpler method is simply to group a set of objects within curly
1165 bracket or brace characters.  On exit from this grouping, the current
1166 position and direction are restored to their value when the opening
1167 brace was encountered.
1168 .NH 2
1169 Block Composites
1171 A block composite object is created a series of commands enclosed by
1172 square brackets.  The composite can be treated for most purposes like
1173 a single closed object, with the size and shape of its bounding box.
1174 Here is an example.  The program fragment
1178 \&A: [
1179 \&    circle;
1180 \&    line up 1 at last circle .n;
1181 \&    line down 1 at last circle .s;
1182 \&    line right 1 at last circle .e;
1183 \&    line left 1 at last circle .w;
1184 \&    box dashed with .nw at last circle .se + (0.2, -0.2); 
1185 \&    Caption: center of last box;
1191 yields the block in figure \n[H1]-1, which we show both with and
1192 without its attachment points.  The block's location becomes the
1193 value of \fBA\fP.
1196 define junction {
1197         circle;
1198         line up 1 at last circle .n;
1199         line down 1 at last circle .s;
1200         line right 1 at last circle .e;
1201         line left 1 at last circle .w;
1202         box dashed with .nw at last circle .se + (0.2, -0.2); 
1203         Caption: center of last box;
1205 [junction();]
1206 move;
1207 compass([junction()]);
1209 .CE "1: A sample composite object"
1211 To refer to one of the composite's attachment points, you can say
1212 (for example) \fBA .s\fP.  For purposes of object naming, composites
1213 are a class.  You could write \fBlast [] .s\fP as an equivalent
1214 refrence, usable anywhere a location is needed.  This construction is
1215 very important for putting together large, multi-part diagrams.
1216 .PP 
1217 Blocks are also a variable-scoping mechanism, like a \fIgroff\fP(1)
1218 environment.  All variable assignments done inside a block are undone
1219 at the end of it.  To get at values within a block, write a name of
1220 the block followed by a dot, followed by the variable or label you
1221 want.  For example, we could refer the the center of the box in the
1222 above composite as \fBlast [] .Caption\fP or \fBA.Caption\fP.
1224 This kind of reference to a label can be used in any way any other
1225 location can be.  For example, if we added \fB"Hi!" at A.Caption\fP
1226 the result would look like this:
1229 A: [junction();]
1230 "Hi!" at A.Caption;
1232 .CE "2: Adding a caption using interior labeling"
1234 You can also use interior labels in either part of a \fBwith\fR
1235 modifier.  This means that the example composite could be placed
1236 relative to its caption box by a command containing \fBwith A.Caption
1237 at\fP.
1239 Blocks may be nested.  This means you can use block attachment points
1240 to build up complex diagrams hierarchically, from the inside out.
1241 Note that \fBlast\fP and the other sequential naming mechanisms
1242 don't look inside blocks, so if you have a program that looks 
1243 like 
1247 \&.PS
1248 P: [box "foo"; ellipse "bar"];
1249 Q: [
1250         [box "baz"; ellipse "quxx"]
1251         "random text";
1252    ]
1253 arrow from 2nd last [];
1254 \&.PE
1259 the arrow in the last line will be attached to object \fBP\fP, not 
1260 object \fBQ\fP.
1262 In DWB \fBpic\fP, only references one level deep into enclosed blocks
1263 were permitted. GNU \fBgpic\fP removes this restriction.
1265 The combination of block variable scoping, assignability of labels and
1266 the macro facility that we'll describe later on can be used to
1267 simulate functions with local variables (just wrap the macro body in
1268 block braces).
1269 .NH 1
1270 Style Variables
1272 There are a number of global style variables in \fBpic\fR that can be used to
1273 change its overall behavior.  We've mentioned several of them in
1274 previous sections.  They're all described here.  For each variable,
1275 the default is given.
1276 .TS H
1277 tab(@), linesize(2);
1278 lb | lb | lb
1279 l | n | l.
1280 .sp 2p
1281 Style Variable@Default@What It Does
1282 .sp 2p
1284 .sp 2p
1286 boxht@0.5@Default height of a box
1287 boxwid@0.75@Default height of a box
1288 lineht@0.5@Default length of vertical line
1289 linewid@0.75@Default length of horizontal line
1290 arcrad @0.25@Default radius of an arc
1291 circlerad@0.25@Default radius of a circle
1292 ellipseht@0.5@Default height of an ellipse
1293 ellipsewid@0.75@Default width of an ellipse
1294 moveht@0.5@Default length of vertical move
1295 movewid@0.75@Default length of horizontal move
1296 textht@0@Default height of box enclosing a text object
1297 textwid@0@Default width of box enclosing a text object
1298 arrowht@0.1@Length of arrowhead along shaft
1299 arrowwid@0.05@Width of rear of arrowhead
1300 arrowhead@1@Enable/disable arrowhead filling
1301 dashwid@0.05@Interval for dashed lines
1302 maxpswid@11@Maximum width of picture
1303 maxpsht@8.5@Maximum height of picture
1304 scale@1@Unit scale factor
1305 fillval@0.5@Default fill value
1306 .sp 5p
1309 Any of these variables can be set with a simple assignment statement.
1310 For example:
1313 [boxht=1; boxwid=0.3; movewid=0.2; box; move; box; move; box; move; box;]
1315 .CE "1: \fBboxht=1; boxwid=0.3; movewid=0.2; box; move; box; move; box; move; box;\fP"
1317 In GNU \fBpic\fR, setting the \fBscale\fR variable re-scales all
1318 size-related state variables so that their values remain equivalent in
1319 the new units.
1321 The command \fBreset\fP resets all style variables to their defaults.
1322 You can give it a comma-separated list of variable names as arguments,
1323 in which case it resets only those.
1325 State variables retain their values across pictures until reset.
1326 .NH 1
1327 Expressions, Variables, and Assignment
1329 A number is a valid expression, of course (all numbers are stored
1330 internally as floating-point).  Decimal-point notation is acceptable;
1331 in GNU \fBgpic\fR, scientific notation in C's `e' format (like
1332 \f(CW5e-2\fP) is accepted.
1334 Anywhere a number is expected, the language will also accept a
1335 variable.  Variables may be the built-in style variable described in
1336 the last section, or new variables created by assignment.
1338 DWB \fBpic\fP supports only the ordinary assignment via =, defines the
1339 variable in the current block if it is not already defined there, and
1340 then changes the value in the current block.
1341 GNU \fBgpic\fP supports an alternate form of assignment using :=. The
1342 .I variable
1343 (right side) must already be defined,
1344 and the value of
1345 .I variable
1346 will be changed only in the innermost block in which it is defined.
1348 You can use the height, width, radius, and x and y coordinates of any
1349 object or corner in expressions If \fBA\fP is an object label or name,
1350 all the following are valid:
1354 A.x                  # x coordinate of the center of A
1355 A.ne.y               # y coordinate of the northeast corner of A
1356 A.wid                # the width of A
1357 A.ht                 # and its height
1358 2nd last circle.rad  # the radius of the 2nd last circle
1362 Note the second expression, showing how to extract a corner coordinate.
1364 Basic arithmetic resembling those of C operators are available; +, *,
1365 -, /, and %.  So is ^ for exponentiation.  Grouping is permitted in
1366 the usual way using parentheses.  GNU \fBgpic\fP allows logical
1367 operators to appear in expressions; ! (logical negation, not
1368 factorial), &&, ||, ==, !=, >=, <=, <, >.
1370 Various built-in functions are supported: \fBsin(\fIx\fP)\fP,
1371 \fBcos(\fIx\fP)\fP, \fBlog(\fIx\fP)\fP, \fBexp(\fIx\fP)\fP,
1372 \fBsqrt(\fIx\fP)\fP, \fBmax(\fIx\fP,\fIy\fP)\fP,
1373 \fBatan2(\fIx\fP,\fIy\fP)\fP, \fBmin(\fIx\fP,\fIy\fP)\fP,
1374 \fBint(\fIx\fP\fB)\fP, and \fBrand()\fP, Both \fBexp\fP and \fBlog\fP are
1375 base 10; \fBint\fP does integer truncation; and \fBrand()\fP returns a
1376 random number in [0-1).
1378 GNU \fBgpic\fP also documents a one-argument form or rand,
1379 \fBrand(\fIx\fP\fB)\fP, which returns a random number between 1 and
1380 \fIx\fP, but this is deprecated and may be removed in a future
1381 version.
1383 The function \fBsprintf()\fP behaves like a C \fIsprintf\fP(3) that
1384 only takes %, %e, %f, and %g format strings.
1385 .NH 1
1386 Macros
1388 You can define macros in \fBpic\fP.  This is useful for diagrams with
1389 repetitive parts.  In conjunction with the scope rules for block
1390 composites, it effectively gives you the ability to write functions.
1392 The syntax is
1395 \fBdefine\fP \fIname\fP \fB{\fP \fIreplacement text \fB}\fP
1399 This defines \fIname\fR as a macro to be replaced by the replacement
1400 text (not including the braces).  The macro may be called as 
1403 \fIname\fB(\fIarg1, arg2, ... argn\fB)\fR
1407 The arguments (if any) will be substituted for tokens $1, $2 ... $n
1408 appearing in the replacement text.
1410 As an example of macro use, consider the following:
1414 \&.PS
1415 \&# Plot a single jumper in a $1 by $2 box, $3 is the on-off state
1416 \&define jumper { [
1417 \&    shrinkfactor = 0.8;
1418 \&    Outer: box invis wid 0.5 ht 1;
1420 \&    # Count on end ] to reset these
1421 \&    boxwid = Outer.wid * shrinkfactor / 2;
1422 \&    boxht  = Outer.ht  * shrinkfactor / 2;
1424 \&    box fill (!$1) with .s at center of Outer;
1425 \&    box fill ($1)  with .n at center of Outer;
1426 \&] }
1428 \&# Plot a block of six jumpers
1429 \&define jumperblock {
1430 \&    jumper($1);
1431 \&    jumper($2);
1432 \&    jumper($3);
1433 \&    jumper($4);
1434 \&    jumper($5);
1435 \&    jumper($6);
1437 \&    jwidth  = last [].Outer.wid;
1438 \&    jheight = last [].Outer.ht;
1440 \&    box with .nw at 6th last [].nw wid 6*jwidth ht jheight;
1442 \&    # Use {} to avoid changing position from last box draw.
1443 \&    # This is necessary so move in any direction will work as expected
1444 \&    {"Jumpers in state $1$2$2$3$4$5$6" at last box .s + (0, -0.2);}
1447 \&# Sample macro invocations
1448 \&jumperblock(1,1,0,0,1,0);
1449 \&move;
1450 \&jumperblock(1,0,1,0,1,1);
1455 It yields the following:
1458 # Plot a single jumper in a $1 by $2 box, $3 is the on-off state
1459 define jumper { [
1460     shrinkfactor = 0.8;
1461     Outer: box invis wid 0.5 ht 1;
1463     # Count on end ] to reset these
1464     boxwid = Outer.wid * shrinkfactor / 2;
1465     boxht  = Outer.ht  * shrinkfactor / 2;
1467     box fill (!$1) with .s at center of Outer;
1468     box fill ($1)  with .n at center of Outer;
1469 ] }
1471 # Plot a block of six jumpers
1472 define jumperblock {
1473     jumper($1);
1474     jumper($2);
1475     jumper($3);
1476     jumper($4);
1477     jumper($5);
1478     jumper($6);
1480     jwidth  = last [].Outer.wid;
1481     jheight = last [].Outer.ht;
1483     box with .nw at 6th last [].nw wid 6*jwidth ht jheight;
1485     # Use {} to avoid changing position from last box draw.
1486     # This is necessary so move in any direction will work as expected
1487     {"Jumpers in state $1$2$2$3$4$5$6" at last box .s + (0, -0.2);}
1490 # Sample macro invocations
1491 jumperblock(1,1,0,0,1,0);
1492 move;
1493 jumperblock(1,0,1,0,1,1);
1495 .CE "1: Sample use of a macro"
1497 This macro example illustrates how you can combine [], brace grouping,
1498 and variable assignment to write true functions.
1500 One detail the example above does not illustrate is the fact that
1501 macro argument parsing is not token-oriented.  If you call \fBjumper(
1502 1 )\fP, the value of $1 will be \fB" 1 "\fP. You could even call
1503 \fBjumper(big  string)\fP to give $1 the value \fB"big  string"\fP.
1505 If you want to pass in a coordinate pair, you can avoid getting
1506 tripped up by the comma by wrapping the pair in parentheses.
1508 Macros persist through pictures.  To undefine a mcro, say \fBundef\fP
1509 \fIname\fR; for example,
1511 \f(CWundef jumper\fP
1512 \f(CWundef jumperblock\fP
1515 would undefine the two macros in the jumper block example.
1516 .NH 1
1517 Import/Export Commands
1519 Commands that import or export data between \fBpic\fR and its
1520 environment are described here.
1521 .NH 2
1522 File and Table Insertion
1524 The statement
1526 \f(CWcopy\fP \fIfilename\fR
1528 inserts the contents of \fIfilename\fR in the \fBpic\fP input stream.
1529 Any .PS/.PE pair in the file will be ignored.  This, you can use this
1530 to include pre-generated images.
1532 A variant of this statement replicates the \fBcopy thru\fP feature of
1533 \fIgrap\fP(1).  If you say
1535 \f(CWcopy\fP \fIfilename\fR \f(CWthru\fP \fImacro\fP
1538 calls the \fImacro\fP (which may be either a name or replacement text)
1539 on the arguments obtained by breaking each line of the file into
1540 blank-separated fields.  The macro may have up to 9 arguments.  The
1541 replacement text may be delimited by braces or by a pair of instances
1542 of any character not appearing in the rest of the text.
1544 If you write
1546 \f(CWcopy thru\fP \fImacro\fP
1549 omitting the filename, lines to be parsed are taken from the input
1550 source up to the next .PE.
1552 In either of the \fBcopy\fP commands, GNU \fBgpic\fP permits a
1553 trailing `\fBuntil\fP \fIword\fP' clause to be added which terminates
1554 the copy when the first word matches the argument (the default
1555 behavior is therefore equivalent to \fBuntil .PE\fP,
1557 Accordingly, the command
1560 .ft CW
1562 \&.PS
1563 copy thru % circle at ($1,$2) % until "END"
1564 1 2
1565 3 4
1566 5 6
1569 \&.PE
1574 is equivalent to
1577 .ft CW
1579 \&.PS
1580 circle at (1,2)
1581 circle at (3,4)
1582 circle at (5,6)
1584 \&.PE
1588 .NH 2
1589 Debug Messages
1591 The command \fBprint\fR accepts any number of comma-separated
1592 arguments, concatenates their output forms, and writes the result to
1593 standard error.  Each argument must be an expression, a position, or
1594 a text string.
1595 .NH 2
1596 Escape to Post-Processor
1598 If you write
1600 \fBcommand\fR \fIarg\fR\|.\|.\|.
1603 \fBpic\fP concatenates the arguments and pass them through as a line
1604 to troff or \*(tx.  Each
1605 .I arg
1606 must be an expression, a position, or text.
1607 This has a similar effect to a line beginning with
1608 .B .
1610 \fB\e\fR,
1611 but allows the values of variables to be passed through.
1612 .NH 2
1613 Executing Shell Commands
1615 The command 
1617 \f(CWsh { \fIanything...\fP }\fP
1620 macroexpands the text in braces, then executes it as a shell command.
1621 This could be used to generate images or data tables for later
1622 inclusion.  The delimiters shown as {} here may also be two copies of
1623 any one character not present in the shell command text.  In either
1624 case, the body may contain balanced {} pairs.  Strings in the body
1625 may contain balanced or unbalanced braces in any case.
1626 .NH 1
1627 Control-flow constructs
1629 The \fBpic\fP language provides conditionals and looping.  For
1630 example,
1634 pi = atan2(0, -1);
1635 for i = 0 to 2 * pi by 0.1 do {
1636     "-" at (i/2, 0);
1637     "." at (i/2, sin(i)/2);
1638     ":" at (i/2, cos(i)/2);
1644 which yields this:
1647 pi = atan2(0, -1);
1648 for i = 0 to 2 * pi by 0.1 do {
1649     "-" at (i/2, 0);
1650     "." at (i/2, sin(i)/2);
1651     ":" at (i/2, cos(i)/2);
1654 .CE "1: Plotting with a \fBfor\fP loop"
1656 The syntax of the \fBfor\fP statement is:
1658 \fBfor\fR \fIvariable\fR \fB=\fR \fIexpr1\fR \fBto\fR \fIexpr2\fR \
1659 [\fBby\fR [\fB*\fR]\fIexpr3\fR] \fBdo\fR \fIX\fR \fIbody\fR \fIX\fR
1661 The semantics are as follows:  Set
1662 .I variable
1663 to \fIexpr\fR1 .
1664 While the value of
1665 .I variable
1666 is less than or equal to
1667 \fIexpr2\fR,
1669 .I body
1670 and increment
1671 .I variable
1673 \fIexpr3\fR;
1675 .B by
1676 is not given, increment
1677 .I variable
1678 by 1.
1680 .I expr3
1681 is prefixed by
1682 .B *
1683 then
1684 .I variable
1685 will instead be multiplied by
1686 \fIexpr3\fR.
1687 .I X
1688 can be any character not occurring in
1689 \fIbody\fR; or the two Xs may be paired braces (as in the \fBsh\fR command).
1691 The syntax of the \fBif\fP statement is as follows:
1693 \fBif\fR \fIexpr\fR \fBthen\fR \fIX\fR \fIif-true\fR \fIX\fR \
1694 [\fBelse\fR \fIY\fR \fIif-false\fR \fIY\fR]
1696 Its semantics are as follows: Evaluate
1697 \fIexpr\fR;
1698 if it is non-zero then do
1699 \fIif-true\fR,
1700 otherwise do
1701 \fIif-false\fR.
1702 .I X
1703 can be any character not occurring in
1704 \fIif-true\fR.
1705 .I Y
1706 can be any character not occurring in
1707 \fIif-false\fR.
1709 Eithe or both of the X or Y pairs may instead be balanced pairs of
1710 braces ({ and }) as in the \fBsh\fR command.  In either case, the
1711 \fIif-true\fR may contain balanced pairs of braces.  None of these
1712 delimiters will be seen inside strings.
1714 All the usual relational operators my be used in conditional expressions;
1715 ! (logical negation, not factorial), &&, ||, ==, !=, >=, <=, <, >.
1717 String comparison is also supported using == and !=.  String
1718 comparisons may need to be parenthesized to avoid syntactic
1719 ambiguities.
1720 .NH 1
1721 Interface To [gt]roff
1723 The output of \fBpic\fP is \fB[gt]roff\fP drawing commands.  The GNU
1724 \fIgpic\fP(1) command warns that it relies on drawing extensions
1725 present in \fIgroff\fP(1) that are not present in \fItroff\fP(1).
1726 .NH 2
1727 Scaling Arguments
1729 The DWB \fIpic\fP(1) program will accept one or two arguments to
1730 \&\fB.PS\fP, which is interpreted as a width and height in inches to
1731 which the results of \fIpic\fP(1) should be scaled (width and height
1732 scale independently).  If there is only one argument, it is
1733 interpreted as a width to scale the picture to, and height will be
1734 scaled by the same proportion.
1736 GNU \fBgpic\fP is less general; it will accept a single width to scale
1737 to, or a zero width and a maximum height to scale to.  With
1738 two nonzero arguments, it will scale to the maximum height.
1739 .NH 2
1740 How Scaling is Handled
1742 When \fBpic\fP processes a picture description on input, it passes .PS
1743 and .PE through to the postprocessor. The .PS gets decorated with two
1744 numeric arguments which are the X and Y dimensions of the picture in
1745 inches.  The post-processor can use these to reserve space for the
1746 picture and center it.
1748 The \fBmgs\fP macros, for example, include the following definitions:
1752 \&.de PS
1753 \&.br
1754 \&.sp \\n[DD]u
1755 \&.ie \\n[.$]<2 .@error bad arguments to PS (not preprocessed with pic?)
1756 \&.el \{\
1757 \&.     ds@need (u;\\$1)+1v
1758 \&.     in +(u;\\n[.l]-\\n[.i]-\\$2/2>?0)
1759 \&.\}
1760 \&..
1761 \&.de PE
1762 \&.par@reset
1763 \&.sp \\n[DD]u+.5m
1764 \&..
1769 Equivalent definition will be supplied by GNU \fIpic\fP(1) if you use
1770 the -mpic option; this should make it usable with macro pages other
1771 than \fIms\fR(1).
1773 if .PF is used instead of .PE, the \fBtroff\fP position is restored to
1774 what it was at the picture start (Kernighan notes that the F stands
1775 for "flyback").
1777 The invocation
1779 \&\fB.PS <\fP\fIfile\fP
1782 causes the contents of \fIfile\fP to replace the .PS line.  This
1783 feature is deprecated; use \fBcopy file\fR instead).
1785 By default, input lines that begin with a period are passed to the
1786 postprocessor, embedded at the corresponding point in the output.
1787 Messing with horizontal or vertical spacing is an obvious recipe for
1788 bugs, but point size and font changes will usually be safe.
1790 Point sizes and font changes are also safe within text strings, as
1791 long as they are undone before the end of string.
1793 The state of \fB[gt]roff\fP's fill mode is preserved across pictures.
1795 The Kernighan paper notes that there is a subtle problem with
1796 complicated equations inside \fBpic\fR pictures; they come out wrong if
1797 \fIeqn\fP(1)  has to leave extra vertical space for the equation.
1798 If your equation involves more than subscripts and superscripts, you
1799 must add to the beginning of each equation the extra information
1800 \f(CWspace 0\fP.  He gives the following example:
1804 arrow
1805 box "$space 0 {H( omega )} over {1 - H( omega )}$"
1806 arrow
1811 delim @@
1815 arrow
1816 box "@space 0 {H( omega )} over {1 - H( omega )}@"
1817 arrow
1819 .CE "1: Equations within pictures"
1820 .NH 1
1821 Interface to TeX
1824 \*(tx mode is enabled by the
1825 .B \-t
1826 option.
1827 In \*(tx mode, pic will define a vbox called
1828 .B \egraph
1829 for each picture.
1830 You must yourself print that vbox using, for example, the command
1834 \ecenterline{\ebox\egraph}
1837 Actually, since the vbox has a height of zero this will produce
1838 slightly more vertical space above the picture than below it;
1842 \ecenterline{\eraise 1em\ebox\egraph}
1845 would avoid this.
1847 You must use a \*(tx driver that supports the
1848 .B tpic
1849 specials, version 2.
1851 Lines beginning with
1852 .B \e
1853 are passed through transparently; a
1854 .B %
1855 is added to the end of the line to avoid unwanted spaces.
1856 You can safely use this feature to change fonts or to
1857 change the value of \fI\ebaselineskip\fR.
1858 Anything else may well produce undesirable results; use at your own risk.
1859 Lines beginning with a period are not given any special treatment.
1861 The \(*tx mode of \fIpic\fP(1) will \fInot\fP translate \fBtroff\fP
1862 font and size changes included in text strings!
1863 .NH 1
1864 Obsolete Commands
1866 GNU \fIgpic\fP(1)  has a command
1868 \fBplot\fR \fIexpr\fR [\fB"\fItext\fB"\fR]
1870 This is a text object which is constructed by using
1871 .I text
1872 as a format string for sprintf
1873 with an argument of
1874 \fIexpr\fP.
1876 .I text
1877 is omitted a format string of "%g" is used.
1878 Attributes can be specified in the same way as for a normal text
1879 object.
1880 Be very careful that you specify an appropriate format string;
1881 \fBpic\fP does only very limited checking of the string.
1882 This is deprecated in favour of
1883 \fBsprintf\fP.
1884 .NH 1
1885 Some Larger Examples
1887 Here are a few larger examples, with complete source code.
1889 One of our earlier examples is generated in an instructive way using a
1890 for loop:
1894 \&.PS
1895 \&# Draw a demonstration up left arrow with grid box overlay
1896 \&define gridarrow
1898 \&    [
1899 \&        {arrow up left $1;}
1900 \&        box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
1901 \&        for i = 2 to ($1 / 0.5) do
1902 \&        {
1903 \&            box wid 0.5 ht 0.5 dotted with .sw at last box .se;
1904 \&        }
1905 \&        move down from last arrow .center;
1906 \&        [
1907 \&            if ( $1 == boxht ) then { "\\fBline up left\\fP" } else { sprintf("\\fBarrow up left %g\\fP", $1) };
1908 \&        ]
1909 \&    ] 
1910 \&    move right from last [] .e;
1912 \&gridarrow(0.5);
1913 \&gridarrow(1);
1914 \&gridarrow(1.5);
1915 \&gridarrow(2);
1916 \&undef gridarrow
1917 \&.PE
1923 # Draw a demonstration up left arrow with grid box overlay
1924 define gridarrow
1926         [
1927                 {arrow up left $1;}
1928                 box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
1929                 for i = 2 to ($1 / 0.5) do
1930                 {
1931                         box wid 0.5 ht 0.5 dotted with .sw at last box .se;
1932                 }
1933                 move down from last arrow .center;
1934                 [
1935                         if ( $1 == boxht ) then { "\fBline up left\fP" } else { sprintf("\fBarrow up left %g\fP", $1) };
1936                 ]
1937         ] 
1938         move right from last [] .e;
1940 gridarrow(0.5);
1941 gridarrow(1);
1942 gridarrow(1.5);
1943 gridarrow(2);
1944 undef gridarrow
1946 .CE "1: Diagonal arrows (dotted boxes show the implied 0.5-inch grid)"
1948 Here's an example concocted to demonstrate layout of a large,
1949 multiple-part pattern:
1953 \&.PS
1954 \&define filter {box ht 0.25 rad 0.125}
1955 \&lineht = 0.25;
1956 \&Top: [
1957 \&    right;
1958 \&    box "\\fBms\\fR" "sources";
1959 \&    move;
1960 \&    box "\\fBHTML\\fR" "sources";
1961 \&    move;
1962 \&    box "\\fBlinuxdoc-sgml\\fP" "sources" wid 1.5;
1963 \&    move;
1964 \&    box "\\fBTexinfo\\fP" "sources";
1966 \&    line down from 1st box .s lineht;
1967 \&    A: line down;
1968 \&    line down from 2nd box .s; filter "\\fBhtml2ms";
1969 \&    B: line down;
1970 \&    line down from 3rd box .s; filter "\\fBformat\\fP";
1971 \&    C: line down;
1972 \&    line down from 4th box .s; filter "\\fBtexi2roff\\fP";
1973 \&    D: line down;
1975 \&move down 1 from last [] .s;
1976 \&Anchor: box wid 1 ht 0.75 "\\fBms\\fR" "intermediate" "form";
1977 \&arrow from Top.A.end to Anchor.nw;
1978 \&arrow from Top.B.end to 1/3 of the way between Anchor.nw and Anchor.ne;
1979 \&arrow from Top.C.end to 2/3 of the way between Anchor.nw and Anchor.ne;
1980 \&arrow from Top.D.end to Anchor.ne
1982 \&    # PostScript column
1983 \&    move to Anchor .sw;
1984 \&    line down left then down ->;
1985 \&    filter "\\fBpic\\fP";
1986 \&    arrow;
1987 \&    filter "\\fBeqn\\fP";
1988 \&    arrow;
1989 \&    filter "\\fBtbl\\fP";
1990 \&    arrow;
1991 \&    filter "\\fBgroff\\fP";
1992 \&    arrow;
1993 \&    box "PostScript";
1995 \&    # HTML column
1996 \&    move to Anchor .se;
1997 \&    line down right then down ->;
1998 \&    A: filter dotted "\\fBpic2img\\fP";
1999 \&    arrow;
2000 \&    B: filter dotted "\\fBeqn2html\\fP";
2001 \&    arrow;
2002 \&    C: filter dotted "\\fBtbl2html\\fP";
2003 \&    arrow;
2004 \&    filter "\\fBms2html\\fP";
2005 \&    arrow;
2006 \&    box "HTML";
2008 \&    # Nonexistence caption
2009 \&    box dashed wid 1 at B + (2, 0) "These tools" "don't yet exist";
2010 \&    line chop 0 chop 0.1 dashed from last box .nw to A.e ->;
2011 \&    line chop 0 chop 0.1 dashed from last box .w  to B.e ->;
2012 \&    line chop 0 chop 0.1 dashed from last box .sw to C.e ->;
2014 \&.PE
2020 define filter {box ht 0.25 rad 0.125}
2021 lineht = 0.25;
2022 Top: [
2023     right;
2024     box "\fBms\fR" "sources";
2025     move;
2026     box "\fBHTML\fR" "sources";
2027     move;
2028     box "\fBlinuxdoc-sgml\fP" "sources" wid 1.5;
2029     move;
2030     box "\fBTexinfo\fP" "sources";
2032     line down from 1st box .s lineht;
2033     A: line down;
2034     line down from 2nd box .s; filter "\fBhtml2ms";
2035     B: line down;
2036     line down from 3rd box .s; filter "\fBformat\fP";
2037     C: line down;
2038     line down from 4th box .s; filter "\fBtexi2roff\fP";
2039     D: line down;
2041 move down 1 from last [] .s;
2042 Anchor: box wid 1 ht 0.75 "\fBms\fR" "intermediate" "form";
2043 arrow from Top.A.end to Anchor.nw;
2044 arrow from Top.B.end to 1/3 of the way between Anchor.nw and Anchor.ne;
2045 arrow from Top.C.end to 2/3 of the way between Anchor.nw and Anchor.ne;
2046 arrow from Top.D.end to Anchor.ne
2048     # PostScript column
2049     move to Anchor .sw;
2050     line down left then down ->;
2051     filter "\fBpic\fP";
2052     arrow;
2053     filter "\fBeqn\fP";
2054     arrow;
2055     filter "\fBtbl\fP";
2056     arrow;
2057     filter "\fBgroff\fP";
2058     arrow;
2059     box "PostScript";
2061     # HTML column
2062     move to Anchor .se;
2063     line down right then down ->;
2064     A: filter dotted "\fBpic2img\fP";
2065     arrow;
2066     B: filter dotted "\fBeqn2html\fP";
2067     arrow;
2068     C: filter dotted "\fBtbl2html\fP";
2069     arrow;
2070     filter "\fBms2html\fP";
2071     arrow;
2072     box "HTML";
2074     # Nonexistence caption
2075     box dashed wid 1 at B + (2, 0) "These tools" "don't yet exist";
2076     line chop 0 chop 0.1 dashed from last box .nw to A.e ->;
2077     line chop 0 chop 0.1 dashed from last box .w  to B.e ->;
2078     line chop 0 chop 0.1 dashed from last box .sw to C.e ->;
2081 .CE "2: Hypothetical production flow for dual-mode publishing"
2083 .\"%%REFERENCE%%
2084 .NH 1
2085 PIC Reference
2087 This is an annotated grammar of PIC.
2088 .NH 2
2089 Lexical Items
2091 In general, \fBpic\fP is a free-format, token-oriented language that
2092 ignores whitespace outside strings.  But certain lines and contructs
2093 are specially interpreted at the lexical level:
2095 A comment begins with # and continues to \en (comments may also follow
2096 text in a line).  A line beginning with a period or backslash may be
2097 interpreted as text to be passed through to the post-processor,
2098 depending on command-line options.  An end-of-line backslash is
2099 interpreted as a request to continue the line; the backslash and
2100 following newline are ignored.
2102 Here are the grammar terminals:
2103 .IP <number> 
2104 A decimal numeric constant.  May contain a decimal point or be
2105 expressed in scientific notation in the style of \fIprintf\fP(3)'s %e
2106 escape. (All variables are represented internally in floating-point.)
2107 .IP <string>
2108 Any ASCII characters surrounded by a pair of double quotes.  May
2109 contain a double quote if preceded by a backslash.
2110 .IP <variable>
2111 A lower-case alphabetic character, followed by any number of
2112 alphanumerics.  (Values of variables are preserved across pictures.)
2113 .IP <label>
2114 An upper-case alphabetic character, followed by any number of
2115 alphanumerics.
2116 .NH 2
2117 Semi-Formal Grammar
2119 Tokens not enclosed in <> are literals, except:
2120 .IP 1.
2121 \en is a newline
2122 .IP 2.
2123 three dots is a suffix meaning \&`replace with 0 or more repetitions
2124 of the preceding element.
2125 .IP 3.
2126 enclosure in square brackets has its usual meaning of `this clause is
2127 optional'.
2128 .IP 4.
2129 Square-bracket-enclosed portions within tokens are optional.  Thus,
2130 \fBh[eigh]t\fR matches either `height' or `ht'.
2132 If one of these special tokens has to be referred to literally, it is
2133 surrounded with single quotes.
2135 The top-level \fBpic\fP object is a picture.
2138 <picture>    ::= .PS [width [height]]\en
2139                  <statement> ...
2140                  .PE \en
2144 The \fIwidth\fP and \fIheight\fP arguments, if present, cause
2145 \fBpic\fR to attempt to scale the picture to the given dimensions in
2146 inches.  In no case, however, will the X and Y dimensions of the
2147 picture exceed the values of the style variables \fBmaxpswid\fP,
2148 \fBmaxpsheight\fP (which default to the normal 8.5 by 11 page size)\fP.
2150 If the ending .PS is replaced by .PF, the page vertical position is
2151 restored to its value at the time .PS was encountered.  Another
2152 alternate form of invocation is \f(CW.PS <\fIfilename\fR, which
2153 replaces the .PS line with a file to be interpreted by \fBpic\fR (but
2154 this feature is deprecated).
2156 The .PS, .PE, and .PF macros to perform centering and scaling are
2157 normally supplied by the post-processor.
2161 <statement>  ::= <command> ;
2162                  <command> \en
2164 <command>    ::= <primitive> <modifier>...
2165                  <label> : <command>
2166                  <label> : <position>
2167                  <variable> = <expr>
2168                  <direction>
2169                  { <command> ... }
2170                  '[' <command> ... ']'
2171                  for <var> = <expr> to <expr> [by <expr>] do { <command> ... }
2172                  if <expr> then { <command> ... } [else { <command> ... }]
2173                  copy <filename> [until <word>]
2174                  copy <filename> thru <macroname> [until <word>] 
2175                  sh <balanced-text>
2176                  print <print-item>
2177                  reset [ <variable> ... ]
2181 The current position and direction are saved on entry to a { } and
2182 restored on exit from it.
2184 Drawn objects within [ ] are treated as a single composite object with
2185 a rectangular shape (that of the bounding box of all the elements).
2186 Variable and label assignments within a block are local to the block.
2187 Current direction of motion is restored to the value at start of block
2188 upon exit.  Position is \fInot\fR restored (unlike { }) instead, the
2189 current position becomes the exit position for the current direction
2190 on the block's bounding box.
2193 <primitive>  ::= box                   \fR# Closed object -- rectangle\fP
2194                  circle                \fR# Closed object -- circle\fP
2195                  ellipse               \fR# Closed object -- ellipse\fP
2196                  arc                   \fR# Open object -- quarter-circle\fP
2197                  line                  \fR# Open object -- line\fP
2198                  arrow                 \fR# Open object -- line with arrowhead\fP
2199                  spline                \fR# Open object -- spline curve\fP
2200                  move
2201                  <text> <text> ...     \fR# Text within invisible box\fP
2203 <attribute>  ::= h[eigh]t <expr>       \fR# Set height of closed figure \fP
2204                  wid[th] <expr>        \fR# Set width of closed figure \fP
2205                  rad[ius] <expr>       \fR# Set radius of circle/arc \fP
2206                  diam[eter] <expr>     \fR# Set diameter of circle/arc \fP
2207                  up [ <expr> ]         \fR# Move up \fP
2208                  down [ <expr> ]       \fR# Move down \fP
2209                  left [ <expr> ]       \fR# Move left \fP
2210                  right [ <expr> ]      \fR# Move right \fP
2211                  from <position>       \fR# Set from position of open figure\fP
2212                  to <position>         \fR# Set to position of open figure\fP
2213                  at <position>         \fR# Set center of open figure\fP
2214                  with <corner>         \fR# Fix corner at specified location\fP
2215                  by <expr> <expr>      \fR# Set object's attachment point\fP
2216                  then                  \fR# Sequential segment composition\fP
2217                  dotted [ <expr> ]     \fR# Set dotted line style\fP
2218                  dashed [ <expr> ]     \fR# Set dashed line style\fP
2219                  chop [ <expr> ]       \fR# Chop end(s) of segment\fP
2220                  ->                    \fR# Decorate with "to" arrow\fP
2221                  <-                    \fR# Decorate with "from" arrow\fP
2222                  <->                   \fR# Decorate with both arrows\fP
2223                  invis                 \fR# Make primitive invisible\fP
2224                  solid                 \fR# Make closed figure solid\fP
2225                  fill <expr>           \fR# Set fill density for figure\fP
2226                  same                  \fR# Copy size of previous object\fP
2227                  <text> <text> ...     \fR# Text within object\fP
2228                  <expr>                \fR# Motion in the current direction\fR
2232 Missing attributes are supplied from defaults; inappropriate ones are
2233 silently ignored.  For lines, splines, and arcs, height and width
2234 refer to arrowhead size.
2236 The \f(CWat\fP primitive sets the center of the current object.  The
2237 \f(CWwith\fP attribute fixes the specified feature of the given object
2238 to a specified location.
2240 The \f(CWsolid\fR primitive is not yet supported in GNU \fBgpic\fR.
2242 The \f(CWby\fR primitive is not documented in the tutorial portion of
2243 the Kernighan paper, and should probably be considered unreliable.
2245 The primitive \fBarrow\fR is a synonym for \fBline ->\fR.
2248 <text>  ::=      <string> [ <placement> ... ]
2249                  sprintf("format", <expr> ...) [ <placement> ... ]
2251 <placement> ::=  center | ljust | rjust | above | below
2255 Text is normally an attribute of some object, in which case successive
2256 strings are vertically stacked and centered on the object's center by
2257 default.  Standalone text is treated as though placed in an invisible
2258 box.
2260 A text item consists of a string or sprintf-expression, optionally
2261 followed by positioning information.  Text or format strings may
2262 contain {gtn}roff font changes, size changes, and local motions,
2263 provided those changes are undone before the end of the current item.
2265 A position is an (x, y) coordinate pair.  There are lots of different
2266 ways to specify positions:
2269 <position>  ::=  <expr> , <expr>
2270                  <place> {+-} <expr> , <expr> 
2271                  <place> {+-} ( <expr> , <expr> )
2272                  ( <position> , <position> )
2273                  <expr> [of the way] between <position> and <position> 
2274                  <expr> '<' <position> , <position> '>'
2275                  ( <position> )
2277 <place>  ::=     <label> [ <dot-corner> ]
2278                  <corner> of <label>
2279                  [0|1|2|3|4|5|6|7|8|9]th [last] <primitive> <dot-corner>
2280                  <expr>'th [last]<primitive> <dot-corner>
2281                  <corner> of [0|1|2|3|4|5|6|7|8|9]th [last] <primitive>
2282                  <corner> of <expr>'th [last] <primitive>
2283                  Here
2285 <dot-corner> ::= .n | .e | .w | .s | .ne | .nw | .se | .sw | .c | .start | .end
2287 <corner> ::=     top | bot | left | right | start | end
2291 As Kernighan notes, "since barbarisms like \fB1th\fP and \fB3th\fP are
2292 barbaric, synonyms like \fB1st\fP and \fB3rd\fP are accepted as well".
2293 Objects of a given type are numbered from 1 upwards in order of
2294 declaration; the \fBlast\fP modifier counts backwards.
2296 The "'th" form (which allows you to select a previous object with an
2297 expression, as opposed to a numeric literal) is bnot documented in DWB
2298 \fBpic\fR(1).
2300 The following style variables control output:
2301 .TS H
2302 tab(@), linesize(2);
2303 lb | lb | lb
2304 l | n | l.
2305 .sp 2p
2306 Style Variable@Default@What It Does
2307 .sp 2p
2309 .sp 2p
2311 boxht@0.5@Default height of a box
2312 boxwid@0.75@Default height of a box
2313 lineht@0.5@Default length of vertical line
2314 linewid@0.75@Default length of horizontal line
2315 arcrad @0.25@Default radius of an arc
2316 circlerad@0.25@Default radius of a circle
2317 ellipseht@0.5@Default height of an ellipse
2318 ellipsewid@0.75@Default width of an ellipse
2319 moveht@0.5@Default length of vertical move
2320 movewid@0.75@Default length of horizontal move
2321 textht@0@Default height of box enclosing a text object
2322 textwid@0@Default width of box enclosing a text object
2323 arrowht@0.1@Length of arrowhead along shaft
2324 arrowwid@0.05@Width of rear of arrowhead
2325 arrowhead@1@Enable/disable arrowhead filling
2326 dashwid@0.05@Interval for dashed lines
2327 maxpswid@11@Maximum width of picture
2328 maxpsht@8.5@Maximum height of picture
2329 scale@1@Unit scale factor
2330 fillval@0.5@Default fill value
2331 .sp 5p
2334 Any of these can be set by assignment, or reset using the \fBreset\fP
2335 statement.  Style variables assigned within [] blocks are restored to
2336 their beginning-of-block value on exit; top-level assignments persist
2337 across pictures.  Dimensions are divided by \fBscale\fR on output.
2339 All \fBpic\fP expressions are evaluated in floating point; units
2340 default to inches.  Expressions have the following simple grammar,
2341 with semantics very similar to C expressions:
2344 <expr> ::=       <expr> <op> <expr>
2345                  ! <expr>
2346                  ( <expr> )
2347                  - <expr>
2348                  <variable>
2349                  <number>
2350                  <place> .x
2351                  <place> .y
2352                  <place> .ht
2353                  <place> .wid
2354                  <place> .rad
2355                  sin(<expr>)
2356                  cos(<expr>)
2357                  log(<expr>)
2358                  exp(<expr>)
2359                  sqrt(<expr>)
2360                  max(<expr>, <expr>...)
2361                  atan2(<expr>, <expr>)
2362                  min(<expr>, <expr>...)
2363                  int(<expr>)
2364                  rand()
2366 <op>  :=         + | - | * | / | % | ^ |
2367                  != | == | '<' | '>' | >= | <= |
2368                  '||' | &&
2372 Both \fBexp\fP and \fBlog\fP are base 10; \fBint\fP does integer
2373 truncation; and \fBrand()\fP returns a random number in [0-1).
2375 There are \fBdefine\fP and \fBundef\fR statements which are not part
2376 of the grammar (they behave as pre-processor macros to the language).
2377 These may be used to define pseudo-functions.
2380 \fBdefine\fP \fIname\fP \fB{\fP \fIreplacement text \fB}\fP
2384 This defines \fIname\fR as a macro to be replaced by the replacement
2385 text (not including the braces).  The macro may be called as 
2388 \fIname\fB(\fIarg1, arg2, ... argn\fB)\fR
2392 The arguments (if any) will be substituted for tokens $1, $2 ... $n
2393 appearing in the replacement text. To undefine a mcro, say \fBundef\fP
2394 \fIname\fR, specifying the name to be undefined.
2395 .\"%%POSTLUDE%%
2396 .NH 1
2397 History and Acknowledgements
2399 Original \fBpic\fP was written to go with Joseph Ossanna's original
2400 \&\fItroff\fP(1) by Brian Kernighan, and later re-written by Kernighan
2401 with substantial enhancements (apparently as part of the evolution of
2402 \&\fItroff\fP(1) into \&\fIditroff\fP(1) to generate
2403 device-independent output).
2405 The language had been inspired by some earlier graphics languages
2406 including \fBideal\fP and \fBgrap\fP.  Kernighan credits Chris van Wyk
2407 (the designer of \fBideal\fP) with many of the ideas that went into
2408 \fBpic\fP.
2410 delim $$
2413 The \fBpic\fP language was originally described by Brian Kernighan in
2414 Bell Labs Computing Science Technical Report #116 (you can obtain a
2415 PostScript copy of the revised version, [1], by sending a mail message to
2416 \&\fInetlib@research.att.com\fP with a body of \&`send 116 from
2417 research/cstr'.).  There have been two revisions, in 1984 and 1991.
2419 The document you are reading effectively subsumes Kernighan's
2420 description; it was written to fill in lacunae in the exposition and
2421 integrate in descriptions of the GNU \fIgpic\fP(1) features.
2423 The GNU \fBgpic\fR implementation was written and is maintained by
2424 James Clark \fI<jjc@jclark.com>\fP.
2425 .NH 1
2426 Bibliography
2427 .IP 1.
2428 Kernighan, B. W. \fBPIC -- A Graphics Language for Typesetting
2429 (Revised User Manual)\fP Bell Labs Computing Science Technical Report
2430 #116, December 1991.
2431 .IP 2.
2432 Van Wyk, C.J. \fBA high-level language for specifying pictures\fP \fIACM
2433 Transactions On Graphics\fP 1,2 (1982) 163-182.