views:

301

answers:

4

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?

+3  A: 

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
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
A: 

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
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
If you just need boxes and arrows and arbitrary shapes, you should use graphviz and get the automatic layout.
vy32
I specifically need manual layout. Graphviz, as the name implies, is for graphs. I want to draw figures in general.
pafcu
A: 

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.

pafcu
A: 

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.

lost-theory