2

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

Tobi
  • 56,353

1 Answers1

2

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}
Tobi
  • 56,353
  • If some file to \include is not in the chapters subfolder you have a problem. – egreg Nov 05 '12 at 23:42
  • 1
    @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 06 '12 at 00: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 06 '12 at 08:43
  • @egreg: OK thanks, but there aren’t other problems? – Tobi Nov 06 '12 at 17:38