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.
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.
10 .\" This document was written for free use and redistribution by
11 .\" Eric S. Raymond <esr@thyrsus.com> in August 1995.
16 .ie t .ds tx T\h'-.1667m'\v'.224m'E\v'-.224m'\h'-.125m'X
19 .de CE\" Centered caption for figure. Assumes previous .KS
25 .\" Definitions end here
27 Making Pictures With GNU PIC
31 <esr@snark.thyrsus.com>
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).
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.
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.
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.
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
70 In this document, the \fIgpic\fP(1) extensions will be marked as such.
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.
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
85 Other details of the \fI[gt]roff\fP(1) interface
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,
95 pic:pic.ms:<nnn>: parse error before `<token>'
96 pic:pic.ms:<nnn>: giving up on this picture
101 where <nnn> is a line number, and <token> is a token near (usually
102 just after) the error location.
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:
117 box width 1.2 "\fIgtbl\fP(1) or \fIgeqn\fP(1)" "(optional)" dashed;
119 box "\fIgtroff\fP(1)";
123 .CE "1: Flow of \fBpic\fP data"
125 This was produced from the following \fBpic\fP program:
130 \&ellipse "document";
132 \&box "\\fIpic\\fP(1)"
134 \&box width 1.2 "\\fIgtbl\\fP(1) or \\fIgeqn\\fP(1)" "(optional)" dashed;
136 \&box "\\fIgtroff\\fP(1)";
138 \&ellipse "PostScript"
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:
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:
200 \&arc; down; move; "arc"
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:
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
228 line "1" "2" "3" "4";
229 line "1" "2" "3" "4" "5";
234 for example, gives you this:
241 line "1" "2" "3" "4";
242 line "1" "2" "3" "4" "5";
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:
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:
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
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
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
303 Default Sizes of Objects
305 Here are the default sizes for \fBpic\fP objects:
317 box@0.75" wide by 0.5" high
319 ellipse@0.75" wide by 0.5" high
329 The simplest way to think about these defaults is that they make the
330 other basic objects fit snugly into a default-sized box.
332 Objects Do Not Stretch!
334 Text is rendered in the current font with normal troff line spacing.
335 Boxes, circles, and ellipses do \fInot\fP automatically resize to fit
336 enclosed text. Thus, if you say \fBbox "text far too long"\fP
340 box "this text is far too long for a default box"
342 .CE "1: Boxes do not automatically resize"
344 which is probably not the effect you want.
348 To change the box size, you can specify a box width with the "width"
352 box width 3 "this text is far too long for a default box"
354 .CE "2: Result of \fBbox width 3 \"text far too long\"\fP"
356 This modifier takes a dimension in inches. There is also a "height"
357 modifier that will change a box's height. The \fBwidth\fP keyword may
358 be abbreviated to \fBewid\fP; the \fBheight\fP keyword to
361 Resizing Other Object Types
363 To change the size of a circle, give it a \fBrad\fP or \fBdiam\fP
364 modifier; this changes the radius or diameter of the circle, according
365 to the numeric argument that follows.
368 {circle rad 0.1; move down 0.2 from last circle .s; "0.1"};
369 move; circle rad 0.2 "0.2"; move; circle rad 0.3 "0.3";
371 .CE "3: Circles with increasing radii"
373 The \fBmove\fP command can also take a dimension, which just tells
374 it how many inches to move in the current direction.
376 Ellipses are sized to fit in the rectangular box defined by their
377 axes, and can be resized with \fBwidth\fP and \fBheight\fP like boxes.
379 You can also change the radius of curvature of an arc with \fBrad\fP
380 (which specifies the radius of the circle of which the arc is a segnmment).
381 Larger values yield flatter arcs.
384 {arc rad 0.1; move down 0.3 from last arc .center; "0.1"};
386 {arc rad 0.2; move down 0.4 from last arc .center; "0.2"};
388 {arc rad 0.3; move down 0.5 from last arc .center; "0.3"};
390 .CE "4: \fBarc rad\fP with increasing radii"
392 Observe that because an arc is defined as a quarter circle, increasing
393 the radius also increases the size of the arc's bounding box.
397 In place of a dimension specification, you can use the keyword
398 \fBsame\fR. This gives the object the same size as the previous one
399 of its type. As an example, the program
404 \&box; box wid 1 ht 1; box same; box
413 box; box wid 1 ht 1; box same; box
415 .CE "5: The \fBsame\fP keyword"
418 Generalized Lines and Splines
422 It is possible to specify diagonal lines or arrows by adding multiple \fBup\fP,
423 \fBdown\fP, \fBleft\fP, and \fBright\fP modifiers to the line object.
424 Any of these can have a multiplier. To understand the effects, think
425 of the drawing area as being gridded with standard-sized boxes.
428 # Draw a demonstration up left arrow with grid box overlay
433 box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
434 for i = 2 to ($1 / 0.5) do
436 box wid 0.5 ht 0.5 dotted with .sw at last box .se;
438 move down from last arrow .center;
440 if ( $1 == boxht ) then { "\fBline up left\fP" } else { sprintf("\fBarrow up left %g\fP", $1) };
443 move right from last [] .e;
451 .CE "1: Diagonal arrows (dotted boxes show the implied 0.5-inch grid)"
453 Multi-Segment Line Objects
455 A "line" or "arrow" object may actually be a path consisting of any number of
456 segments of varying lengths and directions. To describe a path,
457 connect several line or arrow commands with the keyword \fBthen\fP.
460 define zigzag { $1 right 1 then down .5 left 1 then right 1 }
463 .CE "2: \fBline right 1 then down .5 left 1 then right 1\fP"
467 If you start a path with the \fBspline\fP keyword, the path vertices
468 are treated as control points for a spline curve fit.
472 move down 0.2 from last [] .s;
473 "The spline curve..."
474 move right from last [] .e;
477 spline from start of last line right 1 then down .5 left 1 then right 1;
478 "1" at last spline .start + (-0.1, 0);
479 "2" at last spline .start + (1.1, 0);
480 "3" at last spline .end + (-1.1, 0);
481 "4" at last spline .end + (0.1, 0);
483 move down 0.2 from last [] .s;
484 "...with tangents displayed"
487 .CE "3: \fBspline right 1 then down .5 left 1 then right 1\fP"
489 You can describe many natural-looking but irregular curves this
493 [spline right then up then left then down ->;]
494 move down 0.2 from last [] .s;
495 ["\fBspline right then up then left then down ->;\fP"]
496 move right 3 from last [] .se;
497 "\fBspline left then up right then down right ->;\fP"
499 [spline left then up right then down right ->;]
501 .CE "4: Two more spline examples"
503 Note the arrow decorations. Arrowheads can be applied naturally to
504 any path-based object, line or spline. We'll see how in the next
511 We've already seen that the modifier \fBdashed\fP can change the line
512 style of an object from solid to dashed. GNU \fBgpic\fP permits you to
513 dot or dash ellipses, circles, and arcs (and splines in \(*tx mode
514 only); some versions of DWB may only permit dashing of lines and
515 boxes. It's possible to change the dash interval by specifying a
516 number after the modifier.
520 box dashed "default";
522 box dashed 0.05 "0.05";
524 box dashed 0.1 "0.1";
526 box dashed 0.15 "0.15";
528 box dashed 0.2 "0.2";
530 .CE "1: Dashed objects"
534 Another available qualifier is \fBdotted\fP. GNU \fBgpic\fP permits
535 you to dot or dash ellipses, circles, and arcs (and splines in \(*tx
536 mode only); some versions of DWB may only permit dashing of lines and
537 boxes. It too can be suffixed with a number to specify the interval
541 box dotted "default";
543 box dotted 0.05 "0.05";
545 box dotted 0.1 "0.1";
547 box dotted 0.15 "0.15";
549 box dotted 0.2 "0.2";
551 .CE "2: Dotted objects"
555 It is also possible, in GNU \fBgpic\fP only, to modify a box so it has
559 box rad 0.05 "rad 0.05";
561 box rad 0.1 "rad 0.1";
563 box rad 0.15 "rad=0.15";
565 box rad 0.2 "rad=0.2";
567 box rad 0.25 "rad=0.25";
569 .CE "3: \fBbox rad\fP with increasing radius values;"
571 Radius values higher than half the minimum box dimension are silently
572 truncated to that value.
576 Lines and arcs can be decorated as well. Any line or arc (and any
577 spline as well) can be decorated with arrowheads by adding one or more
583 .CE "4: Double-headed line made with \fBline <- ->\fP"
585 In fact, the \fBarrow\fP command is just shorthand for \fBline ->\fP. And
586 there is a double-head modifier <->, so the figure above could have been made
587 with \fCWline <->\fP.
589 Arrowheads have a \fBwidth\fP attribute, the distance across the rear;
590 and a \fBheight\fP attribute, the length of the arrowhead along the shaft.
592 Arrowhead style is controlled by the style variable \fBarrowhead\fP.
593 The DWB and GNU versions interpret it differently. DWB defaults to
594 open arrowheads and an \fBarrowhead\fP value of 2; the Kernighan
595 paper says a value of 7 will make solid arrowheads. GNU \fBgpic\fP
596 defaults to solid arrowheads and an \fBarrowhead\fP value of 1; a
597 value of 0 will produce open arrowheads.
601 It's also possible to change the line thickness of an object (this is
602 a GNU extension, DWB \fBpic\fP doesn't support it.).
603 The default thickness of the lines used to draw objects is controlled by the
606 This gives the thickness of lines in points.
607 A negative value means use the default thickness:
608 in \(*tx output mode, this means use a thickness of 8 milliinches;
609 in \(*tx output mode with the
611 option, this means use the line thickness specified by
613 lines; in troff output mode, this means use a thickness proportional
614 to the pointsize. A zero value means draw the thinnest possible line
615 supported by the output device. Initially it has a value of -1.
616 There is also a \fBthickness\fP attribute (which can be abbreviated to
617 \fBthick\fP). For example, \fBcircle thickness 1.5\fP would draw a
618 circle using a line with a thickness of 1.5 points. The thickness of
619 lines is not affected by the value of the
621 variable, nor by any width or height given in the
627 The modifier \fBinvis\fP makes an object entirely invisible. This
628 used to be useful for positioning text in an invisible object that is
629 properly joined to neighboring ones. Newer DWB versions and GNU
630 \fBpic\fP treat standalone text in exactly this way.
634 It is possible to fill boxes, circles, and ellipses. The
635 modifier \fBfill[ed]\fP accomplishes this. You can suffix it with a fill
636 value; the default is given by the stule variable \fBfillval\fP.
638 DWB \fBpic\fP and \fBgpic\fP have opposite conventions for fill values
639 and different defaults. DWB \fBfillval\fP defaults to 0.3 and smaller
640 values are darker; GNU \fBfillval\fP uses 0 for white and 1 for black.
643 circle fill; move; circle fill 0.4; move; circle fill 0.2;
645 .CE "5: \fBcircle fill; move; circle fill 0.4; move; circle fill 0.9;\fB"
647 GNU \fBgpic\fP makes some additional guarantees. A fill value greater
648 than 1 can also be used: this means fill with the shade of gray that
649 is currently being used for text and lines. Normally this will be
650 black, but output devices may provide a mechanism for changing this.
651 The invisible attribute does not affect the filling of objects. Any
652 text associated with a filled object will be added after the object
653 has been filled, so that the text will not be obscured by the filling.
655 The closed-object modifier \fBsolid\fR is equivalent to \fBfill\fR
656 with the darkest fill value (DWB \fBpic\fR had this capability but
657 mentioned it only in a reference opinion).
659 More About Text Placement
661 By default, text is centered at the geometric center of the object it is
662 associated with. The modifier \fBljust\fR causes the left end to be
663 at the specified point (which means that the text lies to the right of
664 the specified place!), The modifier \fBrjust\fP puts the right end at
665 the place. The modifiers \fBabove\fP and \fBbelow\fP center the text
666 one half line space in the given direction.
668 Text attributes can be combined:
671 [line up "ljust text" ljust;]
673 [line up "rjust text" rjust;]
675 [arrow 1 "ljust above" ljust above;]
677 [arrow 1 "rjust below" rjust below;]
679 .CE "1: Text attributes"
681 What actually happens is that n text strings are centered in a box
682 that is \fBtextwid\fP wide by \fBtextht\fP high. Both these variables
683 are initially zero (that is \fBpic\fR's way of not making assumptions
684 about \fI[tg]roff\fP(1)'s default point size).
686 In GNU \fBgpic\fR, objects can have an
689 This will only work when the postprocessor is
691 Any text associated with an object having the
693 attribute will be rotated about the center of the object
694 so that it is aligned in the direction from the start point
695 to the end point of the object.
696 Note that this attribute will have no effect for objects whose start and
697 end points are coincident.
699 More About Direction Changes
701 We've already seen how to change the direction in which objects are
702 composed from rightwards to downwards. Here are some more
703 illustrative examples:
708 "\fBright; box; arrow; circle; arrow; ellipse\fP";
710 [right; box; arrow; circle; arrow; ellipse;]
712 move down 0.3 from last [] .s;
714 "\fBleft; box; arrow; circle; arrow; ellipse\fP"
716 [left; box; arrow; circle; arrow; ellipse;]
718 # move down 0.3 from last [] .sw;
719 # To re-join this illustrations, delete everything from here down to
720 # the next #-comment, and uncomment the move line above
722 .CE "1: Effects of different motion directions (right and left)"
725 # To re-join this illustrations, delete everything down to here, then
726 # comment out the next `down' line.
727 # Don't forget to re-number the figures following!
730 "\fBdown; box; arrow; circle; arrow; ellipse;\fP"
732 box; arrow; circle; arrow; ellipse;
734 move right 2 from last [] .e;
736 up; box; arrow; circle; arrow; ellipse;
738 "\fBup; box; arrow; circle; arrow; ellipse;\fP"
741 .CE "2: Effects of different motion directions (up and down)"
743 Something that may appear surprising happens if you change directions
747 box; arrow; circle; down; arrow; ellipse
749 .CE "3: \fBbox; arrow; circle; down; arrow; ellipse\fP
751 You might have expected that program to yield this:
754 box; arrow; circle; move to last circle .s; down; arrow; ellipse
756 .CE "4: More intuitive?
758 But, in fact, to get Figure \*[SN]3 you have to do this:
766 move to last circle .s;
774 Why is this? Because the exit point for the current direction is
775 already set when you draw the object. The second arrow in Figure
776 \*[SN]2 dropped downwards from the circle's attachment point for an
777 object to be joined to the right.
779 The meaning of the command \fBmove to last circle .s\fP should be obvious.
780 In order to see how it generalizes, we'll need to go into detail on two
781 important topics; locations and object names.
785 The most natural way to name locations in \fBpic\fP is relative to
786 objects. In order to do this, you have to be able you have to be able
787 to name objects. The \fBpic\fP language has rich facilities for this
788 that try to emulate the syntax of English.
790 Naming Objects By Order Of Drawing
792 The simplest (and generally the most useful) way to name an object is
793 with a \fBlast\fP clause. It needs to be followed by an object type
794 name; \fBbox\fP, \fBcircle\fP, \fBellipse\fP, \fBline\fP, \fBarrow\fP,
795 \fBspline\fP or \fB[]\fP (the last type refers to a \fIcomposite
796 object\fP which we'll discuss later). So, for example, the \fBlast
797 circle\fP clause in the program attached to Figure \*[SN]3 refers to the
800 More generally, objects of a given type are implicitly numbered
801 (starting from 1). You can refer to (say) the third ellipse in the
802 current picture with \fB3rd ellipse\fP, or to the first box as \fB1st
803 box\fP, or to the fifth line as \fB5th line\fP.
805 Objects are also numbered backwards by type from the last one of
806 You can say \fB2nd last box\fP to get the second-to-last box, or
807 \fB3rd last ellipse\fP to get the third-to-last box.
809 In places where \fIn\fBth\fP is allowed, \fB`\fIexpr\fB'th\fP is also allowed.
812 is a single token: no space is allowed between the
821 line from `i'th box.nw to `i+1'th box.se
827 Naming Objects With Labels
829 You can also specify an object by referring to a label. A label is a
830 word (which must begin with a capital letter) followed by a colon;
831 you declare it by placing it immediately before the object drawing command.
832 For example, the program
837 A: box "first" "object"
839 B: ellipse "second" "object"
847 declares labels \fBA\fP and \fBB\fP for its first and second objects.
848 Here's what that looks like:
851 A: box "first" "object"
853 B: ellipse "second" "object"
857 .CE "1: Example of label use"
858 The \fBat\fP statement in the fourth line uses the label \fBA\fP (the
859 behavior of \fBat\fP will be explained in the next section). We'll
860 see later on that labels are most useful for referring to block composite
863 Labels are not constants but variables (you can view colon as a sort
864 of assignment). You can say something like \fBA: A + (1,0);\fP
865 and the effect will be to reassign the label \fBA\fR to designate a
866 position one inch to the right of its old value.
870 The location of points can be described in many different ways. All these
871 forms are interchangeable as for as the \fBpic\fP language syntax is
872 concerned; where you can use one, any of the others that would make
873 semantic sense are allowed.
875 The special label \fBHere\fR always refers to the current position.
879 The simplest is absolute coordinates in inches; \fBpic\fP uses a
880 Cartesian system with (0, 0) at the lower left corner of the virtual
881 drawing surface for each picture (that is, X increases to the right
882 and Y increases upwards). An absolute location may always be written in the
883 conventional form as two comma-separated numbers surrounded by
884 parentheses (and this is recommended for clarity). In contexts where
885 it creates no ambiguity, the pair of X and Y coordinates suffices
888 It is a good idea to avoid absolute coordinates, however. They tend
889 to make picture descriptions difficult to understand and modify.
890 Instead, there are quite a number of ways to specify locations
891 relative to \fBpic\fP objects and previous locations.
894 Locations Relative to Objects
896 The symbol \fBhere\fP always refers to the position of the last object
897 drawn or the destination of the last \fBmove\fP.
899 Alone and unqualified, a \fBlast circle\fP or any other way of
900 specifying a closed-object or arc location refers as a position to the
901 geometric center of the object. Unqualified, the name of a line or
902 spline object refers to the position of the object start.
904 Also, \fBpic\fP objects have quite a few named locations
905 associated with them. One of these is the object center, which can be
906 indicated (redundantly) with the suffix \fB.center\fP (or just \fB.c\fP).
907 Thus, \fBlast circle \&.center\fP is equivalent to \fBlast
910 Locations Relative to Closed Objects
912 Every closed object (box, circle, ellipse, or block composite) also
913 has eight compass points associated with it;
916 define dot {circle fill rad 0.02 at $1}
920 dot(ME.c); "\fB .c\fP" at ME .c ljust;
921 dot(ME.n); "\fB.n\fP" at ME .n above
922 dot(ME.ne); "\fB .ne\fP" at ME .ne above
923 dot(ME.e); "\fB .e\fP" at ME .e ljust
924 dot(ME.se); "\fB .se\fP" at ME .se below
925 dot(ME.s); "\fB.s\fP" at ME .s below
926 dot(ME.sw); "\fB.sw \fP" at ME .sw below
927 dot(ME.w); "\fB.w \fP" at ME .w rjust
928 dot(ME.nw); "\fB.nw \fP" at ME .nw above
930 compass(box wid 1.5 ht 1);
931 move right from last [] .e;
932 compass(circle diam 1);
933 move right from last [] .e;
934 compass(ellipse wid 1.5 ht 1);
936 .CE "1: Compass points"
938 these are the locations where eight compass rays from the geometric center
939 would intersect the figure. So when we say \fBlast circle .s\fP we are
940 referring to the south compass point of the last circle drawn. The
941 explanation of Figure 7.3's program is now complete.
943 (In case you dislike compass points, the names \fB.top\fP,
944 \&\fB.bottom\fP, \fB.left\fP and \fB.right\fP are synonyms for \fB.n\fP,
945 \&\fB.s\fP, \fB.e\fP, and \fB.w\fP respectively; they can even be
946 abbreviated to \fB.t\fP, \fB.b\fP, \fB.l\fP and \fB.r\fP).
948 The names \fBcenter\fP, \fBtop\fP, \fBbottom\fP, \fBleft\fP and
949 \fBright\fP can also be used (without the leading dot) in a prefix
950 form marked by \fBof\fP; thus, \fPcenter of last circle\fB and
951 \fBtop of 2nd last ellipse\fP are both valid object references.
953 Arc objects also have compass point; they are the compass points of
956 Locations Relative to Open Objects
958 Every open object (line, arrow, arc, or spline) has three named
959 points; \fB.start\fP, \fB.center\fP, and \fB.end\fP. They can
960 also be used without leading dots in the \fBof\fP prefix form.
961 The center of an arc is the center of its circle, but the center of
962 a line, path, or spline is halfway between its endpoints.
967 dot(ME.c); ".center" rjust at ME.center + (-0.1, 0.1)
968 dot(ME.start); ".start" rjust at ME.start + (-0.1, 0.1)
969 dot(ME.end); ".end" rjust at ME.end + (-0.1, 0.1)
971 move down 0.2 from last [] .s;
973 critical(line up right 1);
974 move right from last [] .e;
975 critical(arc rad 0.5 cw);
976 move right from last [] .e;
977 critical(line right 1 then down .5 left 1 then right 1);
978 move right from last [] .e;
979 critical(spline right 1 then up right then left then left 1);
981 .CE "2: Special points on open objects"
984 Ways of Composing Positions
986 Once you have two positions to work with, there are several ways to
987 combine them to specify new positions.
989 Vector Sums and Displacements
991 Any two positions may be added or subtracted to yield a new position.
992 The result is the conventional vector sum or difference of coordinates.
993 For example, \fBlast box .ne + (0.1, 0)\fP is a valid position. This
994 example illustrates a common use, to define a position slightly offset
995 from a named one (say, for captioning purposes).
997 Interpolation Between Positions
999 A position may be interpolated between any two positions. The syntax
1000 is `\fIfraction\fP \fBof the way between\fP \fIposition1\fP \fBand\fP
1001 \fIposition2\fP.' For example, you can say \fB1/3 of the way between
1002 here and last ellipse .ne\fP. The fraction may be in
1003 numerator/denominator form or may be an ordinary number (values are
1004 \&\fInot\fP restricted to [0,1]). As an alternative to this verbose
1005 syntax, you can say `\fIfraction\fP \fB<\fP\fIposition1\fP \fB,\fP
1006 \fIposition2\fP\fB>\fP.'; thus, the example could also be written
1007 \fB1/3\fP <here, last ellipse>\fP.
1011 P: 1/3 of the way between last arrow .start and last arrow .end;
1012 dot(P); move right 0.1; "P";
1014 .CE "3: \fBP: 1/3 of the way between last arrow .start and last arrow .end\fP"
1016 This facility can be used, for example, to double connections.
1019 A: box "yin"; move; B: box "yang";
1020 arrow right at 1/4 <A.e,A.ne>;
1021 arrow left at 1/4 <B.w,B.sw>;
1023 .CE "4: Doubled arrows"
1025 You can get Figure \n[H1]-4 from the following program:
1032 arrow right at 1/4 <A.e,A.ne>;
1033 arrow left at 1/4 <B.w,B.sw>;
1039 Note the use of the short form for interpolating points.
1041 Projections of Points
1043 Given two positions \fIp\fP and \fIq\fP, the position
1044 \fB(\fP\fIp\fP\fB,\fP \fIq\fP\fB)\fP has the X coordinate of \fIp\fP
1045 and the Y coordinate of \fIq\fP. This can be helpful in placing an
1046 object at one of the corners of the virtual box defined by two other
1050 box invis wid 2 height 1;
1051 dot(last box .ne); "\fB(B,A)\fB is here" ljust at last circle + (0.1, 0.1);
1052 dot(last box .se); "B" ljust at last circle + (0.1, -0.1)
1053 dot(last box .sw); "\fB(A,B)\fB is here" rjust at last circle + (-0.1, -0.1);
1054 dot(last box .nw); "A" ljust at last circle + (-0.1, 0.1)
1056 .CE "5: Using (\fIx\fP, \fIy\fP) composition"
1060 There are four ways to use locations; \fBat\fP, \fBfrom\fP, \fBto\fP,
1061 and \fBwith\fP. All three are object modifiers; that is, you use them
1062 as suffixes to a drawing command.
1064 The \fBat\fP modifier says to draw a closed object or arc with its
1065 center at the following location, or to draw a line/spline/arrow
1066 starting at the following location.
1068 The \fBto\fP modifier can be used alone to specify a move destination.
1069 The \fBfrom\fP modifier can be used alone in the same way as \fBat\fP.
1071 The \fBfrom\fP and \fBto\fP modifiers can be used with a \fBline\fR or
1072 \fBarc\fR command to specify start and end points of the object. In
1073 conjunction with named locations, this offers a very flexible
1074 mechanism for connecting objects. For example, the following program
1082 arc cw from 1/3 of the way \\
1083 between last box .n and last box .ne to last ellipse .n;
1095 arc cw from 1/3 of the way between last box .n and last box .ne to last ellipse .n;
1097 .CE "6: A tricky connection specified with English-like syntax"
1099 The \fBwith\fP modifier allows you to identify a named attachment
1100 point of an object with another point. This is very useful for connecting
1101 objects in a natural way. For an example, consider these two programs:
1105 box wid 0.5 ht 0.5; box wid 0.75 ht 0.75;
1106 move down from last box .s 0.1;
1107 "\fBbox wid 0.5 ht 0.5; box wid 0.75 ht 0.75\fP"
1109 move from last [].e 2
1111 box wid 0.5 ht 0.5; box wid 0.75 ht 0.75 with .sw at last box .se;
1112 move down from last box .s 0.1;
1113 "\fBbox wid 0.5 ht 0.5; box wid 0.75 ht 0.75 with .sw at last box .se;\fP"
1116 .CE "7: Using the \fBwith\fP modifier for attachments"
1120 When drawing lines between circles that don't intersect them at a
1121 compass point, it is useful to be able to shorten a line by the radius
1122 of the circle at either or both ends. Consider the following program:
1127 circle "y" at 1st circle - (0.4, 0.6)
1128 circle "z" at 1st circle + (0.4, -0.6)
1129 arrow from 1st circle to 2nd circle chop
1130 arrow from 2nd circle to 3rd circle chop
1131 arrow from 3rd circle to 1st circle chop
1136 It yields the following:
1140 circle "y" at 1st circle - (0.4, 0.6)
1141 circle "z" at 1st circle + (0.4, -0.6)
1142 arrow from 1st circle to 2nd circle chop
1143 arrow from 2nd circle to 3rd circle chop
1144 arrow from 3rd circle to 1st circle chop
1146 .CE "8: The \fBchop\fR modifier"
1148 Notice that the \fBchop\fR attribute moves arrowheads rather than
1149 stepping on them. By default, the \fBchop\fR modifier shortens both
1150 ends of the line by \fBcirclerad\fR. By suffixing it with a number
1151 you can change the amount of chopping.
1153 If you say \fBline ... chop \fIr1\fP chop \fIr2\fP\fR with \fIr1\fP
1154 and \fIr2\fP both numbers, you can vary the amount of chopping at both
1155 ends. You can use this in combination with trigonometric functions
1156 to write code that will deal with more complex intersections.
1160 There are two different ways to group objects in \fBpic\fP; \fIbrace
1161 grouping\fP and \fIblock composites\fP.
1165 The simpler method is simply to group a set of objects within curly
1166 bracket or brace characters. On exit from this grouping, the current
1167 position and direction are restored to their value when the opening
1168 brace was encountered.
1172 A block composite object is created a series of commands enclosed by
1173 square brackets. The composite can be treated for most purposes like
1174 a single closed object, with the size and shape of its bounding box.
1175 Here is an example. The program fragment
1181 \& line up 1 at last circle .n;
1182 \& line down 1 at last circle .s;
1183 \& line right 1 at last circle .e;
1184 \& line left 1 at last circle .w;
1185 \& box dashed with .nw at last circle .se + (0.2, -0.2);
1186 \& Caption: center of last box;
1192 yields the block in figure \n[H1]-1, which we show both with and
1193 without its attachment points. The block's location becomes the
1199 line up 1 at last circle .n;
1200 line down 1 at last circle .s;
1201 line right 1 at last circle .e;
1202 line left 1 at last circle .w;
1203 box dashed with .nw at last circle .se + (0.2, -0.2);
1204 Caption: center of last box;
1208 compass([junction()]);
1210 .CE "1: A sample composite object"
1212 To refer to one of the composite's attachment points, you can say
1213 (for example) \fBA .s\fP. For purposes of object naming, composites
1214 are a class. You could write \fBlast [] .s\fP as an equivalent
1215 refrence, usable anywhere a location is needed. This construction is
1216 very important for putting together large, multi-part diagrams.
1218 Blocks are also a variable-scoping mechanism, like a \fIgroff\fP(1)
1219 environment. All variable assignments done inside a block are undone
1220 at the end of it. To get at values within a block, write a name of
1221 the block followed by a dot, followed by the variable or label you
1222 want. For example, we could refer the the center of the box in the
1223 above composite as \fBlast [] .Caption\fP or \fBA.Caption\fP.
1225 This kind of reference to a label can be used in any way any other
1226 location can be. For example, if we added \fB"Hi!" at A.Caption\fP
1227 the result would look like this:
1233 .CE "2: Adding a caption using interior labeling"
1235 You can also use interior labels in either part of a \fBwith\fR
1236 modifier. This means that the example composite could be placed
1237 relative to its caption box by a command containing \fBwith A.Caption
1240 Blocks may be nested. This means you can use block attachment points
1241 to build up complex diagrams hierarchically, from the inside out.
1242 Note that \fBlast\fP and the other sequential naming mechanisms
1243 don't look inside blocks, so if you have a program that looks
1249 P: [box "foo"; ellipse "bar"];
1251 [box "baz"; ellipse "quxx"]
1254 arrow from 2nd last [];
1260 the arrow in the last line will be attached to object \fBP\fP, not
1263 In DWB \fBpic\fP, only references one level deep into enclosed blocks
1264 were permitted. GNU \fBgpic\fP removes this restriction.
1266 The combination of block variable scoping, assignability of labels and
1267 the macro facility that we'll describe later on can be used to
1268 simulate functions with local variables (just wrap the macro body in
1273 There are a number of global style variables in \fBpic\fR that can be used to
1274 change its overall behavior. We've mentioned several of them in
1275 previous sections. They're all described here. For each variable,
1276 the default is given.
1278 tab(@), linesize(2);
1282 Style Variable@Default@What It Does
1287 boxht@0.5@Default height of a box
1288 boxwid@0.75@Default height of a box
1289 lineht@0.5@Default length of vertical line
1290 linewid@0.75@Default length of horizontal line
1291 arcrad @0.25@Default radius of an arc
1292 circlerad@0.25@Default radius of a circle
1293 ellipseht@0.5@Default height of an ellipse
1294 ellipsewid@0.75@Default width of an ellipse
1295 moveht@0.5@Default length of vertical move
1296 movewid@0.75@Default length of horizontal move
1297 textht@0@Default height of box enclosing a text object
1298 textwid@0@Default width of box enclosing a text object
1299 arrowht@0.1@Length of arrowhead along shaft
1300 arrowwid@0.05@Width of rear of arrowhead
1301 arrowhead@1@Enable/disable arrowhead filling
1302 dashwid@0.05@Interval for dashed lines
1303 maxpswid@11@Maximum width of picture
1304 maxpsht@8.5@Maximum height of picture
1305 scale@1@Unit scale factor
1306 fillval@0.5@Default fill value
1310 Any of these variables can be set with a simple assignment statement.
1314 [boxht=1; boxwid=0.3; movewid=0.2; box; move; box; move; box; move; box;]
1316 .CE "1: \fBboxht=1; boxwid=0.3; movewid=0.2; box; move; box; move; box; move; box;\fP"
1318 In GNU \fBpic\fR, setting the \fBscale\fR variable re-scales all
1319 size-related state variables so that their values remain equivalent in
1322 The command \fBreset\fP resets all style variables to their defaults.
1323 You can give it a comma-separated list of variable names as arguments,
1324 in which case it resets only those.
1326 State variables retain their values across pictures until reset.
1328 Expressions, Variables, and Assignment
1330 A number is a valid expression, of course (all numbers are stored
1331 internally as floating-point). Decimal-point notation is acceptable;
1332 in GNU \fBgpic\fR, scientific notation in C's `e' format (like
1333 \f(CW5e-2\fP) is accepted.
1335 Anywhere a number is expected, the language will also accept a
1336 variable. Variables may be the built-in style variable described in
1337 the last section, or new variables created by assignment.
1339 DWB \fBpic\fP supports only the ordinary assignment via =, defines the
1340 variable in the current block if it is not already defined there, and
1341 then changes the value in the current block.
1342 GNU \fBgpic\fP supports an alternate form of assignment using :=. The
1344 (right side) must already be defined,
1347 will be changed only in the innermost block in which it is defined.
1349 You can use the height, width, radius, and x and y coordinates of any
1350 object or corner in expressions If \fBA\fP is an object label or name,
1351 all the following are valid:
1355 A.x # x coordinate of the center of A
1356 A.ne.y # y coordinate of the northeast corner of A
1357 A.wid # the width of A
1358 A.ht # and its height
1359 2nd last circle.rad # the radius of the 2nd last circle
1363 Note the second expression, showing how to extract a corner coordinate.
1365 Basic arithmetic resembling those of C operators are available; +, *,
1366 -, /, and %. So is ^ for exponentiation. Grouping is permitted in
1367 the usual way using parentheses. GNU \fBgpic\fP allows logical
1368 operators to appear in expressions; ! (logical negation, not
1369 factorial), &&, ||, ==, !=, >=, <=, <, >.
1371 Various built-in functions are supported: \fBsin(\fIx\fP)\fP,
1372 \fBcos(\fIx\fP)\fP, \fBlog(\fIx\fP)\fP, \fBexp(\fIx\fP)\fP,
1373 \fBsqrt(\fIx\fP)\fP, \fBmax(\fIx\fP,\fIy\fP)\fP,
1374 \fBatan2(\fIx\fP,\fIy\fP)\fP, \fBmin(\fIx\fP,\fIy\fP)\fP,
1375 \fBint(\fIx\fP\fB)\fP, and \fBrand()\fP, Both \fBexp\fP and \fBlog\fP are
1376 base 10; \fBint\fP does integer truncation; and \fBrand()\fP returns a
1377 random number in [0-1).
1379 GNU \fBgpic\fP also documents a one-argument form or rand,
1380 \fBrand(\fIx\fP\fB)\fP, which returns a random number between 1 and
1381 \fIx\fP, but this is deprecated and may be removed in a future
1384 The function \fBsprintf()\fP behaves like a C \fIsprintf\fP(3) that
1385 only takes %, %e, %f, and %g format strings.
1389 You can define macros in \fBpic\fP. This is useful for diagrams with
1390 repetitive parts. In conjunction with the scope rules for block
1391 composites, it effectively gives you the ability to write functions.
1396 \fBdefine\fP \fIname\fP \fB{\fP \fIreplacement text \fB}\fP
1400 This defines \fIname\fR as a macro to be replaced by the replacement
1401 text (not including the braces). The macro may be called as
1404 \fIname\fB(\fIarg1, arg2, ... argn\fB)\fR
1408 The arguments (if any) will be substituted for tokens $1, $2 ... $n
1409 appearing in the replacement text.
1411 As an example of macro use, consider the following:
1416 \&# Plot a single jumper in a $1 by $2 box, $3 is the on-off state
1418 \& shrinkfactor = 0.8;
1419 \& Outer: box invis wid 0.5 ht 1;
1421 \& # Count on end ] to reset these
1422 \& boxwid = Outer.wid * shrinkfactor / 2;
1423 \& boxht = Outer.ht * shrinkfactor / 2;
1425 \& box fill (!$1) with .s at center of Outer;
1426 \& box fill ($1) with .n at center of Outer;
1429 \&# Plot a block of six jumpers
1430 \&define jumperblock {
1438 \& jwidth = last [].Outer.wid;
1439 \& jheight = last [].Outer.ht;
1441 \& box with .nw at 6th last [].nw wid 6*jwidth ht jheight;
1443 \& # Use {} to avoid changing position from last box draw.
1444 \& # This is necessary so move in any direction will work as expected
1445 \& {"Jumpers in state $1$2$2$3$4$5$6" at last box .s + (0, -0.2);}
1448 \&# Sample macro invocations
1449 \&jumperblock(1,1,0,0,1,0);
1451 \&jumperblock(1,0,1,0,1,1);
1456 It yields the following:
1459 # Plot a single jumper in a $1 by $2 box, $3 is the on-off state
1462 Outer: box invis wid 0.5 ht 1;
1464 # Count on end ] to reset these
1465 boxwid = Outer.wid * shrinkfactor / 2;
1466 boxht = Outer.ht * shrinkfactor / 2;
1468 box fill (!$1) with .s at center of Outer;
1469 box fill ($1) with .n at center of Outer;
1472 # Plot a block of six jumpers
1473 define jumperblock {
1481 jwidth = last [].Outer.wid;
1482 jheight = last [].Outer.ht;
1484 box with .nw at 6th last [].nw wid 6*jwidth ht jheight;
1486 # Use {} to avoid changing position from last box draw.
1487 # This is necessary so move in any direction will work as expected
1488 {"Jumpers in state $1$2$2$3$4$5$6" at last box .s + (0, -0.2);}
1491 # Sample macro invocations
1492 jumperblock(1,1,0,0,1,0);
1494 jumperblock(1,0,1,0,1,1);
1496 .CE "1: Sample use of a macro"
1498 This macro example illustrates how you can combine [], brace grouping,
1499 and variable assignment to write true functions.
1501 One detail the example above does not illustrate is the fact that
1502 macro argument parsing is not token-oriented. If you call \fBjumper(
1503 1 )\fP, the value of $1 will be \fB" 1 "\fP. You could even call
1504 \fBjumper(big string)\fP to give $1 the value \fB"big string"\fP.
1506 If you want to pass in a coordinate pair, you can avoid getting
1507 tripped up by the comma by wrapping the pair in parentheses.
1509 Macros persist through pictures. To undefine a mcro, say \fBundef\fP
1510 \fIname\fR; for example,
1512 \f(CWundef jumper\fP
1513 \f(CWundef jumperblock\fP
1516 would undefine the two macros in the jumper block example.
1518 Import/Export Commands
1520 Commands that import or export data between \fBpic\fR and its
1521 environment are described here.
1523 File and Table Insertion
1527 \f(CWcopy\fP \fIfilename\fR
1529 inserts the contents of \fIfilename\fR in the \fBpic\fP input stream.
1530 Any .PS/.PE pair in the file will be ignored. This, you can use this
1531 to include pre-generated images.
1533 A variant of this statement replicates the \fBcopy thru\fP feature of
1534 \fIgrap\fP(1). If you say
1536 \f(CWcopy\fP \fIfilename\fR \f(CWthru\fP \fImacro\fP
1539 calls the \fImacro\fP (which may be either a name or replacement text)
1540 on the arguments obtained by breaking each line of the file into
1541 blank-separated fields. The macro may have up to 9 arguments. The
1542 replacement text may be delimited by braces or by a pair of instances
1543 of any character not appearing in the rest of the text.
1547 \f(CWcopy thru\fP \fImacro\fP
1550 omitting the filename, lines to be parsed are taken from the input
1551 source up to the next .PE.
1553 In either of the \fBcopy\fP commands, GNU \fBgpic\fP permits a
1554 trailing `\fBuntil\fP \fIword\fP' clause to be added which terminates
1555 the copy when the first word matches the argument (the default
1556 behavior is therefore equivalent to \fBuntil .PE\fP,
1558 Accordingly, the command
1564 copy thru % circle at ($1,$2) % until "END"
1592 The command \fBprint\fR accepts any number of comma-separated
1593 arguments, concatenates their output forms, and writes the result to
1594 standard error. Each argument must be an expression, a position, or
1597 Escape to Post-Processor
1601 \fBcommand\fR \fIarg\fR\|.\|.\|.
1604 \fBpic\fP concatenates the arguments and pass them through as a line
1605 to troff or \*(tx. Each
1607 must be an expression, a position, or text.
1608 This has a similar effect to a line beginning with
1612 but allows the values of variables to be passed through.
1614 Executing Shell Commands
1618 \f(CWsh { \fIanything...\fP }\fP
1621 macroexpands the text in braces, then executes it as a shell command.
1622 This could be used to generate images or data tables for later
1623 inclusion. The delimiters shown as {} here may also be two copies of
1624 any one character not present in the shell command text. In either
1625 case, the body may contain balanced {} pairs. Strings in the body
1626 may contain balanced or unbalanced braces in any case.
1628 Control-flow constructs
1630 The \fBpic\fP language provides conditionals and looping. For
1636 for i = 0 to 2 * pi by 0.1 do {
1638 "." at (i/2, sin(i)/2);
1639 ":" at (i/2, cos(i)/2);
1649 for i = 0 to 2 * pi by 0.1 do {
1651 "." at (i/2, sin(i)/2);
1652 ":" at (i/2, cos(i)/2);
1655 .CE "1: Plotting with a \fBfor\fP loop"
1657 The syntax of the \fBfor\fP statement is:
1659 \fBfor\fR \fIvariable\fR \fB=\fR \fIexpr1\fR \fBto\fR \fIexpr2\fR \
1660 [\fBby\fR [\fB*\fR]\fIexpr3\fR] \fBdo\fR \fIX\fR \fIbody\fR \fIX\fR
1662 The semantics are as follows: Set
1667 is less than or equal to
1677 is not given, increment
1686 will instead be multiplied by
1689 can be any character not occurring in
1690 \fIbody\fR; or the two Xs may be paired braces (as in the \fBsh\fR command).
1692 The syntax of the \fBif\fP statement is as follows:
1694 \fBif\fR \fIexpr\fR \fBthen\fR \fIX\fR \fIif-true\fR \fIX\fR \
1695 [\fBelse\fR \fIY\fR \fIif-false\fR \fIY\fR]
1697 Its semantics are as follows: Evaluate
1699 if it is non-zero then do
1704 can be any character not occurring in
1707 can be any character not occurring in
1710 Eithe or both of the X or Y pairs may instead be balanced pairs of
1711 braces ({ and }) as in the \fBsh\fR command. In either case, the
1712 \fIif-true\fR may contain balanced pairs of braces. None of these
1713 delimiters will be seen inside strings.
1715 All the usual relational operators my be used in conditional expressions;
1716 ! (logical negation, not factorial), &&, ||, ==, !=, >=, <=, <, >.
1718 String comparison is also supported using == and !=. String
1719 comparisons may need to be parenthesized to avoid syntactic
1722 Interface To [gt]roff
1724 The output of \fBpic\fP is \fB[gt]roff\fP drawing commands. The GNU
1725 \fIgpic\fP(1) command warns that it relies on drawing extensions
1726 present in \fIgroff\fP(1) that are not present in \fItroff\fP(1).
1730 The DWB \fIpic\fP(1) program will accept one or two arguments to
1731 \&\fB.PS\fP, which is interpreted as a width and height in inches to
1732 which the results of \fIpic\fP(1) should be scaled (width and height
1733 scale independently). If there is only one argument, it is
1734 interpreted as a width to scale the picture to, and height will be
1735 scaled by the same proportion.
1737 GNU \fBgpic\fP is less general; it will accept a single width to scale
1738 to, or a zero width and a maximum height to scale to. With
1739 two nonzero arguments, it will scale to the maximum height.
1741 How Scaling is Handled
1743 When \fBpic\fP processes a picture description on input, it passes .PS
1744 and .PE through to the postprocessor. The .PS gets decorated with two
1745 numeric arguments which are the X and Y dimensions of the picture in
1746 inches. The post-processor can use these to reserve space for the
1747 picture and center it.
1749 The \fBmgs\fP macros, for example, include the following definitions:
1756 \&.ie \\n[.$]<2 .@error bad arguments to PS (not preprocessed with pic?)
1758 \&. ds@need (u;\\$1)+1v
1759 \&. in +(u;\\n[.l]-\\n[.i]-\\$2/2>?0)
1770 Equivalent definition will be supplied by GNU \fIpic\fP(1) if you use
1771 the -mpic option; this should make it usable with macro pages other
1774 if .PF is used instead of .PE, the \fBtroff\fP position is restored to
1775 what it was at the picture start (Kernighan notes that the F stands
1780 \&\fB.PS <\fP\fIfile\fP
1783 causes the contents of \fIfile\fP to replace the .PS line. This
1784 feature is deprecated; use \fBcopy file\fR instead).
1786 By default, input lines that begin with a period are passed to the
1787 postprocessor, embedded at the corresponding point in the output.
1788 Messing with horizontal or vertical spacing is an obvious recipe for
1789 bugs, but point size and font changes will usually be safe.
1791 Point sizes and font changes are also safe within text strings, as
1792 long as they are undone before the end of string.
1794 The state of \fB[gt]roff\fP's fill mode is preserved across pictures.
1796 The Kernighan paper notes that there is a subtle problem with
1797 complicated equations inside \fBpic\fR pictures; they come out wrong if
1798 \fIeqn\fP(1) has to leave extra vertical space for the equation.
1799 If your equation involves more than subscripts and superscripts, you
1800 must add to the beginning of each equation the extra information
1801 \f(CWspace 0\fP. He gives the following example:
1806 box "$space 0 {H( omega )} over {1 - H( omega )}$"
1817 box "@space 0 {H( omega )} over {1 - H( omega )}@"
1820 .CE "1: Equations within pictures"
1825 \*(tx mode is enabled by the
1828 In \*(tx mode, pic will define a vbox called
1831 You must yourself print that vbox using, for example, the command
1835 \ecenterline{\ebox\egraph}
1838 Actually, since the vbox has a height of zero this will produce
1839 slightly more vertical space above the picture than below it;
1843 \ecenterline{\eraise 1em\ebox\egraph}
1848 You must use a \*(tx driver that supports the
1850 specials, version 2.
1852 Lines beginning with
1854 are passed through transparently; a
1856 is added to the end of the line to avoid unwanted spaces.
1857 You can safely use this feature to change fonts or to
1858 change the value of \fI\ebaselineskip\fR.
1859 Anything else may well produce undesirable results; use at your own risk.
1860 Lines beginning with a period are not given any special treatment.
1862 The \(*tx mode of \fIpic\fP(1) will \fInot\fP translate \fBtroff\fP
1863 font and size changes included in text strings!
1867 GNU \fIgpic\fP(1) has a command
1869 \fBplot\fR \fIexpr\fR [\fB"\fItext\fB"\fR]
1871 This is a text object which is constructed by using
1873 as a format string for sprintf
1878 is omitted a format string of "%g" is used.
1879 Attributes can be specified in the same way as for a normal text
1881 Be very careful that you specify an appropriate format string;
1882 \fBpic\fP does only very limited checking of the string.
1883 This is deprecated in favour of
1886 Some Larger Examples
1888 Here are a few larger examples, with complete source code.
1890 One of our earlier examples is generated in an instructive way using a
1896 \&# Draw a demonstration up left arrow with grid box overlay
1900 \& {arrow up left $1;}
1901 \& box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
1902 \& for i = 2 to ($1 / 0.5) do
1904 \& box wid 0.5 ht 0.5 dotted with .sw at last box .se;
1906 \& move down from last arrow .center;
1908 \& if ( $1 == boxht ) then { "\\fBline up left\\fP" } else { sprintf("\\fBarrow up left %g\\fP", $1) };
1911 \& move right from last [] .e;
1924 # Draw a demonstration up left arrow with grid box overlay
1929 box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
1930 for i = 2 to ($1 / 0.5) do
1932 box wid 0.5 ht 0.5 dotted with .sw at last box .se;
1934 move down from last arrow .center;
1936 if ( $1 == boxht ) then { "\fBline up left\fP" } else { sprintf("\fBarrow up left %g\fP", $1) };
1939 move right from last [] .e;
1947 .CE "1: Diagonal arrows (dotted boxes show the implied 0.5-inch grid)"
1949 Here's an example concocted to demonstrate layout of a large,
1950 multiple-part pattern:
1955 \&define filter {box ht 0.25 rad 0.125}
1959 \& box "\\fBms\\fR" "sources";
1961 \& box "\\fBHTML\\fR" "sources";
1963 \& box "\\fBlinuxdoc-sgml\\fP" "sources" wid 1.5;
1965 \& box "\\fBTexinfo\\fP" "sources";
1967 \& line down from 1st box .s lineht;
1969 \& line down from 2nd box .s; filter "\\fBhtml2ms";
1971 \& line down from 3rd box .s; filter "\\fBformat\\fP";
1973 \& line down from 4th box .s; filter "\\fBtexi2roff\\fP";
1976 \&move down 1 from last [] .s;
1977 \&Anchor: box wid 1 ht 0.75 "\\fBms\\fR" "intermediate" "form";
1978 \&arrow from Top.A.end to Anchor.nw;
1979 \&arrow from Top.B.end to 1/3 of the way between Anchor.nw and Anchor.ne;
1980 \&arrow from Top.C.end to 2/3 of the way between Anchor.nw and Anchor.ne;
1981 \&arrow from Top.D.end to Anchor.ne
1983 \& # PostScript column
1984 \& move to Anchor .sw;
1985 \& line down left then down ->;
1986 \& filter "\\fBpic\\fP";
1988 \& filter "\\fBeqn\\fP";
1990 \& filter "\\fBtbl\\fP";
1992 \& filter "\\fBgroff\\fP";
1994 \& box "PostScript";
1997 \& move to Anchor .se;
1998 \& line down right then down ->;
1999 \& A: filter dotted "\\fBpic2img\\fP";
2001 \& B: filter dotted "\\fBeqn2html\\fP";
2003 \& C: filter dotted "\\fBtbl2html\\fP";
2005 \& filter "\\fBms2html\\fP";
2009 \& # Nonexistence caption
2010 \& box dashed wid 1 at B + (2, 0) "These tools" "don't yet exist";
2011 \& line chop 0 chop 0.1 dashed from last box .nw to A.e ->;
2012 \& line chop 0 chop 0.1 dashed from last box .w to B.e ->;
2013 \& line chop 0 chop 0.1 dashed from last box .sw to C.e ->;
2021 define filter {box ht 0.25 rad 0.125}
2025 box "\fBms\fR" "sources";
2027 box "\fBHTML\fR" "sources";
2029 box "\fBlinuxdoc-sgml\fP" "sources" wid 1.5;
2031 box "\fBTexinfo\fP" "sources";
2033 line down from 1st box .s lineht;
2035 line down from 2nd box .s; filter "\fBhtml2ms";
2037 line down from 3rd box .s; filter "\fBformat\fP";
2039 line down from 4th box .s; filter "\fBtexi2roff\fP";
2042 move down 1 from last [] .s;
2043 Anchor: box wid 1 ht 0.75 "\fBms\fR" "intermediate" "form";
2044 arrow from Top.A.end to Anchor.nw;
2045 arrow from Top.B.end to 1/3 of the way between Anchor.nw and Anchor.ne;
2046 arrow from Top.C.end to 2/3 of the way between Anchor.nw and Anchor.ne;
2047 arrow from Top.D.end to Anchor.ne
2051 line down left then down ->;
2058 filter "\fBgroff\fP";
2064 line down right then down ->;
2065 A: filter dotted "\fBpic2img\fP";
2067 B: filter dotted "\fBeqn2html\fP";
2069 C: filter dotted "\fBtbl2html\fP";
2071 filter "\fBms2html\fP";
2075 # Nonexistence caption
2076 box dashed wid 1 at B + (2, 0) "These tools" "don't yet exist";
2077 line chop 0 chop 0.1 dashed from last box .nw to A.e ->;
2078 line chop 0 chop 0.1 dashed from last box .w to B.e ->;
2079 line chop 0 chop 0.1 dashed from last box .sw to C.e ->;
2082 .CE "2: Hypothetical production flow for dual-mode publishing"
2088 This is an annotated grammar of PIC.
2092 In general, \fBpic\fP is a free-format, token-oriented language that
2093 ignores whitespace outside strings. But certain lines and contructs
2094 are specially interpreted at the lexical level:
2096 A comment begins with # and continues to \en (comments may also follow
2097 text in a line). A line beginning with a period or backslash may be
2098 interpreted as text to be passed through to the post-processor,
2099 depending on command-line options. An end-of-line backslash is
2100 interpreted as a request to continue the line; the backslash and
2101 following newline are ignored.
2103 Here are the grammar terminals:
2105 A decimal numeric constant. May contain a decimal point or be
2106 expressed in scientific notation in the style of \fIprintf\fP(3)'s %e
2107 escape. (All variables are represented internally in floating-point.)
2109 Any ASCII characters surrounded by a pair of double quotes. May
2110 contain a double quote if preceded by a backslash.
2112 A lower-case alphabetic character, followed by any number of
2113 alphanumerics. (Values of variables are preserved across pictures.)
2115 An upper-case alphabetic character, followed by any number of
2120 Tokens not enclosed in <> are literals, except:
2124 three dots is a suffix meaning \&`replace with 0 or more repetitions
2125 of the preceding element.
2127 enclosure in square brackets has its usual meaning of `this clause is
2130 Square-bracket-enclosed portions within tokens are optional. Thus,
2131 \fBh[eigh]t\fR matches either `height' or `ht'.
2133 If one of these special tokens has to be referred to literally, it is
2134 surrounded with single quotes.
2136 The top-level \fBpic\fP object is a picture.
2139 <picture> ::= .PS [width [height]]\en
2145 The \fIwidth\fP and \fIheight\fP arguments, if present, cause
2146 \fBpic\fR to attempt to scale the picture to the given dimensions in
2147 inches. In no case, however, will the X and Y dimensions of the
2148 picture exceed the values of the style variables \fBmaxpswid\fP,
2149 \fBmaxpsheight\fP (which default to the normal 8.5 by 11 page size)\fP.
2151 If the ending .PS is replaced by .PF, the page vertical position is
2152 restored to its value at the time .PS was encountered. Another
2153 alternate form of invocation is \f(CW.PS <\fIfilename\fR, which
2154 replaces the .PS line with a file to be interpreted by \fBpic\fR (but
2155 this feature is deprecated).
2157 The .PS, .PE, and .PF macros to perform centering and scaling are
2158 normally supplied by the post-processor.
2162 <statement> ::= <command> ;
2165 <command> ::= <primitive> <modifier>...
2167 <label> : <position>
2171 '[' <command> ... ']'
2172 for <var> = <expr> to <expr> [by <expr>] do { <command> ... }
2173 if <expr> then { <command> ... } [else { <command> ... }]
2174 copy <filename> [until <word>]
2175 copy <filename> thru <macroname> [until <word>]
2178 reset [ <variable> ... ]
2182 The current position and direction are saved on entry to a { } and
2183 restored on exit from it.
2185 Drawn objects within [ ] are treated as a single composite object with
2186 a rectangular shape (that of the bounding box of all the elements).
2187 Variable and label assignments within a block are local to the block.
2188 Current direction of motion is restored to the value at start of block
2189 upon exit. Position is \fInot\fR restored (unlike { }) instead, the
2190 current position becomes the exit position for the current direction
2191 on the block's bounding box.
2194 <primitive> ::= box \fR# Closed object -- rectangle\fP
2195 circle \fR# Closed object -- circle\fP
2196 ellipse \fR# Closed object -- ellipse\fP
2197 arc \fR# Open object -- quarter-circle\fP
2198 line \fR# Open object -- line\fP
2199 arrow \fR# Open object -- line with arrowhead\fP
2200 spline \fR# Open object -- spline curve\fP
2202 <text> <text> ... \fR# Text within invisible box\fP
2204 <attribute> ::= h[eigh]t <expr> \fR# Set height of closed figure \fP
2205 wid[th] <expr> \fR# Set width of closed figure \fP
2206 rad[ius] <expr> \fR# Set radius of circle/arc \fP
2207 diam[eter] <expr> \fR# Set diameter of circle/arc \fP
2208 up [ <expr> ] \fR# Move up \fP
2209 down [ <expr> ] \fR# Move down \fP
2210 left [ <expr> ] \fR# Move left \fP
2211 right [ <expr> ] \fR# Move right \fP
2212 from <position> \fR# Set from position of open figure\fP
2213 to <position> \fR# Set to position of open figure\fP
2214 at <position> \fR# Set center of open figure\fP
2215 with <corner> \fR# Fix corner at specified location\fP
2216 by <expr> <expr> \fR# Set object's attachment point\fP
2217 then \fR# Sequential segment composition\fP
2218 dotted [ <expr> ] \fR# Set dotted line style\fP
2219 dashed [ <expr> ] \fR# Set dashed line style\fP
2220 chop [ <expr> ] \fR# Chop end(s) of segment\fP
2221 -> \fR# Decorate with "to" arrow\fP
2222 <- \fR# Decorate with "from" arrow\fP
2223 <-> \fR# Decorate with both arrows\fP
2224 invis \fR# Make primitive invisible\fP
2225 solid \fR# Make closed figure solid\fP
2226 fill <expr> \fR# Set fill density for figure\fP
2227 same \fR# Copy size of previous object\fP
2228 <text> <text> ... \fR# Text within object\fP
2229 <expr> \fR# Motion in the current direction\fR
2233 Missing attributes are supplied from defaults; inappropriate ones are
2234 silently ignored. For lines, splines, and arcs, height and width
2235 refer to arrowhead size.
2237 The \f(CWat\fP primitive sets the center of the current object. The
2238 \f(CWwith\fP attribute fixes the specified feature of the given object
2239 to a specified location.
2241 The \f(CWby\fR primitive is not documented in the tutorial portion of
2242 the Kernighan paper, and should probably be considered unreliable.
2244 The primitive \fBarrow\fR is a synonym for \fBline ->\fR.
2247 <text> ::= <string> [ <placement> ... ]
2248 sprintf("format", <expr> ...) [ <placement> ... ]
2250 <placement> ::= center | ljust | rjust | above | below
2254 Text is normally an attribute of some object, in which case successive
2255 strings are vertically stacked and centered on the object's center by
2256 default. Standalone text is treated as though placed in an invisible
2259 A text item consists of a string or sprintf-expression, optionally
2260 followed by positioning information. Text or format strings may
2261 contain {gtn}roff font changes, size changes, and local motions,
2262 provided those changes are undone before the end of the current item.
2264 A position is an (x, y) coordinate pair. There are lots of different
2265 ways to specify positions:
2268 <position> ::= <expr> , <expr>
2269 <place> {+-} <expr> , <expr>
2270 <place> {+-} ( <expr> , <expr> )
2271 ( <position> , <position> )
2272 <expr> [of the way] between <position> and <position>
2273 <expr> '<' <position> , <position> '>'
2276 <place> ::= <label> [ <dot-corner> ]
2278 [0|1|2|3|4|5|6|7|8|9]th [last] <primitive> <dot-corner>
2279 <expr>'th [last]<primitive> <dot-corner>
2280 <corner> of [0|1|2|3|4|5|6|7|8|9]th [last] <primitive>
2281 <corner> of <expr>'th [last] <primitive>
2284 <dot-corner> ::= .n | .e | .w | .s | .ne | .nw | .se | .sw | .c | .start | .end
2286 <corner> ::= top | bot | left | right | start | end
2290 As Kernighan notes, "since barbarisms like \fB1th\fP and \fB3th\fP are
2291 barbaric, synonyms like \fB1st\fP and \fB3rd\fP are accepted as well".
2292 Objects of a given type are numbered from 1 upwards in order of
2293 declaration; the \fBlast\fP modifier counts backwards.
2295 The "'th" form (which allows you to select a previous object with an
2296 expression, as opposed to a numeric literal) is bnot documented in DWB
2299 The following style variables control output:
2301 tab(@), linesize(2);
2305 Style Variable@Default@What It Does
2310 boxht@0.5@Default height of a box
2311 boxwid@0.75@Default height of a box
2312 lineht@0.5@Default length of vertical line
2313 linewid@0.75@Default length of horizontal line
2314 arcrad @0.25@Default radius of an arc
2315 circlerad@0.25@Default radius of a circle
2316 ellipseht@0.5@Default height of an ellipse
2317 ellipsewid@0.75@Default width of an ellipse
2318 moveht@0.5@Default length of vertical move
2319 movewid@0.75@Default length of horizontal move
2320 textht@0@Default height of box enclosing a text object
2321 textwid@0@Default width of box enclosing a text object
2322 arrowht@0.1@Length of arrowhead along shaft
2323 arrowwid@0.05@Width of rear of arrowhead
2324 arrowhead@1@Enable/disable arrowhead filling
2325 dashwid@0.05@Interval for dashed lines
2326 maxpswid@11@Maximum width of picture
2327 maxpsht@8.5@Maximum height of picture
2328 scale@1@Unit scale factor
2329 fillval@0.5@Default fill value
2333 Any of these can be set by assignment, or reset using the \fBreset\fP
2334 statement. Style variables assigned within [] blocks are restored to
2335 their beginning-of-block value on exit; top-level assignments persist
2336 across pictures. Dimensions are divided by \fBscale\fR on output.
2338 All \fBpic\fP expressions are evaluated in floating point; units
2339 default to inches. Expressions have the following simple grammar,
2340 with semantics very similar to C expressions:
2343 <expr> ::= <expr> <op> <expr>
2359 max(<expr>, <expr>...)
2360 atan2(<expr>, <expr>)
2361 min(<expr>, <expr>...)
2365 <op> := + | - | * | / | % | ^ |
2366 != | == | '<' | '>' | >= | <= |
2371 Both \fBexp\fP and \fBlog\fP are base 10; \fBint\fP does integer
2372 truncation; and \fBrand()\fP returns a random number in [0-1).
2374 There are \fBdefine\fP and \fBundef\fR statements which are not part
2375 of the grammar (they behave as pre-processor macros to the language).
2376 These may be used to define pseudo-functions.
2379 \fBdefine\fP \fIname\fP \fB{\fP \fIreplacement text \fB}\fP
2383 This defines \fIname\fR as a macro to be replaced by the replacement
2384 text (not including the braces). The macro may be called as
2387 \fIname\fB(\fIarg1, arg2, ... argn\fB)\fR
2391 The arguments (if any) will be substituted for tokens $1, $2 ... $n
2392 appearing in the replacement text. To undefine a mcro, say \fBundef\fP
2393 \fIname\fR, specifying the name to be undefined.
2396 History and Acknowledgements
2398 Original \fBpic\fP was written to go with Joseph Ossanna's original
2399 \&\fItroff\fP(1) by Brian Kernighan, and later re-written by Kernighan
2400 with substantial enhancements (apparently as part of the evolution of
2401 \&\fItroff\fP(1) into \&\fIditroff\fP(1) to generate
2402 device-independent output).
2404 The language had been inspired by some earlier graphics languages
2405 including \fBideal\fP and \fBgrap\fP. Kernighan credits Chris van Wyk
2406 (the designer of \fBideal\fP) with many of the ideas that went into
2412 The \fBpic\fP language was originally described by Brian Kernighan in
2413 Bell Labs Computing Science Technical Report #116 (you can obtain a
2414 PostScript copy of the revised version, [1], by sending a mail message to
2415 \&\fInetlib@research.att.com\fP with a body of \&`send 116 from
2416 research/cstr'.). There have been two revisions, in 1984 and 1991.
2418 The document you are reading effectively subsumes Kernighan's
2419 description; it was written to fill in lacunae in the exposition and
2420 integrate in descriptions of the GNU \fIgpic\fP(1) features.
2422 The GNU \fBgpic\fR implementation was written and is maintained by
2423 James Clark \fI<jjc@jclark.com>\fP.
2427 Kernighan, B. W. \fBPIC -- A Graphics Language for Typesetting
2428 (Revised User Manual)\fP Bell Labs Computing Science Technical Report
2429 #116, December 1991.
2431 Van Wyk, C.J. \fBA high-level language for specifying pictures\fP \fIACM
2432 Transactions On Graphics\fP 1,2 (1982) 163-182.