[Tex/LaTex] Convert TikZ to EPS using pdfLaTeX

epsmiktexpdftextexstudiotikz-pgf

I would like to convert my TikZ images to EPS format and save in a separate file while pdfLaTeX compiles my document, ideally. Note I am using MikTeX and TeXstudio.

Otherwise, I would like to be able to convert TikZ pictures into EPS from a LaTeX file using pdfLaTeX.

I've tried the method found here but the pictures do not work entirely correctly. There is some cutoff (missing parts of the image), plus it is a pain to convert each figure this way.

Example code below.

test.text

\documentclass[a4paper,10pt]{article}

\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{arrows, calc, decorations.pathmorphing, shapes}

\pgfrealjobname{test}


\begin{document}
    \beginpgfgraphicnamed{levels}
    \begin{tikzpicture}[scale=1, font=\normalsize, every node/.append style={transform shape}]
        \node[rectangle, draw, minimum width=1.5em, minimum height=1.5em, line width=0.05em] (n1) at (0,0) {1};
        \node[rectangle, draw, minimum width=1.5em, minimum height=1.5em, line width=0.05em] (n2) at (2,0) {2};     
    \end{tikzpicture}
    \endpgfgraphicnamed
\end{document}

In the command prompt:

latex --jobname=levels test.tex
dvips -o levels.eps levels.dvi

Any ideas?

Best Answer

Based on @Ignasi answer and the link he posted about using an editor with PdfLaTeX, the solution is as follows:

From foolabs.com download xpdf and unpack to a location on your computer.

In Computer Properties Go to Advanced system settings -> Environment Variables and add the path of your particular xpdfbin\bin to the path. This will enable Windows to find the folder and use the executable pdftops on the command line.

In TeXstudio Go to Options -> Commands and change the PdfLaTeX line to this:

pdflatex.exe --shell-escape -synctex=1 -interaction=nonstopmode %.tex

This will tell TeXstudio to use the above on the command line when you hit the compile button for a file.

Finally, in your LaTeX Document, add the following to the preamble:

\usetikzlibrary{external}

\tikzset{external/system call={pdflatex \tikzexternalcheckshellescape 
                                        -halt-on-error
                                        -interaction=batchmode 
                                        -jobname "\image" "\texsource"
                                        && pdftops -eps "\image.pdf"}}
\tikzexternalize[shell escape=-enable-write18]

Then you can compile to your heart's desire as PdfLaTeX will generate pdf and eps versions of all your Tikz drawings!

Other info for a pertinent LaTeX build and how to do it for your particular case may be found in either of these two links suggested by @Ignasi:

  1. how-to-save-a-figure-produced-by-tikz-save-export-as-eps-file
  2. export-eps-figures-from-tikz

Also, this technique should work using the other tools for Pdf conversion in the xpdf package such as pdftopng.