# Multiple inclusion of the same image gives huge PS file

I am using beamer and having included the same image many times with pgf, declare once and insert.

It looks like the PS file from dvips grow linear as I create slides (the image is on the background). Reading online this should not be a problem on Unix systems, but the on windows it might be an issue.

Anyone have a solution as compiling takes around 1min now and the PS file is 200mb, not that handy?

How its used currently:

\pgfdeclareimage{redtree}{images/red_tree}%
\setbeamertemplate{background}{%
\pgftext[at=\pgfpoint{30mm}{\framesep-0.22\framesep},base,base]{\pgfuseimage{redtree}}
}

• The PGF manual is not completely clear on this, but my inference from the way it is phrased is that the reuse of an existing image only makes a difference to file size for PDF output. As you're using PS, it may be that the image is reincluded for every use. – Loop Space May 7 '12 at 8:40
• Is it a vector image (original EPS) or a raster image (which might have been converted to EPS)? AFAIK there is a way to define a PostScript macro which is reused every time. Is there a specific reason why you are using PS and not PDF? With PDF it is not an issue. – Martin Scharrer May 7 '12 at 8:45
• I am using PDF, but first dvips+ps2pdf. I just dont want that large ps file at every compile point for other reasons, speed, google drive uploads it every time ect. The pdf file is way smaller 20mb. – Poul K. Sørensen May 7 '12 at 9:10
• I think @AndrewStacey is correct that it somehow includes the image for every use, but as I am showing in my question, first declare and then use, i dont know why it does this. – Poul K. Sørensen May 7 '12 at 9:11
• The original eps image are a vector object. – Poul K. Sørensen May 7 '12 at 9:12

The problem with including the same PostScript graphics multiple times is very well explained in chapter "16 Including An EPS File Multiple Times" of

Keith Reckdahl, "Using Imported Graphics in LaTeX and pdfLaTeX", 2006, URL: http://mirror.ctan.org/info/epslatex.pdf

The trick is that the original PostScript file is split in two parts. A header file for dvips loads the graphics data somehow in PostScript's memory and the actual PostScript image file is a small one that only loads the previously stored graphics file. The second file again gets included multiple times, but the file size is smaller, because the larger graphics part is only written once.

That makes a small PostScript size. But the PDF file is probably large, because the PDF generating application would have to recognize that the same graphics is included multiple times.

The reusing feature of PDF works with "form XObjects", they can be generated via pdfmark operators. The downside is that PostScript viewers are not too happy with this. The pictures that are loaded as XObject are not shown. However the PDF file will contain the picture only once and the file size is small.

Unlike pdftex and xetex, the dvips back-end does not detect multiple inclusion of the same file on its own, and the \special{psfile=...} is inserted over and over again into the DVI file, leading to code replication of the multiply embedded image file in the intermediate PostScript and finally in the target PDF.

This can be remedied, as pointed out by Heiko Oberdiek, by creating a PDF Form XObject from the image once during the ps2pdf conversion step and insert references at the places where it is to be repeated. This has been implemented in the xsavebox package. The intermediate PS (small in size) does not show the repeated image, but PS converted back (big again) from the PDF displays them all correctly.

Using http://mirrors.ctan.org/graphics/pstricks/base/doc/images/tiger.eps (78,815 Byte)

--> PS: 241,428 B ; 228,002 Byte of target PDF:

\pdfoutput=0
\documentclass[multido=true]{standalone}

\usepackage{xsavebox} %embed content only once
\usepackage{graphicx}
\usepackage{multido}

\parindent=0pt

\begin{document}

\xsavebox{Tiger}{\includegraphics[width=5in]{tiger}}%
\multido{}{400}{\xusebox{Tiger}}
%\multido{}{400}{\includegraphics[width=5in]{tiger}}

\end{document}


while

\multido{}{400}{\includegraphics[width=5in]{tiger}}


--> PS: 31,676,394 B ; 13,063,945 B of target PDF