[Tex/LaTex] Set a subfolder for \include(only)

filesystem-accessinclude

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)?

Best Answer

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}
Related Question