Basic commands

There are many commands defined in PostScript. That's the bad news. Or is it the good news? What is definitely good news is that many of them are close to common English usage, so they are easy to to remember.

Setting up

  • %! The first two characters of every PS file
  • % Begins a comment - rest of line ignored
  • showpage Usually the correct end to every page. Printers will not print a file without this. Image manipulation programs (such as GIMP) will not display them, either.
  • %!PS-Adobe-2.0 Correct first line of an EPS file
  • %%BoundingBox: lx ly ux uy Document Structure Comment (not true PostScript) telling dimensions - for viewers

Building and drawing paths

  • newpath Start of every path
  • x y moveto Starts a path segment
  • x y lineto Continues a path segment
  • stroke Draws a path in the current color
  • fill Fills a path in the current color
  • clip Restricts drawing to within the current path
  • x y r a b arc Builds an arc in positive direction
  • x y r a b arcn ... negative ...
  • closepath The correct way to end a closed path

stroke, fill, and clip are the essentially only things you can do with a path (although there is also a complicated operation known as gradient fill).

Displaying stuff

  • = Displays and removes from stack
  • == Displays more and removes from stack
  • stack Displays the whole stack without removing stuff
  • pstack Displays the whole stack in more detail (without removing stuff)

Mathematical functions

All of these apply an operation to arguments on the stack and leave the result on the stack.
  • x y add
  • x y sub
  • x y mul
  • x y div
  • x sqrt
  • x neg
  • x abs
  • x cos Angles are in degrees
  • x sin
  • y x atan y x on the stack is replaced by the angle of the point (x, y)
  • x y exp Puts xy on the stack.
  • x ln

The number e = 2.718281828 ... has to be defined by you. As does pi = 3.1415926535 ...

Coordinate changes

  • x y scale
  • x y translate
  • x rotate

Other pieces of the graphics state

  • x setlinewidth Sets the current linewidth in the current units
  • currentlinewidth Gets the current linewidth in the current units
  • gsave Makes a new copy of the current graphics state and puts it on the graphics stack
  • grestore Removes the current graphics state from the graphics stack
  • g setgray Sets the current color to a tone of gray
  • r g b setrgbcolor Sets RGB components
The effect of a nested pair gsave ... grestore is to confine all the changes to the graphics state inside the pair. In general, every page should be nested in this way.

For choosing colours, you might find it useful to look at an indexed colour cube.

Definitions, local and global

  • /s * def Basic assignment of a value
  • /proc { 1 dict begin
    /x exch def
    ...
    end } def
    The correct way to obtain local variables inside a procedure

Stack operations

  • * * exch Exchanges top two items of the stack
  • * dup Duplicates top item
  • pop Removes top item
  • a b roll Rolls the top a items up b

Conditionals

  • true Boolean constant
  • false
  • t { ... } if
  • t { ... }{ ... } ifelse
  • a b eq Returns true or false
  • a b ne
  • a b lt
  • a b gt
  • a b le
  • a b ge

Loops

  • n { ... } repeat Repeats the procedure in between n times
  • start increment final { ... } for The for loop
  • exit Exits from a block of code.
  • { ... } loop The loop loop. Used in combination with a test for exit. Otherwise loops forever.
The for loop puts the loop variable on the stack at the entry to the loop. You should usually remove it with something like /i exch def, as at the beginning of a procedure that reads input.

Sample loop code