From: feh@biostat.mc.duke.edu (Frank Harrell)
Subject: Shrink-wrapping postscript output
X-Gateway: dmssyd.syd.dms.CSIRO.AU
Organization: CSIRO Div. Maths & Stats, CSIRO Div. Applied Physics
Date: Mon, 28 Sep 1992 18:32:36 GMT

Some weeks ago I put out a query regarding functions for "shrink-wrapping"
postscript output for inclusion in LaTeX or TeX documents. Antonio Possolo
(antonio@atc.boeing.com, Applied Math & Stat, Boeing)
was kind enough to respond with a nice function, psfig, which I have
enhanced slightly. This function sets up for an almost-minimal bounding
box and for nice fonts.  The function disallows main titles, which are
assumed to be added in the figure's caption.  Antonio likes the default
ratio of width/height (the "golden ratio").
I often prefer a smaller ratio of 1.4. If exactly one of (width, height)
is zero, the "ratio" is used to replace it based on the one specified.
For a single figure in the plot I usually use psfig(filename,height=3).
The logic in psfig assumes that one figure is being drawn, i.e., that
par(mfrow=c(1,1)) is in effect. It will work for multiple plots if you
set pointsize to something like 9.
sublines specifies the number of extra lines to leave at the bottom of
the plot for subtitles.

By the way, I prefer "psbox" for the final inclusion of figures in
LaTeX/TeX, using the following LaTeX macro:

%Example: \fig{myfig}{An example of a good fit on worthless data}
%Inserts postscript figure myfig.ps into document, with caption given
%For a file named x.ps, a label "x" is generated.
\newcommand{\fig}[2]{\begin{figure}[hptb]               %
        \begin{center}\mbox{$$\psbox{#1.ps}$$}\end{center}      %
%       \psfig{figure=#1.ps,clip=t}                     % use this for psfig
        \small                                          % instead of psbox
        \caption{#2}                                    %
        \label{#1}                                      %
        \end{figure}}

----------------------------------------------------------------------------
Frank E Harrell Jr			feh@biostat.mc.duke.edu
Associate Professor of Biostatistics
Division of Biometry                    Duke University Medical Center
-----------------------------------------------------------------------------
"psfig"<-
function(file = "", width = 0, height = 0, ratio= (1 + sqrt(5))/2,
		font = "Times-Roman", pointsize, sublines=0)
{
#	POSTSCRIPT FIGURE MAKER
#	for incorporation into TeX using PSFIG or BoxedEPSF.
#	The strategy is to create a pleasant aspect ratio, 
#	while minimizing white space around the figure.
#
# Aspect ratio is Golden Ratio
# Standard width is 30 picas = 30*12/(72.27) inches
	StandardWidth <- (30 * 12)/(72.27)
	StandardHeight <- StandardWidth/ratio
	StandardPointSize <- 9
	if ( width == 0 & height == 0 ) 
		{ 
		width _ StandardWidth
		height _ StandardHeight
		scale _ 1
		}
	if ( width > 0 & height == 0 ) 
		{ 
		height <- width/ratio
		scale <- width/StandardWidth
		}
	if ( width == 0 & height > 0 ) 
		{ 
		width <- height*ratio
		scale <- width/StandardWidth
		}
	else scale <- max(width/StandardWidth,height/StandardHeight)

	if(is.null(pointsize)) pointsize <- round(scale * StandardPointSize)
#
#	FONTS & FONT SELECTION
#
#  1 Helvetica               19 Bookman-DemiItalic
#  2 Courier                 20 Bookman-Light
#  3 Times-Roman             21 Bookman-LightItalic
#  4 Helvetica-Oblique       22 Helvetica-Narrow
#  5 Helvetica-Bold          23 Helvetica-Narrow-Bold
#  6 Helvetica-BoldOblique   24 Helvetica-Narrow-BoldOblique
#  7 Courier-Oblique         25 Helvetica-Narrow-Oblique
#  8 Courier-Bold            26 NewCenturySchlbk-Roman
#  9 Courier-BoldOblique     27 NewCenturySchlbk-Bold
# 10 Times-Italic            28 NewCenturySchlbk-Italic
# 11 Times-Bold              29 NewCenturySchlbk-BoldItalic
# 12 Times-BoldItalic        30 Palatino-Roman
# 13 Symbol                  31 Palatino-Bold
# 14 AvantGarde-Book         32 Palatino-Italic
# 15 AvantGarde-BookOblique  33 Palatino-BoldItalic
# 16 AvantGarde-Demi         34 ZapfChancery-MediumItalic
# 17 AvantGarde-DemiOblique  35 ZapfDingbats
# 18 Bookman-Demi           
#
	if(is.number(font)) {
		fontNumber <- font
		if(fontNumber < 1 | fontNumber > length(ps.fonts)) {
			fontNumber <- 1
			cat(paste(
				"\tPSFIG WARNING: Font requested is not available\n",
				"\t\tSubstituted by Helvetica\n"))
		}
	}
	else {
		fontName <- font
		fontNumber <- match(fontName, ps.fonts)
		if(is.na(fontNumber)) {
			fontNumber <- 1
			cat(paste(
				"\tPSFIG WARNING: Font requested is not available\n",
				"\t\tSubstituted by Helvetica\n"))
		}
	}
	postscript(file = file, horizontal = F, width = width, height = height,
		pointsize = pointsize, font = fontNumber
		)	#
#
#	PLOT DESIGN
#	Lines are 1pt wide, which is half standard width
#	(LWD is interpreted in units of 1/36 inch
#	 LWD=0 yields the thinnest possible line on the device)
#	Axis labels closer to axes than default 
#	(MGP: margin line for the axis title, axis labels, 
#	 and axis line in units of MEX)
#	Margin widths narrower than default
#	(MAR: bottom, left, top, right)
	par(lwd = 0.5, mgp = c(2, 0.5, 0), mar = c(3+sublines, 3, 1, 1) + 
		0.10000000000000001)	
	#	SMO is number of rasters that the piecewise linear
#	approximation to a curve is allowed to differ from the exact
#	position of the curve.
	par(smo = 0)	#	PLOTTING SYMBOL
#	PCH  selects plotting characters from Standard Encoding
#	(PostScript Language Reference Manual, p.252)
#	168 = currency
#	180 = centered period
#	183 = bullet (with a negative font parameter yields a circle)
	par(pch = 183)	#
# MAIN TITLE not allowed: plot will be described in figure caption, 
#	handled by TeX itself.
#	cat(paste("\tPSFIG WARNING:", "Do not use high-level parameter MAIN\n",
#		"\t\tFigure caption should be created within LaTeX\n"))	#
	invisible()
}

Newsgroups: aus.stats.s