[Tex/LaTex] TOC and Subfiles: which TOC pkg for unique TOC compilation with each subfile

compilingmultiple filessubfilestable of contents

The Subfiles package makes compiling individual parts of a LaTex document convenient. The package works so that each subfile inherits the master preamble main.tex and the user is able to compile individual files separately. The package itself has no mention about TOCs for separate parts that are compiled.

Independent TOCs with Subfiles pkg must have some convenient solution. The options contain minitoc, shorttoc and titletoc, more in comment.

Which TOC package to use with Subfiles pkg to outline the document more conveniently consisting of multiple files?

Best Answer

One can have independent TOCs without using any package, we need just to add in the main file

\tableofcontents
\let\tableofcontents\relax

With packages

Here are examples of how one can use packages

1- tableof. (edited) :

With the two lines approach above, one can compile separately each included sub-file, and it will have its own TOC. And the main file will have a global TOC. If that's what is wanted no need to read further.

If however one wants the included files to have their own TOC also in the main file, one can use tableof in the following, somewhat convoluted way. The constraints are:

  1. tableof does not modify the book class default \tableofcontents, which, when used once displays the TOC but also erases the .toc file until the end of the document, so one can not use it twice.

  2. tableof has its own additional commands such as \tableoftaggedcontents or \tableof. By themselves these commands display a TOC but do not open a .toc file for write. One must use, if no \tableofcontents command has been used, the package \tofOpenTocFileForWrite.

  3. If one uses \tofOpenTocFileForWrite, after it \tableofcontents displays an empty TOC, one must use the package commands such as \tableoftaggedcontents or \tableof rather.

  4. The latter commands do not print a heading, this can be done by the user with a \section* or a \chapter* etc...

If one insists despite the above constraints to use tableof package, here is how to do it. I have made it so that the included files are supposed to have only one chapter, and their TOC is for the contents of this chapter.

To test this and other examples make sure to start from a clean state with no auxiliary files. And as usual with TOCs, at least two compilations are needed, be it for the main file or the subfiles, if compiled individually. Naturally the numbering of sections and chapters will not be the same in the main file as it is in individually compiled subfiles.

main file myfile.tex

\documentclass{book}
\usepackage{subfiles}
\usepackage{lipsum}
\usepackage{tableof}
\AtBeginDocument{\tofOpenTocFileForWrite}% see tableof's doc
\begin{document}
\tableof{}% replaces book class \tableofcontents, which one
% can not use after \tofOpenTocFileForWrite
\chapter{Foo}
\section{Bar}
\lipsum[1-2]
\section{Baz}
\lipsum[1-2]
\subfile{myfile1}
\subfile{myfile2}
\subfile{myfile3}
\end{document}

myfile1.tex

% main file is called myfile.tex
% this is myfile1.tex
\documentclass[myfile]{subfiles}
\begin{document}
\chapter{Foo1}
\tableof{myfile1}
\toftagstart{myfile1}
\section{Bar1}
\lipsum[1-2]
\section{Baz1}
\lipsum[3-4]
\toftagstop{myfile1}
\end{document}

myfile2.tex

% main file is called myfile.tex
% this is myfile2.tex
\documentclass[myfile]{subfiles}
\begin{document}
\chapter{Foo2}
\tableof{myfile2}
\toftagstart{myfile2}
\section{Bar2}
\lipsum[5-6]
\section{Baz2}
\lipsum[7-8]
\toftagstop{myfile2}
\end{document}

myfile3.tex

% main file is called myfile.tex
% this is myfile3.tex
\documentclass[myfile]{subfiles}
\begin{document}
\chapter{Foo3}
\tableof{myfile3}
\toftagstart{myfile3}
\section{Bar3}
\lipsum[9-10]
\section{Baz3}
\lipsum[10-11]
\toftagstop{myfile3}
\end{document}

2- titletoc

main file myfile.tex

\documentclass{book}
\usepackage{subfiles}
\usepackage{lipsum}
%------
\usepackage{titletoc}
\startcontents
\begin{document}
%------
\printcontents{}{-1}{% parts are add to toc
\setcounter{tocdepth}{2}%
\chapter*{\contentsname}%  or section ... 
}
%\renewcommand{\printcontents}[4][default]{} %if you don't want partial tocs
%------
\part{one}
\chapter{Foo}
\section{Bar}
\lipsum[1-2]
\section{Baz}
\lipsum[1-2]
\subfile{myfile1}
\subfile{myfile2}
\subfile{myfile3}
\end{document}

myfile1.tex

\documentclass[myfile]{subfiles}
\begin{document}
%------
\printcontents{}{-1}{%
\setcounter{tocdepth}{2}%
\chapter*{\contentsname}% or section ... 
}
%------
\chapter{Foo1}
\section{Bar1}
\lipsum[1-2]
\section{Baz1}
\lipsum[3-4]
\end{document}

myfile2.tex

\documentclass[myfile]{subfiles}
\begin{document}
%------
\printcontents{}{-1}{%
\setcounter{tocdepth}{2}%
\chapter*{\contentsname}% or section ... 
}
%------
\chapter{Foo2}
\section{Bar2}
\lipsum[5-6]
\section{Baz2}
\lipsum[7-8]
\end{document}

myfile3.tex

\documentclass[myfile]{subfiles}
\begin{document}
%------
\printcontents{}{-1}{%
\setcounter{tocdepth}{2}%
\chapter*{\contentsname}% or section ... 
}
%------
\chapter{Foo3}
\section{Bar3}
\lipsum[9-10]
\section{Baz3}
\lipsum[10-11]
\end{document}

3- minitoc we add in the main file

\usepackage{minitoc}
\dominitoc
\begin{document}
\tableofcontents
\let\tableofcontents\relax

and in subfiles we add just \tableofcontents.

Further reading

Related Question