LaTeX – how to programmatically change the path of your figures

Scenario: You want to programmatically define where your figures are in your latex document without going through and manually editing all your paths. You have a folder called "figs" that contains all your figures, but this folder might move.

Here’s how to programmatically change the path of where your images are located

Or more specifically programmatically change where the "figs" folder is located

The basic idea: You can define a variable in latex that stores the path to your images and then include this variable when you set your image path.


For more info on variables in latex, see one of the lower answers here…

Step 1: Define a variable in the preamble of your laTeX document.

Note apparently this behaviour is not really defined. You might overwrite/clobber another variable, so I suggest you name your variable to something that’s probably unique to you. I chose to base it on my last-name "\kawFigPath".

For example, if my figure folder "fig" is up one directory from where my current document is located, I would put this in the latex preamble.

\def \kawFigPath {../}
\begin{document}

Note that you would change the {../} part to go wherever your figures are located.

Step 2: In the body of my latex document, add this variable \kawFigPath right before where your figures are located.

Note the space between the \kawFigPath and the "figs" folder.

\begin{figure}
\centering
  \includegraphics[width=1\linewidth]{\kawFigPath figs/myFig}
  \caption{Blah blah.}
  \label{fig:myFig}
\end{figure}

And that’s it! If the location where you store the "figs" folder changes, you simply change \kawFigPath to point to the appropriate location.

Leave a Reply