In Praise of Picby Philipp K. Janert
In praise of what? Pic? Pic? Pic, the old diagram generation "little language" and half brother of roff (or troff or groff), from the days when Unix was young? Yes, indeed, that pic.
Why, in this day of convenient WYSIWYG tools, from Xfig over the GIMP to Visio, would anybody want to make drawings using a command-language, which needs to be compiled(!) to generate a viewable diagram? Well, as it turns out, there are in fact a number of excellent reasons:
- Pic is scriptable, making it an ideal tool when one has to draw several diagrams with minor, but precisely known differences.
- Pic is coordinate-oriented, which is very convenient when drawing geometric bodies or similar diagrams.
- Pic allows the scaling of diagrams to any desired size, and since it is not pixel-oriented, diagrams scaled in this way are free of the "ragged edges" typical of bit-mapped formats.
- Pic is simple. The entire manual, including plenty of examples, fits into some two dozen pages. No need to spend time on a complicated toolset, when one only wants to generate some illustrations.
- Pic is free, open source, quite mature, and very well documented.
My own reasons for learning about pic were a combination of several of these reasons. I wanted to generate images of several 3D bodies (cubes, cones, etc.). But I wanted to be free to change the viewing angles and orientations, without having to do a lot of manual rework. And preparing these illustrations was supposed to distract me only minimally from my main objective. So, pic served me well.
But there is yet another reason for studying pic. It is an excellent example how powerful a "little language" can be. In particular, I was impressed how pic achieves great ease of use through meaningful defaults, which often seem to anticipate the "common case" very well. It can do so, in part because its application area is intentionally quite limited: black-and-white drawings of regular shapes. Because the job it is meant to do is small, pic itself can be simple.
Pic is a little language for the specification of certain kinds of diagrams "frequently used in technical papers and textbooks" (quoted from "Making Pictures with GNU PIC" by Eric S. Raymond). It is not meant to be a general-purpose graphing language, although its scripting abilities allow application to areas not initially intended.
Here is the equivalent of "Hello, World!", using pic:
.PS box "Hello" .PE
and it is compiled into PostScript using the following command line (using the GNU version of all tools):
pic hello.pic | groff > hello.ps
Alternatively, you can call
groff with the
-p option, like so (see Figure 1):
groff -p hello.pic > hello.ps
Figure 1. Hello
A more interesting program is this:
.PS box "Box" arrow "Arrow" "" circle "Circle" line "Line" "" ellipse "Ellipse" arc "Arc" .PE
With the exception of the
spline command, this picture demonstrates all graphing elements native to pic. We see two things; for one the syntax is trivially easy. The only features that aren't entirely obvious are the lines
.PE. These are
groff directives to start and end a picture. Also, the selection of graphics primitives is quite limited: no triangles, diamonds, or pie slices, etc. (see Figure 2).
Figure 2. All graphing elements native to pic
What struck me most about this syntax, though, was the kind of sensible defaults. The diagram "flows" in some direction, following the commands. New picture elements are joined to one another in an obvious way. (By default, the flow is left-to-right, but can be changed at any point with the
Although the choice of graphics primitives is limited, there are many ways to change the appearance of the elements.
All elements can be individually sized like so (see Figure 3):
.PS box # Default size: 0.75 wide by 0.5 high move box width 0.1 # Full name of size attribute move box wid 1.5 ht 0.25 # Abbreviated attribute names are possible .PE
Figure 3. Box sizes
All sizes are given in inches of the final (printed) diagram (!). The overall scale can be changed by including the
scale statement before any drawing command, e.g.,
scale=2.54 will change the unit to centimeters.
Objects can be dashed, dotted, and filled. GNU pic also allows you to generate boxes with rounded corners. All these decorations can be customized, usually through a numeric argument. (In the following example, note the double arrow as well.) See Figure 4.
.PS box rad 0.15 # Box with rounded corners line dotted ellipse dashed 0.2 line <-> # Double arrow circle fill 0.4 move to last circle .n + ( 1, 0 ) # Explained in section on Coordinates below spline right 1 then down 0.5 left 1 then right 1 move to last spline .start # Go back to start of spline, cf. text below line dashed right 1 then down 0.5 left 1 then right 1 .PE
Figure 4. Decorations
Splines can be used to express some "dynamic" diagrams in an easy fashion. They are specified through their vertices, as seen when comparing the spline to the control line in the picture above.
Pic makes massive use of relative coordinates. Mostly, you tell pic to go some place by telling it how to get there from some place you already know. So, pic gives you a rather expressive set of commands to describe relative positions.
The simplest is the
2nd last etc.) directive, illustrated in Figure 5:
.PS circle move box move circle arrow from 2nd last circle to last box .nw arrow from 2nd last circle to last box .sw line from last box to last circle .w .PE
Figure 5. Circle illustrating 2nd last directive
Pages: 1, 2