# Set a subfolder for \include(only)

Let’s assume a project with a main file main.tex including the chapter with \include.

% main.tex
\documentclass{book}

\includeonly{one}

\begin{document}
\include{one}
\include{two}
\end{document}


% one.tex
\chapter{First chapter}
Some text


% two.tex
\chapter{Second chapter}
Some text


To keep the diretory clean the chapters should be saved in a folder named chapters/. Doing this means also to add the folder to each \include macro and to \includeonly too.

\documentclass{book}

\includeonly{chapters/one}

\begin{document}
\include{chapters/one}
\include{chapters/two}
\end{document}


So I wonder if it is possible to change the default path used with \include(only)?

-
The answers in How to make the main file recognize relative paths used in the imported files? provide solutions to your problem. For example, the suggested inputx and import packages. –  Werner Nov 5 '12 at 23:04
@Werner: Thank you! But as far as I can see import doesn’t provide what I need … inputx isn’t on CTAN, is it? –  Tobi Nov 5 '12 at 23:13
No, inputx is provided as part of the answer. Since I haven't tried it, this would be an optimal opportunity to test it out and push an answer-to-package... –  Werner Nov 5 '12 at 23:15
@Werner: Are you blackstev? Would you like to make a official package out of it and send it to CTAN? –  Tobi Nov 6 '12 at 1:00

It seems to be possible to hack the definition of \include(only). It seems to work but I’m not sure about the consequences …

\documentclass{book}

\makeatletter
\def\include#1{\relax
\ifnum\@auxout=\@partaux
\@latex@error{\string\include\space cannot be nested}\@eha
\else \@include{chapters/#1} \fi%
}
\def\includeonly#1{%
\@partswtrue
\edef\@partlist{\zap@space chapters/#1 \@empty}%
}
\makeatother

\includeonly{one}

\begin{document}
\include{one}
\include{two}
\end{document}


I thought about make this a little package providing a macro \includepath{<dir>} to add the directory automatically. Would this work without any harms?

\documentclass{book}

\makeatletter
\def\@includepath{}
\newcommand*{\includepath}[1]{%
\xdef\@includepath{#1}%
}
\@onlypreamble\includepath
\def\include#1{\relax
\ifnum\@auxout=\@partaux
\@latex@error{\string\include\space cannot be nested}\@eha
\else \expandafter\@include{\@includepath#1} \fi%
}
\def\includeonly#1{%
\@partswtrue
\edef\@partlist{\expandafter\zap@space\@includepath#1 \@empty}%
\renewcommand*{\includepath}[1]{%
\typeout{ERROR: \string\includepath\space must be
used before \string\includeonly!}
}
}
\makeatother

\includeonly{two}

\begin{document}
\include{one}
\include{two}
\end{document}

-
If some file to \include is not in the chapters subfolder you have a problem. –  egreg Nov 5 '12 at 23:42
@egreg: I tested it an the only problem is No file chapters/one.tex which is the same with the original \include when a file is missing, isn’t it? –  Tobi Nov 6 '12 at 0:58
I was thinking to the fact that you can't use \includeonly for referring to a file that doesn't live in chapters. –  egreg Nov 6 '12 at 8:43
@egreg: OK thanks, but there aren’t other problems? –  Tobi Nov 6 '12 at 17:38