# Choosing whether to include PDF or PNG in PDFLaTeX

Can I tell graphicx somehow which graphic format to choose when I omit the file extension in includegraphics and both a PDF and a PNG version of the same image exist? The reason is that I have some very large PDF vector graphics (in terms of file size/drawing primitives) that take a pretty long time to load each time I view the document. I would like to convert them to PNGs while working on the document and only use the PDF versions in the final document. Therefore, a global option that I can switch in the end would be great.

An additional plus would be to do the conversion to PNG on the fly during the LaTeX run if there is a PDF that has not been converted to PNG yet, if a tool for that exists. (I know how to use convert, but not how to include it automatically in LaTeX.)

-
A cheap solution is to load graphicx with the draft option, that avoids the inclusion of the graphic files, while setting a box with the correct dimensions. –  egreg Feb 23 '12 at 9:40
Thanks, but I don't want to skip the inclusion of these images entirely, I want to include a different (rasterized) version. –  Jan Pöschko Feb 23 '12 at 9:41

The order of preference when files with the same name and different extensions is

.png .pdf .jpg .mps .jpeg .jbig2 .jb2 .PNG .PDF .JPG .JPEG .JBIG2 .JB2


which is stored in the macro \Gin@extensions. So if you have both image.png and image.pdf, pdflatex will load the former.

If you are mixing case in extensions, then

\DeclareGraphicsExtensions{%
.png,.PNG,%
.pdf,.PDF,%
.jpg,.mps,.jpeg,.jbig2,.jb2,.JPG,.JPEG,.JBIG2,.JB2}


will ensure that PNG are always preferred over PDF files. For the final version it will be sufficient to switch the two lines.

A handier way, suggested by Heiko Oberdiek, is to use the package grfext:

\usepackage{grfext}
\PrependGraphicsExtensions*{.png,.PNG}


that will have the same effect without the need to check in pdftex.def for the list of extensions.

If you want also automatic conversion, you can say

\usepackage{graphicx}
\usepackage{epstopdf}
\epstopdfDeclareGraphicsRule{.pdf}{png}{.png}{convert #1 \OutputFile}
\DeclareGraphicsExtensions{%
.png,.PNG,%
.pdf,.PDF,%
.jpg,.mps,.jpeg,.jbig2,.jb2,.JPG,.JPEG,.JBIG2,.JB2}


When image.pdf exists but not image.png, the file image-pdf-converted-to.png will be created and loaded in its place. Add the options you prefer between convert and #1 (for example -density 100 or something like that).

You need to call pdflatex with the --shell-escape option for this automatic conversion to work. Of course you'll comment out the \epstopdfDeclareGraphicsRule command for the final version, when only PDF files should be loaded (and switch the order of precedence in the lines below).

-
Great tip on the automatic conversion. –  recluze Feb 29 '12 at 1:47

A pragmatic solution would be to just have two subdirectories with illustrations, pics and draftpics, where in draftpics you store the low-res png's. If you use a variable like:

\def \picsdir {draftpics}


You can refer to this variable when including them using includegraphics:

\includegraphics[width=\textwidth]{\picsdir/spam}


At the end you only need to change the variable to get the final version.

ps I submitted hi-res png's (>300 dpi) to journals and they where accepted without any problem. And hi-res png's still load pretty quickly. So I think you might not need the pdf's...

-
Yes, 300dpi would be fine with me (I'm a journal graphic editor), but when I'm forced to do a conversion to bitmap myself, I do it at 600dpi usually. –  tohecz Nov 27 '12 at 7:46

Me personally, I make a poor copy of the .pdf graphics and use these, just when I'm finished I switch to the high-quality ones. I don't know whether there's an automated LaTeX gadget for that.

My solution in bash (Unix systems) I have the full .pdf images stored in sub-directory full_images. Using the following script I convert them to preview quality:

#!/bin/bash

mkdir -p prev_images

for f in ./full_images/*;
do
echo $f g=${f#./full_}
convert -density 92 $f ./prev_$g
done


I then use one of the following commands to move the correct images to the root folder of the document:

cp -v ./prev_images/* .
cp -v ./full_images/* .


Instead of using cp (copy) in the last step, you can use ln -s (symbolic link) and save some disk space, not having the images twice. The command is one of the following:

ln -s full_images images
ln -s prev_images images


This creates a directory link, which you have to make searchable by \includegraphics, so you ought to put the following into your preamble:

\graphicspath{{images}}


Change the \graphicspath directly, i.e. having two lines with one un-commented and do not speficy the folder:
\graphicspath{{full_images/}}

just a remark as to how this could be done transparently. By the use of ln to the folders you can quick switch without having to copy anything. So simply: ln -s full_images images and adding that to the graphics path. \graphicspath{{images}}. It then only means that you can do: ln -fs prev_images images ot switch. Just an idea of improvement. If you don't want to add the graphics path you can ln the files in folder: images. –  zeroth Apr 4 '12 at 22:08