Monday, January 19, 2015

The mysteries of postscript files

Last week I talked about saving figures from R.  I mentioned .eps files, a type of vector image that is sometimes requested as a format for figures in submitting manuscripts for publication.   Even without having ghostscript installed on a Windows machine, R will correctly save the image (at least it did on mine).  However, you can't view it.  Additionally, if you make an svg file and try to save it as a .eps file from Inkscape, it doesn't save the file correctly.  (My test file left off the axis labels, for example.)  So, here's how to view, and save (or print to) .eps files on a Windows 7 computer (I assume similar steps will work for other Windows computers).

Viewing .eps files (works well, do this!)

First, I downloaded GPL Ghostscript.  Choose the appropriate type (32-bit or 64-bit).  You can find out which you have by right-clicking on "Computer" in the start menu and selecting "Properties".  This detail is under the "System" section as "System type". I installed the program and then got GSView and installed it.  I tried opening a .eps file.  It worked!

Inkscape and .eps files (sometimes it works, sometimes it doesn't, so skip this step if you want)

Now, Inkscape.  I've always had mixed success saving files from there to .eps files.  Theoretically, this should work.  Sometimes it cuts off bits of the graph though.  If you want to try it, here's an example.  Just be warned I've never gotten it to completely work!  Open a .svg file in Inkscape (perhaps the one created last week).  I saved again, and it still cuts off the axis labels and axis numbering.  I need to link up the two programs according to the Inkscape forum.  In the Start Menu, click on "Computer".  In that window near the "System Properties".  Go to the "Advanced" tab and click on the "Environment Variables" button.  There are two lists of variables.  The top one is "User variables for [yourusername]".   Click "New" and name the new variable "GS_PROG" with value: "C:\Program Files\gs\gs9.15\bin\gswin32.exe"; navigate there to confirm it's where your file lives (or gswin64.exe if you have 64-bit windows).  Under "System variables" edit "PATH".  There are already a bunch of path values listed in there; DO NOT DELETE THEM.  At the end of the current list, place a semicolon, and add: "C:\Program Files\gs\gs9.15\bin;C:\Program Files\gs\gs9.15\lib", similar to how it's described in this tutorialThen click "OK" on both windows.  Now, start Inkscape.  It now recognizes .eps files as a type it can import.  However, I've never been able to get that to work (Inkscape just locks up).  So, close the import window and just open one of your .svg files.  You can then save it as a .pdf, open the .pdf in your usual pdf viewer, and then print it to a .eps file in the next section.

Printing as .eps (works well!)

You can print to .eps files using the regular print features in whatever program.  I followed these instructions (where to find "Add Printer" varies a bit depending on which version of Windows you have but it's pretty easy to find.  If you are having trouble just type "Add Printer" into the Start Menu search box.  Anyway, it worked beautifully.  I used an existing port ("FILE: print to file") as they said to.  With this .eps printer set up, I could take my .svg to .pdf in Inkscape, then print to .eps from the pdf viewer (I use Foxit Reader but this print to .eps should work in any program that prints).   I can't recall if you have to already have Ghostscript installed to make this work, but you'll need to view it to confirm, so you might as well get Ghostscript and GSView installed.

Summary of how I get to .eps files

Do you need to add anything to your file after using R to create your figure?  No?  Then use R and view them in GSView.  Easy!

If you need to add anything to your R plot, then you have two options.

(1) If you have Adobe Illustrator, save it as .eps, then open in Illustrator and edit it to your liking.

(2) Don't have Illustrator (like me)?   Save as a .svg from R, edit in Inkscape, then save as .eps (sometimes this works, sometimes it cuts off pieces of the figure and I haven't figured out why).  If the .eps from Inkscape is not good, save your .svg file instead as a .pdf.  Then open the .pdf in Adobe Acrobat Pro (just the reader won't work) and save as .eps.  Or, if you don't have Adobe Acrobat Pro, print as .eps from your pdf reader.  A few more steps, but still doable with all free programs (Ghostscript, GSView, Inkscape, and your pdf reader of choice) and pretty easy.

No comments:

Post a Comment

Comments and suggestions welcome.