# \input and absolute paths

Sometimes it is convenient to use an absolute path to include a preamble. To do so, I use the following "hack":

\documentclass[a4paper, 12pt]{article}
\newcommand{\folder}{/path/to/folder}
\input{\folder/preamble}

\begin{document}
\end{document}


Now my question is, whether there is a more elegant way to do this, especially a way which is compatible with one of the flatten scripts from Replace \input{fileX} by the content of fileX automatically ?

-

You can define the internal macro \input@path to hold a list of base directories:

\makeatletter
\def\input@path{{/path/to/folder}}
%or: \def\input@path{{/path/to/folder}{/path/to/other/folder}}
\makeatother


Then you can use \input{fileinthatfolder} everywhere. It also works for \includegraphics IIRC.

But I don't think this is supported by the mentioned scripts. AFAIK they simply take the string from the file and don't look at any macros.

-
So this is basically \graphicspath but for \input? Neat. –  Seamus Aug 4 '11 at 10:52
@Seamus: actually \graphicspath defaults to the value of \input@path at package load time. Then inside \includegraphics inside a group \input@path is set to the \graphicspath value. However, there might be some tools which don't take \input@path into account. IIRC it is better to use the TEXINPUTS environment variable. But you can't set them inside the document. –  Martin Scharrer Aug 4 '11 at 11:04
I actually just changed my answer to offer the TEXINPUTS as a possible solution! Great minds... –  Seamus Aug 4 '11 at 11:09

So I had one answer (which is preserved below for posterity) but I think I understand the question a little better now. What you want is to write \input{foo} and have LaTeX know where to search for foo. Martin's answer offers one way to do this. Here are some others.

One way is to make a directory in your local texmf tree and put files you want to use in there. So (assuming a uni system) this will be something like ~/texmf/ Make a subdirectory e.g. ~/texmf/tex/latex/commonstuff/ and put your files in there. Then they will be available to \input. Be careful about name clashes, though!

If, for whatever reason, you cannot move where your files are stored, you could add that directory to TEXINPUTS which is a variable that stores the directories that tex searches for files called with \usepackage \input and similar commands.

I maintain that this may be overkill if the problem is simply that some files live in the folder "above" the one containing the tex file (when the ../ trick discussed below works fine)

I don't really understand the question. As far as I am aware, absolute paths work with \input? I just tried one and it worked.

Sometimes it is easier to use relative paths, but to know how to use them. For example a lot of the time I want to insert a graphic which is housed in a different folder. Let's say I have the following:

Work
Document
main.tex
Graphics
img.pdf


In main.tex I can write \includegraphics{../Graphics/img} and this finds my image: .. goes "up a level".

-
Could whoever downvoted this explain why? Thanks. –  Seamus Aug 3 '11 at 16:25
Whoever downvoted seems happy with the new answer... I'd still appreciate an explanation of what was faulty with the earlier answer... –  Seamus Aug 4 '11 at 12:30

An other solution (easiest) is given by Ali Mehrizi in this post.

The solution is based on the import package, using the subimport command:

\subimport{PathToFolder/}{File.tex}.


A later \input{} into the "File" will search for a file into the specified folder PathToFolder/.

-
While this does not quite seem to answer the question, the information itself was very helpful in solving a slightly different problem: how to organize a large document with a subdirectory structure. See also: tex.stackexchange.com/q/4602/8057 –  krlmlr Mar 7 at 18:13

I'm not sure if you're satisfied with this: Just do a symbolic link of the file you want to include into the directory of the file you want to compile and use \input{preamble}. (This might not work anywhere else than Linux)

-
Thanks, but that's not an option for me. Suppose the preamble file is in a folder foo. Now I have a rather large directory tree under foo, where in every subfolder there could be a tex file which wants to include the preamble. Using emacs auto-insert, emacs inserts the include line for the preamble automatically if I create a tex file in some folder of that tree. So using a symbolic link would be not very convenient since then I have to create one for every folder of my tree and a new one for every new folder... –  student Jul 8 '11 at 19:53