Named Variables

Beside the integer and string types of operands, there is also a “name”
operand. You can recognize a literal name because it is preceded by a
slash. It can be any word you make up. Since all the built-in
commands in Postscript are lower case, you can’t go wrong by throwing
in an upper-case letter or two. Here are some of the names I made up
for a program we’ll be talking about later:


You get the idea.

Literal names are almost always first used with the def operator,
which defines the name as a variable which contains an operand or a
procedure. The Adobe reference manual says

key value def

which means the def operator expects on the stack a key (the name) and
a value (an operand or procedure) that you want the name to be
associated with. The def operator doesn’t leave anything on the stack,
but the result of using it is that your program will know the value of that variable when you use it.

For example, define three variables:

/firstNum 2 def
/secondNum 3 def
/MyProcedure { add } def

Notice that braces go around the add operator to defer it from
executing immediately.

In the program, use the names without the slash prefix in order to
execute them:

firstNum secondNum MyProcedure

is equivalent to saying

2 3 add

The number 5 will be left on the stack to do with as you will, such as
storing it in another variable:

/theResult firstNum secondNum MyProcedure def

You want to increment theResult by two? Do this:

/theResult theResult 2 add def

or even

/bump { 2 add } def
/theResult theResult bump def

By this time the value held by the variable theResult should be
something like 9. How do you find out for sure? In our last lesson we
introduced the “print” operator which is only good for printing
strings. Another built-in procedure is the equal sign,=, which first converts
anything into a string and then prints. Now is a good time to create a
test file, save it as (or whatever), and run it thru your

/firstNum 2 def
/secondNum 3 def
/MyProcedure { add } def
/theResult firstNum secondNum MyProcedure def
(theResult is ) print theResult =


3 thoughts on “Named Variables

  1. This one works in my interpreter while yours doesn’t:

    /firstNum 2 def
    /secondNum 3 def
    /MyProcedure { add } def
    /theResult firstNum secondNum MyProcedure def
    (theResult is ) show theResult ( ) cvs show

  2. Thanks Peter. Yes, the “print” and “=” commands are not available in all interpreters.

    To run this in the Macintosh Preview application, for instance, you’d need to also define the font, position on the page with a “moveto”, and finish with a “showpage:

    /Helvetica 10 selectfont

    /firstNum 2 def
    /secondNum 3 def
    /MyProcedure { add } def
    /theResult firstNum secondNum MyProcedure def

    300 500 moveto
    (theResult is ) show theResult ( ) cvs show

  3. However, in the Macintosh Terminal using the built-in pstopdf command, the PostScript “print” and “=” commands are properly interpreted:

    $ pstopdf ~/desktop/
    theResult is 5

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s