Zum Inhalt springen

Switching between high-resolution and low-resolution versions of Images

So far, while specifying the image filename in the \includegraphics command, we have omitted file extensions. However, that is not necessary, though it is often useful. If the file extension is omitted, LaTeX will search for any supported image format in that directory, and will search for various extensions in the default order (which can be modified).

This is useful in switching between development and production environments. In a development environment (when the article/report/book is being developed), it is desirable to use low-resolution versions of images (typically in .png format) for fast compilation of the preview. In the production environment (when the final version of the article/report/book in development is produced), it is desirable to include the high-resolution version of the images.

This is accomplished by

  • Not specifying the file extension in the \includegraphics command, and
  • Specifying the desired extension in the preamble.

Thus, if we have two versions of an image, venndiagram.pdf (high-resolution) and venndiagram.png (low-resolution), then we can include the following line in the preamble to use the .png version while developing the report -

  \DeclareGraphicsExtensions{.png,.pdf}

The command above will ensure that if two files are encountered with the same basename but different extensions (for example venndiagram.pdf and venndiagram.png), then the .png version will be used first, and in its absence the .pdf version will be used (low-resolution versions might not have been generated for some of the images).

Once the report has been developed, to use the high-resolution .pdf version, we can change the line in the preamble specifying the extension search order to -

  \DeclareGraphicsExtensions{.pdf,.png}

Converting High-Resolution Images to Low-Resolution Automatically

Improving on the technique described in the last section, we can also instruct LaTeX to generate low-resolution .png versions of images on the fly during the LaTeX run if there is a PDF that has not been converted to PNG yet. To achieve that, we can include the following in the preamble after \usepackage{graphicx} -

  \usepackage{epstopdf}
  \epstopdfDeclareGraphicsRule{.pdf}{png}{.png}{convert #1 \OutputFile}
  \DeclareGraphicsExtensions{.png,.pdf}

If venndiagram2.pdf exists but not venndiagram2.png, the file venndiagram2-pdf-converted-to.png will be created and loaded in its place. The command convert #1 is responsible for the conversion and additional parameters may be passed between convert and #1. For example - convert -density 100 #1.

Important Notes:

  • For the automatic conversion to work, we need to call pdflatex with the --shell-escape option.
  • For the final production version, we should comment out the \epstopdfDeclareGraphicsRule, so that only high-resolution PDF files should be loaded. We'll also need to change the order of precedence for extensions in the lines below.

Managing the folderpath to images

It may be useful to arrange all the images inside a directory-structure

  • when a document uses a lot of images, or
  • when we want to use the same image in multiple documents.

In such a case, we can tell LaTeX where to look for images using the command \graphicspath, which takes one argument specifying the additional paths we want to be searched when the \includegraphics command is used. Here are some examples:

  \graphicspath{ {/home/mike/images/} }
  \graphicspath{ {first_folder/}{second_folder/}{third_folder/} }
  \graphicspath{ {./images/} }
  \graphicspath{ {c:\mypict~1\images} }
  \graphicspath{ {c:/mypict~1/images/} } % works with Windows OS

Notes

  • Trailing / is required in the folder paths specified
  • { {first_folder/}{second_folder/}{third_folder/} } is one argument, not three.
  • In the fourth and fifth examples above, the "safe" (MS-DOS) form of the Windows MyPictures folder is used (because spaces in file path may create problem).

In the third example shown above, there should be a directory named "images" in the same directory as the main tex file. This is called RELATIVE addressing (the first, fourth and fifth example represent ABSOLUTE addressing). For more details, please refer to http://www.ctan.org/tex-archive/macros/latex/required/graphics/grfguide.pdf .

Using ABSOLUTE paths makes the file less portable between different systems (imagine having to recreate the same folder structure in all work-stations). Whenever possible, the use of RELATIVE addressing is recommended, particularly when the images are contained in a directory under the current directory. In particular, use only RELATIVE addressing in Overleaf projects.

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX