Using GRAP with TeX

GRAP is one of the TROFF PIC preprocessors that is intended to be used for data plots. For TeX we have MetaPost or even fancier TiKZ/PGF, but in my opinion they are not as handy as GRAP when it comes to quickly plot some 2D data points.

Unfortunately, the grap program comes with Heirloom Doctools or Plan9 (which is also from original UNIX source code and produces almost identical output) cannot produce the PIC language format accepted by dpic tool, which to my knowledge has no actual application other than been used by Circuit Macros to convert circuit diagrams written with M4 and the PIC language to suitable formats usable by TeX.

Incidentally, the GNU Groff has no grap program contained, but the external preprocessor written by Ted Faber can produce gpic instructions which is largely compatible with dpic after a small modification, that is change the:

  |Frame: [
  |Origin:
  |Left: line right 0 up 2
  |Top: line right 2 up 0
5 |Right: line right 0 up -2
  |Bottom: line right -2 up 0
  |]

to

  |Frame: [
  |Origin: [ ]
  |Left: line right 0 up 2
  |Top: line right 2 up 0
5 |Right: line right 0 up -2
  |Bottom: line right -2 up 0
  |]

It seems dpic does not like consecutive tags so an empty scope is used to separate them.

Below is the output produced from the example GRAP program:

   |.G1
   |frame ht 2 wid 2
   |coord x 0,100 y 0,100
   |grid bot dotted from 20 to 80 by 20 "%g"
 5 |grid left dotted from 20 to 80 by 20 "%g"
   | 
   |"Text above" above at 50,50
   |"Text rjust   " rjust at 50,50
   |bullet at 80,90
10 |vtick at 80,80
   |box at 80,70
   |times at 80,60
   | 
   |circle at 50,50
15 |circle at 50,80 radius .25
   |line dashed from 10,90 to 30,90
   |arrow from 10,70 to 30,90
   | 
   |draw A solid
20 |draw B dashed delta
   |next A at 10,10
   |next B at 10,20
   |next A at 50,20
   |next A at 90,10
25 |next B at 50,30
   |next B at 90,30
   |.G2
If you are the Troff expert you should already know how it looks like!
Figure 1GRAP output typed using TiKZ

Better than the result typed by Troff if you ask my opinion.

✎︎
Note

As Dwight Aplevich the author of dpic suggests, using pic define aligned {} to have pic processor ignore the instruction can workaround the problem mentioned here. Or you may provide -u flag to Faber's GRAP to prevent align instruction to be generated

There is another issue with Faber's grap which is the aligned instruction produced for title text is not implemented by dpic. The example grap program below:

  |.G1
  |frame invis ht .3 wid 5 bottom solid
  |label bot "Populations (in Millions) of the 50 States"
  |coord x .3, 35 y 0, 1 log x
5 |ticks bot out at .5, 1, 2, 5, 10, 20
  |ticks left off
  |.G2

Which emits the pic instructions (wrapped for formatting) that would be rejected by dpic. However if we use Heirloom's grap (or Plan9's, they were both based on the original Unix implementation).

   |Graph: [
   |    # gg 0.3 .. 35, 0 .. 1
   |define xy_gg @      (($1)-(-0.522879))*2.41903, (($2)-(0))*0.3 @
   |define x_gg @       (($1)-(-0.522879))*2.41903 @
 5 |define y_gg @       (($1)-(0))*0.3 @
   |    frameht = 0.3
   |    framewid = 5
   |Frame:      box ht frameht wid framewid with .sw at 0,0 invis
   |    line from Frame.nw to Frame.ne invis
10 |   line from Frame.sw to Frame.se 
   |   line from Frame.sw to Frame.nw invis
   |   line from Frame.se to Frame.ne invis
   |   textht = 0.166667
   |Label:     box invis wid 0 ht 1*textht \
15 |"Populations (in Millions) of the 50 States" \
   |with .n at Frame.s - (0,2 * textht)
   |Ticks_gg:                                                  
   |   ticklen = 0.1
   |   line  down ticklen from (x_gg(-0.30103),0)
20 |   box invis "0.5" ht .25 wid 0 with .n at last line.end
   |   line  down ticklen from (x_gg(0),0)
   |   box invis "1" ht .25 wid 0 with .n at last line.end
   |   line  down ticklen from (x_gg(0.30103),0)
   |   box invis "2" ht .25 wid 0 with .n at last line.end
25 |   line  down ticklen from (x_gg(0.69897),0)
   |   box invis "5" ht .25 wid 0 with .n at last line.end
   |   line  down ticklen from (x_gg(1),0)
   |   box invis "10" ht .25 wid 0 with .n at last line.end
   |   line  down ticklen from (x_gg(1.30103),0)
30 |   box invis "20" ht .25 wid 0 with .n at last line.end
   | 
   |]

The issue with this is the assignment statement after label, by which dpic insists it must be an object. Removing the label or add an extra Here should fix the problem.

After the issue is fixed, dpic produces the image below.

You should have seen the similar image from PIC's manual
Figure 2Image produced by conversion to TikZ