49

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 ?

4 Answers 4

46

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.

8
  • 1
    So this is basically \graphicspath but for \input? Neat.
    – Seamus
    Commented Aug 4, 2011 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. Commented Aug 4, 2011 at 11:04
  • I actually just changed my answer to offer the TEXINPUTS as a possible solution! Great minds...
    – Seamus
    Commented Aug 4, 2011 at 11:09
  • 2
    In case anyone else is running into problems with this answer: I am trying to use PDFLaTeX on an ubuntu machine with texlive. If I use /path/to/folder instead of /path/to/folder/ I get a problem. However with /path/to/folder/ things run smoothly. Thanks! Commented Dec 27, 2015 at 1:22
  • I had the same experience. The final slash seems to be mandatory. Maybe someone can edit the answer?
    – jan
    Commented Apr 28, 2016 at 4:58
25

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.

Further reading:

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".

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

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/.

1
  • 1
    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
    Commented Mar 7, 2015 at 18:13
1

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)

3
  • 1
    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
    Commented Jul 8, 2011 at 19:53
  • This is a very bad idea indeed! Besides not being OS portable, it does not work with folder under other tools, like for example Dropbox which does allow for sym links withing their directories. Commented Mar 6, 2022 at 1:32
  • It works well with Nextcloud and git though. (I mean, you'd hopefully not share a LaTeX project via Dropbox anyways.) OS portability is a problem, yes, if you work with Windows (users).
    – Turion
    Commented Mar 6, 2022 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .