Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 ?

share|improve this question
add comment

4 Answers

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

share|improve this answer
2  
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
add comment

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.

share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

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)

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.