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}
Mika Lanzky
It works for spaces in path-names, but what about underscores? Double quotes doesn’t seem to solve this problem. Is it just not advisable to use underscore at all in neither path-names nor file-names when dealing with LaTeX?
tom
Hi Mika,
Thanks for your comment. Can you provide a minimal working example to give me an idea of what your path-/file-name looks like? I can include figures with underscores in their file-name. But I remember this was a problem with earlier versions of TeX.
Thanks,
Tom
sammichaelstevens
Thank you for the post – an extremely clear explanation.
tom
Great to hear. Thanks, Tom.
Beginner
Dear Tom,
I succeeded using pictures from a folder I made in the directory in Latex named “pictures” by writing \includegraphics{pictures/testingpicture.png}. Hence I did not need the first slash “/” for the directory. Maybe I misunderstood that part
Thanks once again.
Hoan
Hello
Thank you alot.
I think in
1
\includegraphics{/figures/filename}
It should be
1
\includegraphics{figures/filename}
without / after {
tom
Hi Hoan,
Thanks for your comment. I updated the post.
Best, Tom