# Does tikz 2.1 no longer work with the preview package?

I used to be able to create png images of diagrams made with tikz code by using the preview package and printing the dvi output from dvips to a png file using ghostscript. The latest version of tikz seems to have broken this ability in some way.

If I use the following latex code:

\documentclass{article}
\usepackage{tikz}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}
\end{document}


I then run Latex to get the dvi file, and dvips to convert to postscript. I then run ghostscript to produce the png with the following command:

gswin32c.exe -sDEVICE=png16m -dTextAlphaBits=4 -r300 -dGraphicsAlphaBits=4 -dSAFER -q -dNOPAUSE -sOutputFile="tikz_preview_png_problem.png" -f "tikz_preview_png_problem.ps"


Previous to tikz 2.1 this worked fine, but now produces the following output:

Error: /undefined in pgfo
Operand stack:

Execution stack:
%interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval-
-   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   fa
lse   1   %stopped_push   1878   1   3   %oparray_pop   1877   1   3   %oparray_
pop   1861   1   3   %oparray_pop   1755   1   3   %oparray_pop   --nostringval-
-   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringv
al--   2   %stopped_push   --nostringval--
Dictionary stack:
--dict:1161/1684(ro)(G)--   --dict:0/20(G)--   --dict:74/200(L)--   --dict:95
/300(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 9094
GPL Ghostscript 8.71: Unrecoverable error, exit code 1


Is there any way to get around this, it's a pain in the bum as I liked to be able to produce high res png of my vector graphics for use in non-latex docs or websites etc. I'm using Miktex on windows with Texniccenter as my editor in case it is relevant.

-
Why are you not just using pdflatex instead? It works fine and also allows you to convert to PNG. Have a look at the standalone class which is helpful here. The upcoming version will have an option to convert the output to a PNG automatically. –  Martin Scharrer May 1 '11 at 11:52
Thanks, I'll look into the standalone package as an alternative, I wasn't aware of it. –  crobar May 1 '11 at 14:09
@crobar: The standalone class uses the preview package internally, so it just reduces the code overhead, but won't save you from the ghostscript error. –  Martin Scharrer May 1 '11 at 14:58
Tikz has nothing to do with this error. I think the problem comes from preview and ghostscript. On OS X, I can compile with dvips but with the Apple distiller, if the distiller is ghostscript I have the same problem with the stack. –  Alain Matthes May 1 '11 at 15:33
@Altermundus: TikZ could have something to do with the error. If I swap tikzpicture for tabular or even a PSTricks pspicture everything rolls along smoothly. –  Sharpie May 1 '11 at 17:12

I poked around a bit and I couldn't figure out what is going wrong with the Postscript pipeline. My preferred method of generating standalone images uses the PDF pipeline along with ImageMagick's convert utility. The workflow is considerably simpler:

pdflatex foo.tex
convert -density 300 foo.pdf foo.png


Windows binaries can be found here, but there are two caveats:

• You will need gswin32c.exe on your PATH as convert requires GhostScript to get images out of PDF or PS format.

• convert.exe will have to be before Windows binaries on the PATH as there is a system32 tool also called convert.exe that shares the same name. Or you will have to rename convert.exe to something like imconvert.exe.

The Rapid Environment Editor is a nifty tool that can help you sort the Windows PATH.

## Update

It appears there was indeed a change in TikZ 2.10 that broke compatibility with Preview on the Postscript side. There are two prior bug reports that reference this problem and a post on the PGF mailing list that pins the problem down to a line in pgfsys-dvips.def.

## Update 2

Since the problem is with TikZ producing broken Postscript and not with GhostScript, then gswin32c should be able to perform the conversion on a PDF file:

pdflatex foo.tex
gswin32c -sDEVICE=png16m -dTextAlphaBits=4 -r300 -dGraphicsAlphaBits=4 -dSAFER -q -dNOPAUSE -dBATCH -sOutputFile=foo.png foo.pdf


This would spare the installation and setup of ImageMagick.

-
Thanks, I think this does in fact answer my question, so I'll mark it as the answer. I will look into your suggested workaround, but as I work on multiple computers would prefer something easier to implement. –  crobar May 1 '11 at 19:30
@crobar It is possible to use your GhostScript command line on a PDF file instead of a PS file. I just instinctively reach for ImageMagick because I always have it installed and the required command is a lot simpler. –  Sharpie May 1 '11 at 20:04