groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [Groff] An example for `command' in pic.man


From: Ted Harding
Subject: RE: [Groff] An example for `command' in pic.man
Date: Sat, 15 May 2004 00:52:39 +0100 (BST)

On 14-May-04 Bernd Warken wrote:
> The pic preprocessor has a powerful interface to groff outside with
> `command'.  Although its syntax is not straightforward, no examples
> seem to exist for this interface.  The following diff adds an example
> to the man page for pic in <groff-source>/src/preproc/pic/pic.man:

The following is a much more complicated example, which could be
included in something, though probably not a man page. It illustrates
the fact that not only can 'pic' draw pictures (which is done in the
example), but it also includes a powerful numerical computation engine.

The example reads in a small data file, plots the data (with some
annotations), calculates the coefficients for a regression of y on x,
and then (with 'pic' wrapped in 'tbl') outputs an analysis of variance
for the regression. In order to embed 'pic' in 'tbl' a modification
of the 'ms' macros .PS and .PE is included, so that .PS produces no
visible effect (T for "tacit" if you like).

The data file is at the end.

Groff this with groff -tpe -ms

Best wishes to all,
Ted.

============================================

.de PS
.ie '\\$3'T' \{\
.\"address@hidden Using pic in Table
.ds pic*Table T
.\}
.el \{\
.ds pic*Table
.br
.sp \\n[DD]u
.ie \\n[.$]<2 address@hidden bad arguments to PS (not preprocessed with pic?)
.el \{\
.       address@hidden (u;\\$1)+1v
.       in +(u;\\n[.l]-\\n[.i]-\\$2/2>?0)
.\}
.\}
..
.de PE
.ie '\\*[pic*Table]'T' .rm pic*Table
.el \{\
address@hidden
.sp \\n[DD]u+.5m
.\}
..
.LP
\&
.sp 1
.PS
n=0; sx=0; sy=0; sxx=0; syy=0; sxy=0; a=0; b=0; f=0
copy "regress.dat" thru {
n=n+1; sx=sx+$1; sy=sy+$2
sxx=sxx+$1*$1; syy=syy+$2*$2; sxy=sxy+$1*$2
sprintf("\(bu") at ($1,$2);
  move right $4 up $5 $3 ljust
if f==0 then {
  xmin=$1; xmax=$1; ymin=$2; ymax=$2; f=1; } \
else {
  xmin=min(xmin,$1); xmax=max(xmax,$1);
  ymin=min(ymin,$2); ymax=max(ymax,$2);
  }
}
mx=sx/n; my=sy/n; sxx=sxx-(sx*sx/n);
syy=syy-(sy*sy/n); sxy=sxy-(sx*sy/n)
b = sxy/sxx; a=my-(b*mx);
{ line from (0, 0) to (5, 0) };
{ line from (0, 0) to (0, 3) }
for x=0 to 5 by 1 do {
  line down 0.05 at (x,0)
  sprintf("%1.0f",x) below
  }
for y=0 to 3 by 1 do {
  line left 0.05 at (0,y) sprintf("%1.0f ",y) rjust
  }
line invis \
  from (0,-0.25) to (5,-0.25)\
  "\fINumber of Drinking Companions\fP" below
line invis \
  from (-0.2,0) to (-0.2,3)\
  "\fIAverage Time between Buying Rounds\fP" aligned above
line invis from (0,3.5) to (5,3.5) \
  "\s-1U M I S T\s0  Social Interaction Working Party" "" "1994"

if b<0 then {
line from (xmin,a+b*xmin) to (xmax,a+b*xmax) \
sprintf("\fIy\fP = %4.2f \- %4.2f \fIx\fP",a,-b) aligned above
} else {
line from (xmin,a+b*xmin) to (xmax,a+b*xmax) \
sprintf("\fIy\fP = %4.2f + %4.2f \fIx\fP",a,b) aligned above
}
.PE
.sp 1
.LP
.TS
center tab(@);
l n.
.PS T
## Summary statistics
dfreg = 2 ; dfres = (n-2); r = sxy/sqrt(sxx*syy);
ssreg = syy*r*r ; ssres = (syy - ssreg);
fratio = (ssreg/dfreg)/(ssres/dfres)
command "_"
command "\fINumber of Data\fP@"      n
command "\fID.F. for Regression\fP@" dfreg
command "\fID.F. for Residuals\fP@"  dfres
command "_"
command "\fIF-Ratio\fP@"             fratio
command "_"
command ".TE"
.PE

======== file "regress.dat" ===================
1 0.7  "Alf" 0 .1
2 0.85 "Bert" 0.07 0
3 1.4  "Charlie" 0 -0.12
4 2.25 "Dave" 0 0.1
5 2.35 "Ed" 0.07 0

====================================================


--------------------------------------------------------------------
E-Mail: (Ted Harding) <address@hidden>
Fax-to-email: +44 (0)870 167 1972
Date: 15-May-04                                       Time: 00:52:39
------------------------------ XFMail ------------------------------


reply via email to

[Prev in Thread] Current Thread [Next in Thread]