MSRI Graphics School - Lecture 3 on PostScript

Special comments

All lines beginning with % in PostScript are comments, but some such lines are more comments than others. More interesting, at any rate. They are not part of your PostScript program, and not interpreted by gs, but they are read and interpreted by your viewer (such as gv) or your printer.

  • %!PS-Adobe-2.0 is a good combination of magic words to start your file. Don't even think about why. Viewers just seem to like it.
  • %%BoundingBox: 0 0 100 100 tells the viewer that your picture fits into a box whose lower left corner on the page is at (0, 0) and whose upper right corner is at (100, 100). Some viewers (including gv) will restrict their view to this rectangle (as long as you have the special introductory line %!PS-Adobe ... in your file).
  • %%Pages: 3 says that your file has three pages to display.
  • %%Page: 1 1 goes just before page 1. This and the %%Pages specification allow your viewer to go back and forth among different pages. This will produce weird effects unless each page is truly isolated from other pages---i.e. do not change the value of a global variable on one page and use it on another.
Samples In the original program, pageno and g were global variables that were changed on each page, but here we have to set them explicitly on each page. The word `Page' is slightly misleading here - a `page' is just a section of your program isolated from others. You could have several showpage operators in between them, but then the usual English designation of page is no longer valid.

Adding TEX labels

The bounding box of a PS file plays an important role in the task of placing a TEX label in a diagram like this:

This is done in several steps:
  1. Make up the PostScript file drawing the figure. Enclose this part by a gsave/grestore pair in order to restore the default coordinate system.
  2. Make up the TEX file with the label you want placed as a mathematics display. In Plain TEX, put \nopagenumbers at the beginning of the file. Thus:
    \nopagenumbers
    $$ y = x^2 $$
    \bye
    
    Using Latex, the file might be
    \documentclass[10pt]{article}
    \usepackage{amssymb,amsmath,amsfonts}
    \pagestyle{empty}
    \begin{document}
    $$y=x^2$$
    \end{document}
    
  3. Suppose this file is L.tex. Now TEX it, producing L.dvi.
  4. Do dvips -E L -o L.eps, obtaining the file L.eps. The option -E produces a PostScript file known as an encapsulated file with a bounding box delimiting the image.
  5. Look at the bounding box of the file L.eps, say the lower left corner is 200 691.
  6. Include in your diagram file at the end lines like
    gsave
    -200 -691 translate
    (L.eps) run
    grestore
    
How to transform the diagram you import is shown in the sample below. In the next lecture we'll see how to automate the somewhat intricate procedure we did `by hand' here. Exercise. Reproduce the following graph:

The file as it is written above has a run command in it, which essentially includes the file it is running. It is not all by itself a complete file because it does not contain the PostScript code it is running. If you want to print the file or make an image out of it, you must create a new file with the file you run actually included, replacing the Run line. You can do this with most text editors (that allow you to include a file wholesale).

There is one final problem. The .eps file produced by TEX and dvips has a showpage at its end. This changes pages in your file, of course, and makes it impossible to include several labels without some fiddling. The correct thing to do is define showpage to be {} while including label files, and then restore the usual definition at the end of your file The way to do this is put a new dictionary on the stack, and then remove it. This is done in the files here.