116

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.)

2
  • 10
    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
    Commented Feb 23, 2012 at 9:40
  • 2
    Thanks, but I don't want to skip the inclusion of these images entirely, I want to include a different (rasterized) version. Commented Feb 23, 2012 at 9:41

3 Answers 3

188
+100

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).

1
  • The solution with automatically converting between formats worked for me. It sped up the build process significantly! However, it adds a dependency to the conversion tool. For example I had to install the convert tool. On my Debian system, this is part of the imagemagick package. (apt-get install imagemagick).
    – cojocar
    Commented Feb 28, 2020 at 19:30
28

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...

2
  • 4
    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.
    – yo'
    Commented Nov 27, 2012 at 7:46
  • What about the alternative: Either \def\picsext{.png} or \def\picsext{.pdf} combined with \includegraphics[width=\textwidth]{spam\picsext}. Is there any reason why this approach would only work as a prefix (=directory) and not as a suffix (=explicit extension)?
    – bluenote10
    Commented Feb 16, 2015 at 16:10
16

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/* .

Additional option:

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

Additional option:

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

\graphicspath{{full_images/}}
\graphicspath{{prev_images/}}
3
  • 1
    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.
    – nickpapior
    Commented Apr 4, 2012 at 22:08
  • @zeroth I thought about such possibility, and I must claim that I prefer to have anything clear (which is not what you call transparent)---I don't like having files spread to multiple directories so that I have to change directory to find the true file name. I will add it as an option.
    – yo'
    Commented Apr 5, 2012 at 6:55
  • fair enough. I understand.
    – nickpapior
    Commented Apr 5, 2012 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .