Potentially Useful

Writing boring posts is okay

PostScript Graph Paper

(first posted ) in: Programming

I needed a few pages of graph paper, and it seemed easier and more economical1 to print some at home than to buy a whole pad of it. There are plenty of websites offering PDF files with grids on them, but I didn’t like what I found2 and decided to make my own. I thought that the process was fun enough that I have since made a few different kinds:

These files are meant for US Letter paper but could easily be adapted to other dimensions. They are also “full bleed” because I decided to allow my printer to impose its own margins; just be sure that you turn off any “autoscale” or “fit to page” option before printing these.

Why PostScript?

Drawing a grid is not difficult to do with a computer. I considered writing code to programmatically generate an SVG file, but then I recalled learning that PostScript, the precursor to the PDF format, is itself a bona fide programming language. It only took an afternoon of familiarizing myself with the language to make the grid, and I kept going from there.

It turns out that PostScript is a stack-based (“concatenative”) language, like Forth or UXNTAL. I was playing with the latter last fall3, so I have some familiarity with this type of programming. Stack languages seem strange when you’re used to other paradigms, but once you get started you may find it easy to shift into the right mindset for structuring programs this way.

A complete PostScript program, which produced the 5 mm × 5 mm grid linked above, follows:

% Adjust these to suit your needs, units specified in "points" (1/72 inch)
/w 612 def /h 792 def      % US Letter paper
/sp 5 72 mul 25.4 div def  % Grid spacing (5 mm here)
/st 1 72 mul 300 div def   % Stroke width (1 'dots' in a 300 dpi resolution)
/co 0.6 def                % Gray-level of the line (0 = black, 1 = white)

% Remainder function (`mod` doesn't take floating point values)
/r {dup 3 1 roll div dup floor sub mul} def
% Return the starting offset that centers the grid
/o {r 2 div} def

% Page and line setup
<< /PageSize [w h] >> setpagedevice
co setgray
st setlinewidth

% Create vertical lines
w sp o
sp w
  0 moveto
  h lineto
} for

% Create horizontal lines
h sp o
sp h
  0 exch moveto
  w exch lineto
} for

% Draw

This code is probably not idiomatic or efficient, and I welcome feedback on writing better PostScript. But it worked for me and won’t be hard to adapt to your own needs.

Programming with PostScript

If you’re interested in playing with PostScript programming, Ghostscript4 has an interactive interpreter that allows you to manipulate and view the stack (just type stack), and draw on a page. I just came across this relevant quote from Peter Norvig:

Play. Which way would you rather learn to play the piano: the normal, interactive way, in which you hear each note as soon as you hit a key, or “batch” mode, in which you only hear the notes after you finish a whole song? Clearly, interactive mode makes learning easier for the piano, and also for programming. Insist on a language with an interactive mode and use it.

Interest in PostScript peaked during an era when books were easily the best way to learn about a computing technology5; I’ve seen the following recommended and found them fairly easily online:

  1. I actually printed on the back of scrap paper because I’m that cheap eco-conscious. ↩︎

  2. For starters, these sites all branded their downloads—which is certainly their right, but I didn’t want an advertisement on my paper. ↩︎

  3. I started learning it for a project I ought to pick back up. I followed compudanzas’ introduction to uxn programming and recommend it! ↩︎

  4. I used Ghostscript to convert these PS files to PDF, and have been using it to tweak PDFs from the command line for years without realizing it was a whole interactive programming language interpreter. ↩︎

  5. This may well still be true today, but speaking for myself, I rarely begin learning new tools with a trip to the library, as I once had for C and Perl. ↩︎