3

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?

The Guy
  • 411

3 Answers3

5

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.

The Guy
  • 411
3

This is what externalize TikZ library does. Try to compile next code with command pdflatex --shell-escape levels (being levels the name of the file). As a result you'll get a pdf file with the whole document, but also another file called levels-figure0.pdf which contains the TikZ figure:

enter image description here

In fact you'll get as many levels-figureXXX.pdf files as tikzpictures you have in your .tex source. Next time you compile again your source file, all tikzpictures are not processed and replaced by corresponding .pdf results.

If any tikzpicture has been changed in the mean time, it's processed again, but only those which has been updated.

This default behavior can be changed introducing commands and options from externalize library whose information can be found in section "50 externalization library" (pp 597-610) from pgfmanual.

\documentclass[a4paper,10pt]{article}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\usepackage{lipsum}

\begin{document}%
\lipsum[1]

\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}

\lipsum[2]
\end{document}
Ignasi
  • 136,588
  • So the library can recognize if the figure code was changed before to compile it? If not, simply use the pdf figure created before; if yes, compile it again and create the pdf again? – Sigur May 14 '15 at 08:01
  • 1
    @Sigur: After better reading I think it remakes all of them by default. I'll confirm it later and correct my answer. – Ignasi May 14 '15 at 08:40
  • 1
    @Sigur: I've confirmed it. By default only edited figures are recompiled. Although you can change this behavior. – Ignasi May 14 '15 at 11:03
  • thanks for the attention. Indeed it is a very good tool. – Sigur May 14 '15 at 11:21
  • Can this be used to make eps figures too? – The Guy May 14 '15 at 17:36
  • 1
    @TheGuy Yes, you can use pdflatex which generates pdf files and convert them with pdftops -eps pdffile epsfile. Or use latex which produces .ps files for every figure. – Ignasi May 15 '15 at 09:41
  • @Ignasi I had to set \tikzexternalize[shell escape=-enable-write18] and call pdflatex -shell-escape test.tex for this to generate the figures. However, I cannot compile using TeXstudio, I have to manually do this. Is there a way to make this all work in TeXstudio. I cannot seem to figure it out from the manual. – The Guy May 15 '15 at 17:05
  • @TheGuy I've never used TeXstudio, but for sure that somewhere you can configure which commands it executes for compiling.Did you look in TeX.SX? – Ignasi May 15 '15 at 17:08
  • Tried it. No luck for me. Ugh.

    Anyway, I attempted to use pdftops and I get an error saying it is not a recognized command. Is this something I need to download separately?

    – The Guy May 15 '15 at 17:23
  • Apparently for windows, one needs to download pdftops.exe from (http://www.foolabs.com/xpdf/download.html) to get the functionality for making a pdf into an eps. – The Guy May 15 '15 at 18:33
  • 1
    @TheGuy I think these ones provide better answers than mine: http://tex.stackexchange.com/questions/91275/how-to-save-a-figure-produced-by-tikz-save-export-as-eps-file/91277#91277 or http://tex.stackexchange.com/questions/8641/export-eps-figures-from-tikz/8646#8646 – Ignasi May 16 '15 at 07:22
  • @Ignasi Ahah! Figured it out based on the first link you sent and the help you gave! Thanks a million! – The Guy May 18 '15 at 14:27
0

As an alternative to using \usetikzlibrary{external} if the document is stable and you will make few modifications to tikzpicture, it is better to handle it in separate files and include them as an image (or with \input{test-tkz.tex}). The ltximg script you can do the following:

$ ltximg  --eps --subenv --imgdir=mypics --prefix=tkz -o test-out test.tex

Now you will have a new document test-out.tex with the tikzpicture environments converted into images (preserving the consistency of the fonts) and you will also have each tikzpicture environment in separate files (in case you need some extra modification) and one file test-all.tex with (only) all tikzpicture.

First check the documentation to see the options and conditions that the input file must meet.