For the graphicx package, we can use \graphicspath{...} so that we can directly use filenames in the argument of \includegraphics{...}. Can a similar thing be done for directly using filenames in \input{...}. I know of the method mentioned here but if this was the way to do it, then why does \graphicspath{...} exist?
- 755
-
3Related: How to make the main file recognize relative paths used in the imported files? (Take a look at Andrew's comment as well.) – Qrrbrbirlbel Oct 25 '12 at 13:49
2 Answers
\graphicspath comes from LaTeX's \input@path, just using the paths for graphics files.
\input@path can be set independently, e.g.:
\makeatletter
\def\input@path{{path1/}{path2/}}
\makeatother
Internally package graphics stores its path of \graphicspath in \Ginput@path and
locally sets \input@path to \Ginput@path, if it looks for files via \IfFileExists.
Addition to \input@path
The macro \input@path can be undefined (usually the default in LaTeX) or it can already contain other path entries. Therefore, \providecommand in the following code first defines the macro for the case that \input@path is undefined. Then \g@addto@macro extends the definition of \input@path.
\makeatletter
\providecommand*{\input@path}{}
\g@addto@macro\input@path{{path1/}{path2/}}% append
\makeatother
Alternatively \edef can be used, which expands the definition text.
The next example uses it to prepend the contents to \input@path to the old meaning of \input@path:
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{path1/}{path2/}\input@path}% prepend
\makeatother
- 271,626
-
2how do you add to that list without removing whatever is there already? – Frederick Nord Sep 23 '15 at 20:54
-
3@FrederickNord Answer updated for showing how to append or prepend new path elements. – Heiko Oberdiek Sep 23 '15 at 21:44
-
This works great! I sometimes compile .tex files directly other times from a makefile in another folder. Using this method automatically adjusts the locations of figures and .bib files for each of these eventualities. – maja zaloznik Aug 08 '18 at 16:12
You can try this:
\graphicspath{{figures/}{logs/}}
It must have two items, like figures and logs.
Besides, it must have / after the item.
Try it!
-
Does this really apply specifically to
\input? My understanding is that this will still apply only to graphics, not to other files. – dgoodmaniii Nov 18 '19 at 13:55