5

I need to draw some pictures for my LaTeX documents, and I've found that hand-made PostScript seems to be a good fit (I want to do stuff programatically, need math functions, etc.). I've also tried TikZ but that just seemed overcomplicated and hard to use.

However, using plain standard PostScript is a bit painful since there aren't really any standard functions for drawing shapes (e.g. not even rectangles).

Is there any PostScript library that would include functions for common shapes and make life a bit easier? Seems to me this problem should be fairly common.

Or should I skip PostScript and move on to some superior system? Which one?

pafcu
  • 7,808
  • 12
  • 42
  • 55
  • PGF/TikZ is IMHO really easy to use and has great libraries that enable you to create great drawings with only a couple of lines. – f3lix Nov 29 '09 at 17:04

7 Answers7

6

A few people and many PostScript drivers define their own set of procedures for drawing shapes. A PostScript driver may output the following shortcuts:

/bd{bind def} bind def
/cp{closepath}bd
/gs{gsave}bd
/gr{grestore}bd
/m{moveto}bd
/rm{rmoveto}bd
/l{lineto}bd
/rl(rlineto}bd
/s{stroke}bd
/f{fill}bd
/sf{gs s gr f}bd
/xx{exch}bd

/rect {4 2 roll m 1 index 0 rl 0 xx rl neg 0 rl cp} bd

Then, a rectangle would be drawn like this:

0 0 100 100 rect sf

The cumbersomeness of this does make PostScript particularly hard to deal with. MetaPost may be a better fit if you your drawings are programmatically/mathematically generated. MetaPost generates encapsulated PostScript (which you can include in your LaTeX document) but it is more suitable for drawing images with algebraic definitions.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Yes, defining my own functions for shapes is what I've done. I just have a bad feeling I'm reinventing the wheel, since there must be thousands of different implementations of rectangles by now. It would make sense that someone would have packaged his own functions and made them available to others. Thanks for the tip on MetaPost. Seems quite interesting, led me to Asymptote which seems to be a modernized version of MetaPost. – pafcu Sep 11 '09 at 12:05
1

I like using matplotlib. It can generate both postscript and PDF directly, it's in python, and it can also do pretty sophisticated plots (hence its name). If you want to hack PostScript directly you'll be able to use psticks in LaTeX, but you'll need to run-trip everything through dvi2ps and then ps2pdf to make PDFs. Do you really want PostScript or PDFs? I think that you want PDFs, right?

vy32
  • 28,461
  • 37
  • 122
  • 246
  • Yes, PDFs are my final goal. The only reason I use PostScript is because it is a good combination of drawing and math. I'm not sure matplotlib is what I'm looking for, since what I want to do is draw figures which are only partially mathematically generated. I need boxes and arrows and possibly arbitrary shapes which might be hard to do with a library aimed for plotting functions? Also, Python code tends to be quite verbose. Nevertheless, I will take a closer look at matplotlib too. – pafcu Sep 11 '09 at 12:09
  • If you just need boxes and arrows and arbitrary shapes, you should use graphviz and get the automatic layout. – vy32 Sep 13 '09 at 01:32
  • I specifically need manual layout. Graphviz, as the name implies, is for graphs. I want to draw figures in general. – pafcu Sep 13 '09 at 12:21
1

OK, I've decided that Asymptote is the best thing since sliced bread. Handles drawing both graphs and arbitrary figures really well, and has a vast number of extension modules (including MetaPost compatibility if you care about that). Additionally it typesets text using LaTeX which is just incredibly cool. As an added bonus it even outputs directly to PDF (or EPS).

I still think it's a bit sad there's no good libraries of routines for good ol' PostScript though.

Community
  • 1
  • 1
pafcu
  • 7,808
  • 12
  • 42
  • 55
1

I have used Asymptote (for graphs though) but I found it tiresome to learn yet another custom language. If you're familiar with Python, you can give PyX a try. Its feature set is similar to that of Asymptote. For example, it can also use LaTeX for typesetting text/math.

Another option is Enthought Enable, but that is probably less suited.

Brecht Machiels
  • 3,181
  • 3
  • 25
  • 38
1

I've had good results constructing images directly in postscript. One helpful convention I've found is to treat objects like glyphs in a font. So each object expects the currentpoint to be set at, say, the bottom left corner, and leaves the currentpoint at the bottom right. The you can put them in an array and forall through it: each object leaves the currentpoint ready for the next one.

luser droog
  • 18,988
  • 3
  • 53
  • 105
0

There are lots of postscript libraries look here http://www.ericlindsay.com/computer/printing.htm and here http://www.tinaja.com/post01.shtml and here http://seit.unsw.adfa.edu.au/staff/sites/gfreeman/qs.html

Breton
  • 15,401
  • 3
  • 59
  • 76
0

Generate SVG, then use something like iText and/or Inkscape to programmatically convert to PDF/PS. I built a publishing stack this way and it worked out really nice.

Steven Kryskalla
  • 14,179
  • 2
  • 40
  • 42