Site icon texblog

The Path to your Figures

Most people prefer to not save their figures in the same directory as the tex file itself since it would clutter up quickly. A common solution is to save all figures in a sub-folder of the main directory and use

\includegraphics{figures/filename}

instead of

\includegraphics{filename}

to import them (to use \includegraphics you need the graphicx package). Personally, I often store my figures in a sub-folder of a common parent folder or in a completely different directory depending on the project I am working on. If the figures are saved in a sibling directory, we can use ../ to access the parent directory.

\includegraphics{../figures/filename}

It can also be used multiple times to go up several folders.

\includegraphics{../../figures/filename}

Alternatively, it is also possible to specify the entire path of the figure.

\includegraphics{/Users/me/Documents/project/figures/filename}

Defining the graphicspath

If your document has many figures it can become tedious to always add the path or it can make your document look messy. With the command \graphicspath the graphicx package allows to specify one or several paths in which to search for figures. The default setting for this path is the folder of the tex file.

\usepackage{graphicx} %Loading the package
\graphicspath{{figures/}} %Setting the graphicspath
...
\begin{document}
\includegraphics{filename} %Importing a figure
\end{document}

We can also define several directories to be searched for figures.

\graphicspath{{figures/}{../figures/}{C:/Users/me/Documents/project/figures/}}

Each path has to end with a / and be enclosed in curly braces { } even if only one path is specified. Only the specified directories will be searched for figures and not their subdirectories. To include the subdirectories, they have to be specified separately as their own paths. Alternatively, you can add the subdirectories again manually with each figure. Accessing the subdirectories of paths specified by \graphicspath is much easier now. When you are using \graphicspath you can still define the path of figures individually.

\usepackage{graphicx} %Loading the package
\graphicspath{{figures/}} %Setting the graphicspath
...
\begin{document}
\includegraphics{filename1} %Importing a figure
\includegraphics{topic1/filename1} %Importing a figure from a subdirectory of the graphicspath
\includegraphics{/Users/me/Documents/project/photos/filename} %Importing a figure from a non-graphicspath
\end{document}

It is important to keep in mind that problems can occur when several files with the same name exist in the different paths. The order of how the paths were specified will define which file will be imported in such a case (the file appearing in the earliest defined path).

Directory path names with spaces

While we all know that we should avoid having directories with spaces in their names, it still happens. The biggest culprit on my system is “Google Drive”. We have basically two options: (i) adding double quotes " " around each path or (ii) using the space option of the grffile package.

Option 1

If you are adding the path name manually with each figure, you can just add double quotes " " around the path.

\includegraphics{"/Users/me/Google Drive/project/figures/filename"}

If you are specifing the filename extension, you need to use an extra set of curly braces { } instead of the double quotes " " around the path.

\includegraphics{{/Users/me/Google Drive/project/figures/filename.png}}

To define a \graphicspath with spaces, we need to put double quotes " " around the path.

\graphicspath{{"/Users/me/Google Drive/project/figures/"}}
Option 2

Defining a \graphicspath (or regular path) with spaces can be done with the space option of the grffile package.

\usepackage{graphicx} %Loading the package
\usepackage[space]{grffile} %Loading the package
\graphicspath{{/Users/me/Google Drive/project/figures/}} %Setting the graphicspath
...
\begin{document}
\includegraphics{filename} %Importing a figure
\includegraphics{/Users/me/Google Drive/project/photos/filename} %Importing a figure from a non-graphicspath
\end{document}
Exit mobile version