# Changing the working directory when including files in a subdirectory

I'm working on a mathematics magazine that involves multiple articles from different authors. I would like to give each article a sub-directory in which I can store all the relevant files - .tex, image files, etc. However, when I then `\include` the article .tex file from the master file, the images won't be linked correctly without editing.

For example, suppose the article is stored in `articles/article1/` and includes an image matrix.png. In the article .tex file this would be inserted, normally, through `\includegraphics{matrix.png}`. However, when we move to including the article in the magazine through the master file, all these instances would have to be changed to `\includegraphics{articles/article1/matrix.png}`

So, is it possible to change the LaTeX working directory before including the article such that instances of `\includegraphics` will not have to be changed, but rather that LaTeX will know to search for these files in the article directory? I'm thinking that this would be similar-ish in functionality to HTML's base href command for links.

Or, is there a better way to achieve all this?

Try

``````\graphicspath{{articles/article1/}}
``````

before the `\include`.

• Also you can have several paths by separating with `\graphicspath{{article1}{article2}}`. – zeroth Mar 26 '12 at 10:56
• This is a perfect solution - thanks! I am designing a custom command to add articles that will take care of headering, titling, reseting the figure counter, etc. - and this fits perfectly into that. – James Fennell Mar 26 '12 at 18:54
• Note: on my installation I had to use `\graphicspath{{articles/article1/}}` to get this working -- I had to include the trailing / and had to have two braces like @zeroth has, even for only one directory. – James Fennell Mar 26 '12 at 19:46
• Same issue as @JamesFennell here. Would be great if Stephan could fix that in the answer. – Danilo Bargen Jan 16 '13 at 19:01
• @DaniloBargen done. Sorry fro not reacting immediately after James' comment. – Stephan Lehmke Jan 17 '13 at 7:13

If you want to include more than just graphics, you can also use the environment variable `TEXINPUTS` and refine it before calling your latex processor:

``````TEXINPUTS=.:graphics:articles/article1:articles/article2:../../something/somewhere/else:\$TEXINPUTS pdflatex main.tex
``````

The last \$TEXINPUTS call, although normally empty, is necessary otherwise the normal TeX paths disappear.

This will add all these paths so that `\imput{file}`, `\include{file}`, `\includegraphics{file}` and even `\usepackage{file}` will also search in these folders to find the appropriate file before giving the `file not found` error message.

• A problem with this solution from my perspective is that it would add complication to the process of adding new articles, and could also presumably result in conflicts if different articles have images with the same name. I am designing a special command for adding new articles, and Stephan Lehmke's solution works nicely within this. Still, I can see this being helpful in other situations - thanks! – James Fennell Mar 26 '12 at 18:53
• Is it possible to do same thing from preamble in `main.tex`? – Crowley May 14 '15 at 9:33
• @JamesFennell: In my opinion, files should be included not based on working directory. Includes should be relative to the original file that executes the include. It seems TeX people do not like this because some systems do not have the concept of "directory". Well, this is no excuse in my opinion... but... :-( – André Caldas Apr 2 at 17:29