# Using relative paths for \include commands

I have a top-level latex file: /thesis.tex

And subfolders with chapters: /chap1/ch1.tex

In thesis.tex I use \include commands like so:

\include{chap1/ch1}


And in ch1.tex I have more includes for the sections of the chapter:

\include{introduction}


The ch1.tex file and all its included sections use relative paths as above (this was a standalone document that is now a chapter of the larger thesis.tex doc). I realize I have to fix all the paths in the include statements for ch1.tex and its sections to respect the main document directory as the root:

\include{chap1/introduction}


Is there another way? Do I have to go through and change all the include and figure references in this way, or is there a way I can tell all such links to use relative paths within ch1?

• You have chap1 in one place, but then mention ch1 in another. Also, a leading slash denotes an absolete path. Is your thesis.tex really in /? :-) – Andrey Vihrov Aug 12 '11 at 17:04
• I'm not sure but maybe the import package is what you need. – Martin Scharrer Aug 12 '11 at 19:10

## 3 Answers

It is best to use relative paths, that way if you move the entire directory tree things will still work. So use

\newcommand*{\MyPath}{../thesis}%
\input{\MyPath/chap1/ch1.tex}


So assuming that you have your files as:

<some path>/thesis/thesis.tex
<some path>/thesis/chap1/ch1.tex
<some path>/thesis/chap2/ch2.tex


all the files are input relative to where they are with respect to the thesis directory.

Am pretty sure that you'll need to update all you \input statements, but this way it is flexible in case you decide to change the top level directory, or move it somewhere.

• How do I do this in Plain TeX? – Wk_of_Angmar Jul 29 '15 at 0:05
• @Wk_of_Angmar: I don't use Plain TeX but I think using \def\MyPath{../thesis} and the Plain TeX equivalent of \input. Perhaps you could post a new question if that does not work. – Peter Grill Jul 29 '15 at 4:41
• Is there an easier way that doesn't involve a hack like this? – tommy.carstensen Oct 22 '15 at 19:04
• @tommy.carstensen: Not sure how to help you. Perhaps if I knew what part of this you consider a hack, it might be easier to help. I think you should post a new question and detail what you want to accomplish and the problems with the approaches outlined here. – Peter Grill Oct 22 '15 at 22:15
• @PeterGrill Thanks! I did here: tex.stackexchange.com/questions/274457/… It is suggested to be a duplicate of tex.stackexchange.com/questions/128067/… – tommy.carstensen Oct 23 '15 at 0:05

I know the question is pretty old but since this post was the first on tex.sx I found this might be of help.

As proposed by Martin, a solution to the problem could be to use the import package as explained in How to use the import package?.

Adding this as a separate answer as I am not recommend using this method as it likely more work than it is worth, and that is to modify the input macro to do a search for a file over a list of directories and include that. This would have the advantage that you would not need to change each of the individual files, and allow you to move files around and still have them work.

If you decide to pursue this approach this question on How to iterate through the name of files in a folder might be helpful.