LaTeX code for including images in your document

In these instructions, it is assumed that your image has already been uploaded to your Overleaf project.

This page explains how to include images in the most common formats, how to shrink, enlarge and rotate them, and how to reference them within your document. We will start with an example to demonstrate how to import a picture.

\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {./images/} }

\begin{document}
The universe is immense and it seems to be homogeneous, 
in a large scale, everywhere we look at.

\includegraphics{universe}

There's a picture of a galaxy above
\end{document}
Example of inserting an image

LaTeX cannot manage images by itself, so we need to use the graphicx package. To use it, we include the following line in the preamble: \usepackage{graphicx}.

The command \graphicspath{ {./images/} } tells LaTeX that the images are kept in a folder named images under the directory of the main document.

The \includegraphics{universe} command is the one that actually includes the image in the document. Here universe is the name of the file containing the image without the extension, then universe.PNG becomes universe. The file name of the image should not contain white spaces nor multiple dots.

Open an images example in Overleaf

The folder path to images

When working on a document which includes several images, it's possible to keep those images in one or more separated folders so that your project is more organized.

The command \graphicspath{ {images/} } tells LaTeX to look in the images folder. The path is relative to the current working directory—so, the compiler will look for the file in the same folder as the code where the image is included. The path to the folder is relative by default, if there is no initial directory specified, for instance

%Path relative to the .tex file containing the \includegraphics command
\graphicspath{ {images/} }

This is a typically straightforward way to reach the graphics folder within a file tree, but can leads to complications when .tex files within folders are included in the main .tex file. Then, the compiler may end up looking for the images folder in the wrong place. Thus, it is best practice to specify the graphics path to be relative to the main .tex file, denoting the main .tex file directory as ./ , for instance:

%Path relative to the main .tex file 
\graphicspath{ {./images/} }

as in the introduction.

The path can also be absolute, if the exact location of the file on your system is specified. For example, if you were working on a local LaTeX installation on your own computer:

%Path in Windows format:
\graphicspath{ {c:/user/images/} }

%Path in Unix-like (Linux, Mac OS) format
\graphicspath{ {/home/user/images/} }

Notice that this command requires a trailing slash / and that the path is in between double braces.

You can also set multiple paths if the images are saved in more than one folder. For instance, if there are two folders named images1 and images2, use the command:

\graphicspath{ {./images1/}{./images2/} }

Open an images example in Overleaf

For additional information on some of the options that are available for the \includegraphics command, see Advanced LaTeX image topics.

Last updated

Was this helpful?