# TOC and Subfiles: which TOC pkg for unique TOC compilation with each subfile?

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?

• it seems that tableof and etoc works fine. titletoc too. And minitoc if each file is a section-like (part, chapter ...). I am not sure about compatibilite with non standard class (for minitoc and titletoc). – touhami Dec 26 '15 at 18:25

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.

• in the method with tableof, the main file (not file1, etc...) should contain in its preamble \AtBeginDocument{\tofOpenTocFileForWrite} (cf doc of tableof; perhaps it worked for you because the files had initially been compiled while they contain a \tableofcontents command ? but starting with a clean state with no auxiliary file, the current code, as explained in tableof's doc will not create the needed .toc file) – user4686 Dec 31 '15 at 8:48
• @jfbu thanks I'll see. you can edit my answer. – touhami Dec 31 '15 at 16:56
• at first I wasn't sure what the OP was asking for. May I edit the tableof part of your answer ? (I saw your edit following my comment, but if you check you will see something else is needed like \toftagstop; thus your simpler no package answer is better anyhow). – user4686 Dec 31 '15 at 17:36
• @jfbu feel free, I add \toftagstop as a note in my answer, answer that was given as example of demonstration, of course one should think also in toc heading and so. – touhami Dec 31 '15 at 18:04
• I cannot understand which part of the example belong to tableof package and which parts are integral to latex/tex. Is this "\tableofcontents \let\tableofcontents\relax" an example about tex/latex functionality while the part starting with "1- tableof. (edited) :" begins tableof functionality demonstration? Alas the minitoc pkg is also used: so this demonstrates the tableof-minitoc functionalatiy for TOC with subfiles? Did I understand it correctly? – hhh Jan 3 '16 at 11:25

This outlines the touhami: firstly the basic functionality of the main about \let\relax while the last parts demonstrate the subfiles pkg and TOC pkgs separately.

# Main

The \tableofcontents prints the tableofcontents to the whole document. The \let\tableofcontents\relax works like pointing to /dev/null with lazy evaluation, "at the point of definition" and more in What is the difference between \let and \def? and What does \relax do?.

# Demonstrations

[0th demonstration] "it seems that tableof and etoc works fine" comment by touhami, etoc and tableof. Demonstration to be done.

[1st demonstration] Documentation for tableof

Tableof demonstration worked. The left is the main TOC while the right is the sub-TOC.

[2nd demonstration] Documentation for titletoc

The titletoc demonstration has the same main TOC over all TOCs, no sub-TOCs for each chapter, not working elegantly where succint subTOCs outline each chapter (not the same TOC repeating all the time).

[3th demonstration] Minitoc and its README

[Trial 1] Not printing the sub-TOCs with Minitoc, more here.

The minitoc shows the main TOC correctly while repeating the sub-TOCs, the goal is to outline only the relevant parts of each chapter.

• I am confused now. What do you with sub-tocs? titletoc works for me the same as Tableof. For minitoc i will add example later. if you can make it more clear. – touhami Jan 3 '16 at 17:26
• minitoc should works too for you but i think you are looking for partials tocs (minitoc of each chapter)? – touhami Jan 3 '16 at 17:48
• @touhami added a picture about the terminology explaining the terms "main TOC" and "sub-TOC", my terminology, I don't know better words to describe them. – hhh Jan 3 '16 at 19:15
• OK. I can see it now, I think you look for partials tocs that for seperate subfiles will be as main tocs. The problem here, is that subfiles should be (let say) section-like (part, chapter or section) see my first comment tex.stackexchange.com/questions/284773/…. This is not necessary for tableof and titletoc – touhami Jan 3 '16 at 19:29
• As you can see in 1st image, there will be a problem : partial toc is after chapter heading Foo2, now this is not one want if the subfiles are compiled seperate no? I think it's time to edit the question or start another one with MWE of main file, subfiles and how you want it works. – touhami Jan 4 '16 at 12:30