# subimport and includeonly

Say I have a document structured as follows.

main.tex
+ ch1
- ch.tex
- s1.tex
- s2.tex
+ ch2
- ch.tex
- s1.tex
- s2.tex


Here, *.tex are LaTeX files, and ch1 and ch2 are directories.

% main.tex
\documentclass{article}
\usepackage{import}
\includeonly{ch1/ch}
\begin{document}
\subimport{ch1/}{ch}
\subimport{ch2/}{ch}
\end{document}

% ch1/ch.tex
\input{s1}
\input{s2}

% ch1/s1.tex
Section 1.1

% ch1/s2.tex
Section 1.2

% ch2/ch.tex
\input{s1}
\input{s2}

% ch2/s1.tex
Section 2.1

% ch2/s2.tex
Section 2.2


Unfortunately, this compiles the whole document, not just chapter 1. I'm probably doing this completely wrong, but I was trying to do the following. I have a separate folder for each chapter; in each chapter folder, I have a file ch.tex that manages the chapter as a whole, and files s1.tex, s2.tex,... that contain the major sections of the chapter. I am using the import command because I want to be able to use relative paths within ch.tex (both for inputing the section files, and for things like including graphics). However, in order to speed up compilation, I want to be able to use \includeonly. Is there a way to get the advantages of both import and \includeonly? What is the best practice for structuring large documents like this? (I'm curious about the best practice even if it means giving up relative paths or includeonly. Based on the other posts I read, it seems like the experts favor include instead of import [How to use the import package?, Splitting a large document into several files, How to make "\input" in a "\include"-d file use the correct current path? ] -- is there a good reason for this?) I saw a post (Getting \includeonly functionality with import package) that I hoped would give me the answer, but that post ended up skirting the actual issue.

• If chXare directories I'd simply use \input{chX/sY.tex} to include the sections. Keep it simple!
– Dox
Jul 8 '13 at 11:12

Here's the outline of a structure I've grown over the years.

Each chapter has a well named directory containing contents.tex and any other needed files for that chapter.

In my preamble, macros.tex:

% placeholders for commands often renewed
\newcommand{\here}{here}
\newcommand{\mychaptername}{whatever}


In the main driver, book.tex:

\include{macros}

%%-%%
%\includeonly{
%Preface/contents,
%FermiProblems/contents,
% ...
%}
%
\begin{document}

\renewcommand{\here}{Preface}
\renewcommand{\mychaptername}{Preface}
\include{\here/contents}

\renewcommand{\here}{FermiProblems}
\renewcommand{\mychaptername}{Calculating on the Back of an Envelope}
\include{\here/contents}

% and so on ...


Then, in a typical contents.tex

% FermiProblems/contents.tex
%
\chapter{\mychaptername}
\label{\here}
% ...
\includegraphics[width=40mm]{\here/fermi_stamp.jpg}
% ...
\begin{mytikz}
\input{\here/carbonfootprint}
\end{mytikz}
% ...


Remembering to use \here for files referenced from contents.tex takes care of finding those files in the current directory.

Since there are only a dozen or so chapters and they change infrequently, cutting and pasting the three line block of code for each chapter is easier than writing a macro to do the job.

I've found this structure to be pretty robust. There's a lot more in my preamble, and adding even some complex functionality (writing selected material to other files for later compilation) can be managed from one place.

For a large project you're just beginning, I recommend wrapping some standard LaTeX constructs (e.g. the mytikz environment above) with your own macros, so that you can change their behavior later with less wizardry and hence fewer questions on this site.

Edit: To compile just one chapter, use the following driver in the chapter directory.

\documentclass[pdftex,12pt]{book}

\input{../macros}

\begin{document}
\renewcommand{\here}{.}
\renewcommand{\mychaptername}{Test}
\include{\here/contents}
\end{document}


I was more concerned with what happens with multiple \documentclass \begin{document} and \end{document} commands. All of the following will run without errors except for preamble.tex

\documentclass{book}
\usepackage{mathtools}% for example
\newcommand{\preambleflag}{}
\begin{document}% you can only do this once


Here is master.tex

\input{preamble}
\tableofcontents
\input{chapter1}
\input{chapter2}
\end{document}


Here is chapter1.tex

\csname @ifundefined\endcsname{preambleflag}%
{\input{preamble}%
\edef\masterswitch{end}}%
{\edef\masterswitch{@gobble}}%
\chapter{I started life as a child}
\csname \masterswitch\endcsname{document}%


And chapter2.tex is almost identical

\csname @ifundefined\endcsname{preambleflag}%
{\input{preamble}%
\setcounter{chapter}{1}% will be incrremented before use
\edef\masterswitch{end}}%
{\edef\masterswitch{@gobble}}%
\chapter{I started life as a child}