How to make “\input” in a “\include”-d file use the correct current path?

I'm having a master file let's say test.tex and also some folders sub1/ and sub2/. There is also a TeX file in each of them: sub1/sub1.tex and sub2/sub2.tex. In the sub1/ and sub2/ are also some snippets sub1/snip11.tikz, sub1/snip12.tikz, sub2/snip21.tikz and sub2/snip22.tikz. The files look like following:

test.tex:

\documentclass[12pt,a4paper]{scrartcl}

\usepackage{tikz}

\begin{document}

\section{sub1}
\include{sub1/sub1}

\section{sub2}
\include{sub2/sub2}

\end{document}


sub1/sub1.tex:

\input{snip11.tikz}
\input{snip12.tikz}


sub2/sub2.tex:

\input{snip21.tikz}
\input{snip22.tikz}


The snip??.tikz contain some tikzpicture like

\begin{tikzpicture}
\draw (0,1) arc (90:130:1);
\end{tikzpicture}


If one tries to compile it, it will obviously fail, because while being in sub1/sub1.tex the \input command will look for snip11.tikz in the root folder instead of in the folder sub1/.

Is there any way to make \input look in the same folder as the folder in which the containing TeX file is located?

Of course I could add the correct path in the sub1/sub1.tex, but I'm having tons of subfolders and also this is not the solution I'm looking for.

-

The FAQ suggests the packages import or chapterfolder:

-
I tried it and although it looks like the correct solution it won't work. – Wauzl Apr 19 '12 at 13:56
I made an error. I tried \import instead of \include and now it works and seems like the corect solution for this issue. – Wauzl Apr 19 '12 at 14:04

It's not so easy to make \input work from a relative directory as the TeX macro layer doesn't know where your sub.tex is, it could be anywhere in the TEXINPUTS path. But you may use the same feature if your TEXINUTS setting (environment variable or texmf.cnf setting) includes .// then Tex will recursively search all subdirectories for each file, so as long as your filenames are distinct it doesn't matter which subdirectory they are in.

so

$latex test This is pdfTeX, Version 3.1415926-1.40.11 (Web2C 2010) restricted \write18 enabled. entering extended mode (./test.tex LaTeX2e <2009/09/24> Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, ge rman-x-2009-06-19, ngerman-x-2009-06-19, ancientgreek, ibycus, arabic, armenian , basque.... ! LaTeX Error: File hmm.tex' not found.  but setting the path: $ TEXINPUTS=";.//" latex test
This is pdfTeX, Version 3.1415926-1.40.11 (Web2C 2010)
restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2009-06-19, ngerman-x-2009-06-19, ancientgreek, ibycus, arabic, armenian
, basque,...
(./sub1/hmm.tex


here the file hmm.tex has been found in the subdirectory.

test.tex here is just

 \input{hmm}


Here TEXINPUTS has been changed on the command line just for this command (bash syntax) that may or may not be available depending on your system but you will be able to set TEXINPUTS somewhere.

-

If you only have files with different name in all your subfolders, I would suggest getting rid of the subfolder calls in your main document and only calling \input{file} while adding the relevant folders to the environment variable TEXINPUTS like:

TEXINPUTS=".:./sub1:./sub2:./sub3:\$TEXINPUTS" latex main.tex


you can include path for graphics included with \includegraphics{} as well as it also plays the role of the \graphicspath{} command.

If you have two files with the same name in two different subfolders, then it might be a bit more tricky.

-

Here is a potential solution that uses a variable that is changed each time a new file is loaded. It should work for any number of subfolders.

For your main file, test.tex:

\documentclass[12pt,a4paper]{scrartcl}

\usepackage{tikz}

%these are the relevant lines%
\newcommand\subfolder{}
\newcommand\rinclude[2]{
\renewcommand\subfolder{#1}
\include{#1/#2}}
\newcommand\rinput[1]{
\input{\subfolder/#1}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section{sub1}
\rinclude{sub1}{sub1}

\section{sub2}
\rinclude{sub2}{sub2}

\end{document}


Then, in your sub1/sub1.tex file you would use:

\rinput{snip11.tikz}
\rinput{snip12.tikz}


Likewise, in your sub2/sub2.tex file:

\rinput{snip21.tikz}
\rinput{snip22.tikz}


For this solution you would simply use the new command \rinclude instead of \include and in your .tex files in the subfolders, used \rinput instead of \input. The first argument for the \rinclude command is the subfolder and the second argument is the filename. Arguments for \rinput are the same as for \input.

-
It actually worked, but the solution with the package import seems more straightforward. – Wauzl Apr 19 '12 at 14:02