4.20 combining complete documents into one

4.20.1 combining using manual sectioning

Problem description: I have many standalone Latex documents that I want to compile into documents on their own, but also I want to combine them into one main document and have the table of contents and other references work as if all documents were written as one file.

Solution:

Given this example layout

 home/main.tex 
 home/folderA/a.tex
 

Where main.tex and a.tex are self contained Latex files, each with its own title, table of contents and can include local resources such as images and listings.

Add the package \usepackage{standalone} in main.tex to strip all the preamble from the included latex files. Also add it to each child document, so that each child document can be compiled as standalone as well.

To use the above, make sure the master document at the top level includes all packages included by all the children.

Setup main.tex

\documentclass[12pt,notitlepage]{article} 
\usepackage{standalone}% 
\usepackage{listings} 
\usepackage{import} 
\usepackage{lipsum} 
\usepackage{graphicx} 
\usepackage{hyperref} 
 
\makeatletter 
\providecommand{\currentimportpath}{\import@path} 
\makeatother 
 
\begin{document} 
\title{This is my document home/main.tex title} 
\author{me} 
\maketitle 
\tableofcontents 
 
\section{this is first section in main.tex} 
once upon a time, and now include the other document 
 
\subimport{folderA/}{a} 
\end{document}
 

Setup a.tex as follows. This is important: remember to add \standalonetrue after \usepackage{standalone} in each child package. We need this to be able to compile each child package on its own and get a table of contents and title. We will us an \ifstandalone logic in each child to check if we are building it standalone or not. When the main is build, this flag will automatically be false, hence we will not get table of content shown in each child. Little complicated, but just do it as shown:

\documentclass[12pt,notitlepage]{article} 
\usepackage{standalone} 
\standalonetrue   %remember this ! 
 
\usepackage{listings} 
\usepackage{import} 
\usepackage{lipsum} 
\usepackage{graphicx} 
\usepackage{hyperref} 
 
\makeatletter 
\providecommand{\currentimportpath}{\import@path} 
\makeatother 
 
\begin{document} 
 
\ifstandalone   %Do this, so toc only shows when build standalone 
  \title{This is my document home/folderA/a.tex title} 
  \author{me} 
  \maketitle 
  \tableofcontents 
\fi 
 
\section{first section in file a.tex} 
\includegraphics{a.png} 
 
\section{second section in file a.tex} 
\lstinputlisting{a.txt} 
 
\section{third section in file a.tex} 
you can find my report \href{\currentimportpath a.txt}{here} 
 
\lipsum[1] 
\end{document}
 

Now you can compile a.texm or its own

cd home/folderA 
pdflatex a.tex
 

And the result is

But when compiling main.tex cd home; pdflatex main.tex the result shows a table of contents that includes all children documents as shown

This is the result side by side

This zip file contains the tree shown above with all the files needed to rebuild it.

4.20.2 combining using automatic sectioning

One problem with the above approach, is that we had to be explicit with add \section and \subsection in the correct order in different documents in different folders.

It would be better if this can be automated. Using a method shown here http://tex.stackexchange.com/questions/9024/is-there-a-program-th at-allows-to-move-sections-and-automatically-adjusts-the-le and I modified it slightly, here are the files needed. First assume we have a tree like this:

home/main.tex 
home/A/a.tex 
home/A/B/b.tex
 

and we want to build main.tex, which includes a.tex, and where a.tex also includes b.tex, and where we can build each document as standalone. These are the 3 files

main.tex

\documentclass[12pt,notitlepage]{book} 
\usepackage{standalone}% 
\input{commonlatex} 
 
\begin{document} 
\title{This is my document home/main.tex title} 
\author{me} 
\maketitle 
\tableofcontents 
 
\begin{deeplevel}{this is first section in main.tex} 
once upone a time, and now include the other document 
 
\subimport*{folderA/}{a} 
\end{deeplevel} 
\end{document}
 

the file a.tex is

\documentclass[12pt,notitlepage]{article} 
\usepackage{standalone} 
\standalonetrue 
\input{commonlatex} 
 
\begin{document} 
 
\ifstandalone 
  \setcounter{level@depth}{1} 
  \title{This is my document home/folderA/a.tex title} 
  \author{me} 
  \maketitle 
  \tableofcontents 
\fi 
 
\begin{deeplevel}{first section in file a.tex} 
\includegraphics{a.png} 
 
  \subimport*{folderAB/}{b} 
 
  \begin{deeplevel}{this should show up below the above} 
     \lipsum[75] 
  \end{deeplevel} 
 
\end{deeplevel} 
 
\begin{deeplevel}{second section in file a.tex} 
\lstinputlisting{a.txt} 
\end{deeplevel} 
 
\begin{deeplevel}{third section in file a.tex} 
you can find my report \href{\currentimportpath a.txt}{here} 
\end{deeplevel} 
 
\lipsum[1] 
\end{document}
 

and the file b.tex is

\documentclass[12pt,notitlepage]{article} 
\usepackage{standalone} 
\standalonetrue 
\input{commonlatex} 
 
\begin{document} 
 
\ifstandalone 
  \setcounter{level@depth}{2} 
  \title{This is my document home/folderA/folderB/b.tex title} 
  \author{me} 
  \maketitle 
  \tableofcontents 
\fi 
 
\begin{deeplevel}{first section in file b.tex} 
\lipsum[75] 
 
\begin{deeplevel}{this should show up below the above} 
  \lipsum[75] 
\end{deeplevel} 
 
\end{deeplevel} 
 
\begin{deeplevel}{second section in file b.tex} 
\lstinputlisting{b.txt} 
\end{deeplevel} 
 
 
\begin{deeplevel}{third section in file b.tex} 
you can find my report \href{\currentimportpath b.txt}{here} 
\end{deeplevel} 
 
\lipsum[1] 
\end{document}
                                                                                  
                                                                                  
 

and the common include file is

%====================================== 
\makeatletter 
\newcounter{level@depth} 
\setcounter{level@depth}{-1} 
 
\newenvironment{deeplevel} % 
{ % 
  \addtocounter{level@depth}{1}% 
 
  \ifcase\c@level@depth 
  \expandafter \part 
  \or \expandafter \chapter 
  \or \expandafter \section 
  \or \expandafter \subsection 
  \or \expandafter \subsubsection 
  \or \expandafter \paragraph 
  \or \expandafter \subparagraph 
  \or \expandafter \subsubparagraph 
  \else 
  \PackageError{deeplevel} 
  { % 
    Sections are too deeply nested.% 
  } % 
  { % 
    Trying to recover with \string\subsubparagraph% 
  } % 
  \expandafter \subsubparagraph 
  \fi 
} % 
{% 
  \addtocounter{level@depth}{-1}% 
} 
 
\makeatother
 

Now one is able to build main.tex or a.tex or b.tex each on its own, and still get a complete document for each.