Advanced LaTeX image topics

Commonly used LaTeX units and lengths

When including images using the \includegraphics command, it is sometimes required to provide measurements and length when adjusting the size or positioning. The table below summarizes some of the most frequently used LaTeX units and lengths.

unit or length
Definition

pt

the default length unit, about 0.3515mm

mm

a millimeter

cm

a centimeter

in

an inch

ex

the height of an x in the current font

em

the width of an m in the current font

\columnsep

distance between columns

\columnwidth

width of the column

\linewidth

width of the line in the current environment

\paperwidth

width of the page

\paperheight

height of the page

\textwidth

width of the text

\textheight

height of the text

\unitlength

units of length in the picture environment

Compilers and image types

latex

When compiling with latex, you can only use EPS images, which are a vector format.

pdflatex

If you are compiling using "pdflatex" to produce a PDF, then you can use a number of image formats:

JPG: Best choice to insert photos
PNG: Best choice to insert diagrams (if a vector version could not be generated) and screenshots
PDF: Even though we are used to seeing PDF documents, a PDF can also store images 
EPS: EPS images can be included using the epstopdf package (you just need to install the package, you 
        don't need to use \usepackage{} to include it in your document.)

Vector format or bitmap format?

Images can be in either vector format or bitmap format. Generally you don't need to worry about it, but if you do happen to know the format the image is in, you can use that information to choose an appropriate image format to include in your LaTeX document. If you have an image in vector format, you should go for PDF or EPS. If you have it in bitmap format, you should go for JPG or PNG, as storing bitmap pictures in PDF or EPS takes a lot of disk space.

Open an images example in Overleaf

Providing high-res and low-res images

So far while specifying the image file name 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 still in progress), 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 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 base name but different extensions (e.g., venndiagram.pdf and venndiagram.png), then the .png version will be used first, and in its absence the .pdf version will be used. This is also a good idea if some low-resolution versions are not available.

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}

Improving on the technique described in the previous paragraphs, we can also instruct LaTeX to generate low-resolution .png versions of images on the fly while compiling the document 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.

There are some important things to keep in mind though:

  • For the automatic conversion to work, you need to call pdflatex with the --shell-escape option.

  • For the final production version, you must comment out the \epstopdfDeclareGraphicsRule, so that only high-resolution PDF files are loaded. You'll also need to change the order of precedence.

Open an images example in Overleaf

Further reading

For more information see:

Last updated

Was this helpful?