# How to combine several child files such that each child file has its own Table of Contents?

I want each file's Table of Contents to remain in tact, and at no point do I want the page numbering to restart for any child file. I am using LyX.

I tried using the titletoc package, and replaced the LyX Table of Contents object (which is inserted by going Insert->List/TOC->Table of Contents) with the following LaTeX code:

\startlist{toc}


But I receive the following error when I export the master document:

LaTeX error: Something's wrong -- perhaps a missing /item.

(I do not receive this error when I export the child documents individually.)

Thus, my questions are:

• How can I combine several child files into a master document, with each child file having its own Table of Contents?
• Is the use the titletoc package the appropriate package for what I am trying to achieve?

Potentially Related Questions:

Two separate Tables of Contents

Titletoc/Titlesec/Titleps Documentation: http://mirror.its.dal.ca/ctan/macros/latex/contrib/titlesec/titlesec.pdf

This is how I have interpreted this question.

The child files are independent, standalone documents. Each has its own table of contents, which I interpreted as a requirement for a mini-toc.

The master file has its own toc. The child files are loaded into the master file using \input. The child files retain their own toc, updated with new page numbers reflecting the master document pagination.

This is done as follows:

1. Each child document uses the article class and the standalone package. The child files include a test (\newif\ifstandlone \standalonetrue) to determine whether it is being compiled standalone. Each child file then becomes a standalone document that can be compiled, building its own toc in the process using \startcontents and \printcontents from the titletoc package.

2. The master file uses report class and also loads the standalone package. Each child file is loaded using \input. Because standalone is being used, it strips the preamble of each child file leaving just the content of each child file. A toc is built in the master file using macros from titletoc.

The net outcome is that each child file can be a standalone document that can be compiled to produce a finished document, while it is also possible to build a master file from the child files without any alteration of those files. Both the master document and child files have their respective toc.

This answer is enabled by some excellent answers posted elsewhere on TeX.SE. The sources are: using titletoc (Minitoc and memoir) and standalone (Get {standalone} to ignore blocks of text from \input files (e.g., \begin{spacing} and \maketitle)).

This is the MWE and output.

\begin{filecontents*}{child1.tex}
\documentclass[a4paper,11pt]{article}
\usepackage{standalone}
%https://tex.stackexchange.com/questions/29995/get-standalone-to-ignore-blocks-of-text-from-input-files-e-g-beginspacin/30000#30000
\newif\ifstandlone \standalonetrue
\usepackage{lipsum}
\usepackage{titletoc}

\begin{document}
%https://tex.stackexchange.com/questions/5944/minitoc-and-memoir/7877#7877
\section*{Contents}
\startcontents[sections] %
\printcontents[sections]{}{1}{}
\section{Child 1 - Section 1}
\lipsum[2]
\section{Child 1 - Section 2}
\lipsum[2]
\end{document}
\end{filecontents*}

\begin{filecontents*}{child2.tex}
\documentclass[a4paper,11pt]{article}
\usepackage{standalone}
\newif\ifstandlone \standalonetrue
\usepackage{lipsum}
\usepackage{titletoc}

\begin{document}
\section*{Contents}
\startcontents[sections]
\printcontents[sections]{}{1}{}
\section{Child 2 - Section 1}
\lipsum[2]
\section{Child 2 - Section 2}
\lipsum[2]
\end{document}
\end{filecontents*}

\documentclass[a4paper,11pt]{report}
\usepackage{standalone}
\usepackage{lipsum}

%https://tex.stackexchange.com/questions/39153/table-of-contents-with-chapter
\usepackage{titletoc}
\titlecontents*{chapter}% <section-type>
[0pt]% <left>
{}% <above-code>
{}% <numberless-entry-format>
{\bfseries\hfill\contentspage}% <filler-page-format>

\begin{document}

\tableofcontents

\chapter{First child document}
\input{child1.tex}

\chapter{Second child document}
\input{child2.tex}

\end{document}


Are you looking for something like this?

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{titlesec,titletoc,lipsum}

\titleformat{\section}
{\normalfont\Large\bfseries}
{\thesection}{1em}{}
[\vspace*{2pc}%
\startcontents
\printcontents{}{1}{\setcounter{tocdepth}{2}}%
]

\newcommand\repeatsection{%
\section{This section}
\lipsum[1]
\subsection{This subsection}
\lipsum[1]
\subsection{This subsection}
\lipsum[1]
\subsection{This subsection}
\lipsum[1]
}

\newcommand\repeatsections{%
\repeatsection\repeatsection\repeatsection\repeatsection
}

\begin{document}
\tableofcontents
\repeatsections
\end{document}