[Tex/LaTex] Convert a picture or figure to EPS or PDF file

conversiondiagramsepsfloatspdf

In my document I'm embedding figures using the following code:

\begin{figure}[!t]
  \centering
  \def\svgwidth{1.2\columnwidth}
  \resizebox{\columnwidth}{!}{
    \endlinechar=255\relax
    \begin{picture}(1,0.63636365)
      \put(0,0){\includegraphics[width=\unitlength]{figure.eps}}
      \put(0.50454546,0.58636365){\color[rgb]{0,0,0}\makebox(0,0)[b]{\smash{some text $ax^2+bx+c=0$.}}}
    \end{picture}
  }
  \caption{Caption.}
  \label{fig:figure}
\end{figure}

Where the image (figure.eps) contains no text and all annotations are added on top of the image using \put commands.

This method works very well (annotations can include any LaTeX macros, which is extremely useful) as long as I'm sticking to TeX. Now, however, I'd like to take the resulting figures (preferably in a vector format) and put them in some other tool.

Questions:

  1. Is there any way of outputting these floats into PDF files with no margins, captions and with page dimensions equal to the specified float size? I imagine I'd have to somehow wrap each float in a .tex file with a custom preamble.
  2. Is it possible to automate this process so that if I want to dump all my N figures to PDFs I don't have to repeat the step (1) N times.

Best Answer

In general I recommend you to place all your complicated figures into separate files and use e.g. the standalone class to be able to compile them separatly and then \input them easily in the main document with the standalone package.

For your existing document you can use the preview package (which is used internally by standalone). It only compiles things wrapped in a preview environment and every such environment as a page. With the tightpage option you get tight pages as I requested.

I would use code like the following to redefine figure to use preview environments and to disable (or include) the \caption. The resulting PDF should have one picture per page. You can then use the page=<number> option of \includegraphics to include it or split it into several PDFs using tools like pdftk allfigures.pdf burst output figure%02d.pdf or Ghostscript.

\documentclass{article}

\usepackage[active,tightpage]{preview}

\makeatletter
\renewenvironment{figure}[1][]{%
    \begin{preview}%
        \renewcommand\caption[2][]{}% or
        %\def\@captype{figure}% If you want to include the caption
}{%
    \end{preview}%
}
\makeatother

\begin{document}

text text text text 

\begin{figure}[!t]
    \rule{5cm}{8cm}% dummy replacement
    \caption{some caption}
\end{figure}

text text text text 

\begin{figure}[!t]
    \rule{8cm}{5cm}% dummy replacement
    \caption{some caption}
\end{figure}

text text text text 


\end{document}