# pdfLaTeX with PSTricks: is it possible to get one .pdf for each figure?

I know that it is possible to use pdfLaTeX in a document with PSTricks figures by adding

\usepackage[pdf]{pstricks}


in the preamble and then running

pdflatex --shell-escape <file>


Then, the necessary PDF figures are created on the fly and stored in a common <file>-pics.pdf.

Now, my question is whether it would be possible to do the same but with each figure stored in a separate .pdf, instead of all of them in the common <file>-pics.pdf.

With the command-line PDF Toolkit you can burst the PDF into single-page outputs:

burst

Splits a single input PDF document into individual pages. Also creates a report named doc_data.txt which is the same as the output from dump_data. If the output section is omitted, then PDF pages are named: pg_%04d.pdf, e.g.: pg_0001.pdf, pg_0002.pdf, etc. To name these pages yourself, supply a printf-styled format string in the output section. For example, if you want pages named: page_01.pdf, page_02.pdf, etc., pass output page_%02d.pdf to pdftk.

dump_data

Reads a single input PDF file and reports its metadata, bookmarks (a/k/a outlines), page metrics (media, rotation and labels) and other data to the given output filename or (if no output is given) to stdout. Non-ASCII characters are encoded as XML numerical entities. Does not create a new PDF.

So you would use

pdftk <file>-pics.pdf burst


(with an appropriately-named <file>) to obtain as output pg_<num>.pdf for each image <num>.

Use the following code to extract each page and save it in a separate PDF file.

The steps are:

• Change \def\filename{<filename>-pics}.
• Compile with pdflatex -shell-escape extractor.
% extractor.tex
\documentclass{article}
\usepackage{filecontents}

\begin{filecontents*}{template.tex}
\documentclass[preview]{standalone}
\usepackage{graphicx}
\begin{document}
\includegraphics[page=\Page]{\Filename}
\end{document}
\end{filecontents*}

\usepackage{multido}
\def\dojob#1#2{\immediate\write18{pdflatex -jobname=#1-#2 "\def\noexpand\Page{#2}\def\noexpand\Filename{#1} \noexpand\input{template}"}}

\def\filename{<filename>-pics}% you just need to change this !
\pdfximage{\filename.pdf}

\begin{document}
\multido{\i=1+1}{\the\pdflastximagepages}{\dojob{\filename}{\i}}

There are \the\pdflastximagepages\ file(s) generated.
\end{document}


## Simulation

Compile with pdflatex -shell-escape main. You will have each page from main-pics.pdf saved in a separate PDF.

% Main.tex
\documentclass{article}
\usepackage{filecontents}

\begin{filecontents*}{template.tex}
\documentclass[preview]{standalone}
\usepackage{graphicx}
\begin{document}
\includegraphics[page=\Page]{\Filename}
\end{document}
\end{filecontents*}

\usepackage{multido}
\def\dojob#1#2{\immediate\write18{pdflatex -jobname=#1-#2 "\def\noexpand\Page{#2}\def\noexpand\Filename{#1} \noexpand\input{template}"}}
\def\Separate#1{\pdfximage{#1.pdf}\multido{\i=1+1}{\the\pdflastximagepages}{\dojob{#1}{\i}}}

\usepackage[pdf]{pstricks}

\begin{document}
\section{Circle}
\begin{pspicture}[showgrid](3,4)
\pscircle{2}
\end{pspicture}

\section{Rectangle}
\begin{pspicture}[showgrid](3,4)
\psframe(3,4)
\end{pspicture}

\section{Separating PDF\ldots}
\Separate{main-pics}
There are \the\pdflastximagepages\ file(s) generated.
\end{document}


run the document with

pst2pdf <file>


and it will produce a complete document and also a single pdf for every pspicture enviroment in a subdirectory. For Windows it needs only an installed Perl like Strawbery Perl or the one from http://www.activestate.com/perl. For Linux Perl is present by default :-)