From u600256@rho.lanl.gov Tue Jul 5 17:03:14 1994 Date: Tue, 5 Jul 94 12:56:26 -0600 To: aliaw@picard.tamu.edu Subject: gp-ps.sample A short guide to PostScript files created by gnuplot's "postscript" terminal by Dick Crawford, aka rccrawford@lanl.gov Before we begin, let me say a few words about PostScript. It is highly ver- satile, but with versatility comes complexity. Syntax is extremely important; a misplaced command can do unexpected things. Thus the adventurous soul who wishes to alter the PostScript but has not been initiated into the arcana of PostScript would be well-advised to change only those items (like numerical values, font names or heights, or the letters defining symbols) that are fairly obvious as to their meaning. I am ignoring those commands used for color PostScript. If you want to play with that, you're on your own. If you use the enhanced PostScript terminal 'enhpost' and want to play with its output, many of the comments embedded in the sample file below are appli- cable to it also. The main difference is in the way enhpost handles text. It also has an expanded repertoire of line and point styles. PostScript is stack-oriented and works in Reverse Polish notation: 'a b add' takes 'a' and 'b' off of the stack and replaces them with 'a+b'. The 'stroke' command actually draws lines on the page. It uses whatever line width and style are currently defined. Thus if we specify a bunch of commands like 'LT1 1 2 moveto 3 4 lineto LT3 stroke' [LT1 and LT3 are line type defi- nitions, 'x y moveto' moves the (virtual) pen to (x,y) and 'x y lineto' draws a line from wherever the pen was to (x,y)], the line connecting points (1,2) and (3,4) will be in type LT3. PostScript is case-sensitive. The sample file below has been pruned of repeated commands, just to keep its length down. Comment lines begin with '%'. Lines beginning with '%# ' have been added to the sample by me for explanatory purposes. Here we go. %!PS-Adobe-2.0 %%Creator: gnuplot %%DocumentFonts: Helvetica %%BoundingBox: 50 50 554 770 %%Pages: (atend) %%EndComments %# %# The 'dictionary' contains stuff defined by the user. %# /gnudict 40 dict def gnudict begin /Color false def /Solid false def %# %# The following parameter scales all linewidths in the plot. %# /gnulinewidth 5.000 def %# %# The following parameter specifies the vertical displacement of the labels %# and titles (it is used in the 'show' commands defined below). It should %# be about 1/3 of the font height. %# /vshift -46 def /dl {10 mul} def %# %# The following two parameters scale the horizontal and vertical sizes of %# the symbols used by 'plot with points'. %# /hpt 31.5 def /vpt 31.5 def %# %# The following four commands are aliases of the four 'pen movement' %# commands. Use of these aliases significantly shortens the file. Note %# that the first two are 'absolute' movements and the latter two are %# 'relative'. M and R move to the specified position; L and V draw a %# straight line (from the current position) to it. %# /M {moveto} bind def /L {lineto} bind def /R {rmoveto} bind def /V {rlineto} bind def %# %# These are a couple more parameters used in plotting symbols. Why %# they are here instead of up with 'hpt' and 'vpt' I know not. %# /vpt2 vpt 2 mul def /hpt2 hpt 2 mul def %# %# The 'show' command writes out a character string. The following three %# varieties do so as left-, right-, and center-justified. [Remember, %# we're still in the dictionary--the font doesn't need to be specified %# until we actually use one of these.] %# /Lshow { currentpoint stroke M 0 vshift R show } def /Rshow { currentpoint stroke M dup stringwidth pop neg vshift R show } def /Cshow { currentpoint stroke M dup stringwidth pop -2 div vshift R show } def /DL { Color {setrgbcolor Solid {pop []} if 0 setdash } {pop pop pop Solid {pop []} if 0 setdash} ifelse } def %# %# The following commands define the various line types (normal, bold, %# dashed, etc.) used by gnuplot. %# /BL { stroke gnulinewidth 2 mul setlinewidth } def /AL { stroke gnulinewidth 2 div setlinewidth } def /PL { stroke gnulinewidth setlinewidth } def /LTb { BL [] 0 0 0 DL } def /LTa { AL [1 dl 2 dl] 0 setdash 0 0 0 setrgbcolor } def /LT0 { PL [] 0 1 0 DL } def /LT1 { PL [4 dl 2 dl] 0 0 1 DL } def %# %# ...and a bunch more. %# %# The following commands define the symbols used to plot data points. %# /P { stroke [] 0 setdash currentlinewidth 2 div sub M 0 currentlinewidth V stroke } def /D { stroke [] 0 setdash 2 copy vpt add M hpt neg vpt neg V hpt vpt neg V hpt vpt V hpt neg vpt V closepath stroke P } def %# %# ...and a bunch more. %# %# The dictionary is now complete. We activate it, save what went before %# (the 'gsave' command saves everything on a different stack) and begin. %# end %%EndProlog %%Page: 1 1 gnudict begin gsave %# %# First we position the plot on the page and scale it. %# %# The 'translate' command moves the origin to the specified position. %# [The PostScript default origin is near the lower left-hand corner.] %# The 'scale' command changes the units used in the plot. %# [The PostScript default unit is the point: 72 points equal one inch.] %# The 'rotation' command rotates the coordinates clockwise through the %# specified angle (degrees). %# [The PostScript default orientation is profile.] %# %# This sample changes the orientation to landscape (the gnuplot default) %# and the unit to tenths of a point. Note that if the first two commands %# were interchanged, the translation would be only five points in each %# direction, instead of fifty. %# %# If you want to change the size or the position of the plot, this is where %# to do it. %# 50 50 translate 0.100 0.100 scale 90 rotate 0 -5040 translate 0 setgray %# %# Define the font to be used. The number is the height. As usual, fonts %# used primarily for text have characters only about 70% the specified %# height, because spacing between lines is built-in. The Symbol font and %# others that are normally not used for text produce characters that are %# more nearly the specified height. Thus if you want to intersperse, for %# example, Greek and Roman letters (as you might when writing an equation), %# you'll need to play with the heights of the two fonts in order for them %# to look reasonably proportioned. %# %# The font defined here is used for the tick labels first, then the axis %# labels and plot titles, and finally the key. If you want to change the %# font, simply insert the modified line after you are done with the old one %# (i.e. the last 'show' command to use it) but before the new one is needed. %# Note that if you change the font height, you'll also want to change the %# value of 'vshift' (it's near the top of the dictionary). I'll give an %# example of this later on... %# /Helvetica findfont 140 scalefont setfont newpath %# %# Draw the left and lower axes. %# LTa 840 351 M 6129 0 V 840 351 M 0 4478 V %# %# Draw and label the ticks along the y-axis. %# %# The tick length is 63 units. %# LTb 840 351 M 63 0 V 6066 0 R -63 0 V 756 351 M (0) Rshow %# [Several labels removed to shorten the file...] 840 4829 M 63 0 V 6066 0 R -63 0 V -6150 0 R (8) Rshow %# %# Draw and label the ticks along the x-axis. %# 840 351 M 0 63 V 0 4415 R 0 -63 V 840 211 M (0) Cshow %# [Several labels removed to shorten the file...] 6969 351 M 0 63 V 0 4415 R 0 -63 V 0 -4555 R (3) Cshow %# %# Draw the bounding rectangle. %# 840 351 M 6129 0 V 0 4478 V -6129 0 V 840 351 L %# %# Label the axes and title the plot. %# 140 2590 M currentpoint gsave translate 90 rotate 0 0 M (ylabel) Cshow grestore 3904 71 M (xlabel) Cshow 3904 4969 M (title) Cshow %# %# The key is drawn just before the applicable data. %# %# Choose a line type and write the key for function 1. %# In this sample, it is drawn with lines. %# %# The key: %# LT0 6486 4626 M %# %# Suppose we want the font in the key to be smaller and a different type %# face. We'd insert right here, for example, %# %# /vshift -33 def %# /Times-Roman findfont 100 scalefont setfont %# %# (function 1) Rshow 6570 4626 M 252 0 V %# %# The data: %# 840 911 M 2043 560 V 4926 911 L 2043 560 V %# %# ...and more. %# %# We don't need to eplicitly 'stroke' to draw the lines for function 1 %# because the 'stroke' command is included in the line-type definition %# commands. Thus switching line types automatically 'strokes' previous %# lines. Clever program, that gnuplot... %# %# Choose a line type and write the key for function 2. %# In this sample, it is drawn with points. %# %# The key: %# LT1 6486 4486 M (function 2) Rshow 6654 4486 D %# %# The data: %# 840 911 D 2883 2030 D 4926 2590 D 6969 3710 D %# %# ...and more. %# %# Since we're now done, we 'stroke' the last lines, close the dictionary %# and restore the previous settings (those saved by the 'gsave' at the top). %# [The 'gsave'/'grestore' pair is included so that if this file is embedded %# in another PostScript file, this patch won't mess up the other parts of %# the picture.] %# stroke grestore end %# %# And, finally, we send the page to the printer. [If we do embed this %# file into another PostScript file, we'd remove this 'showpage'. Unless, %# of course, this was appended to the other file, in which case we'd %# remove the 'showpage' from the end of that file.] %# showpage %%Trailer %%Pages: 1 I hope this is useful.