6

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?

hhh
  • 8,743
  • 2
    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

2 Answers2

7

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

touhami
  • 19,520
  • 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) –  Dec 31 '15 at 08: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). –  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 03 '16 at 11:25
  • @hhh first part \tableofcontents \let\tableofcontents\relax without any package (latex functionality) part 2 is with packges 3 example given tableof, titletoc and minitoc. IMHO there is no need to any package if you don't look for partials tocs. – touhami Jan 03 '16 at 11:52
  • @touhami is the part 2 example meant to use all of the packages together or each separately? The myfile* are for each 1-3 demonstration about tableof, titletoc and minitoc i.e. not just for tableof? This distinction is somewhat confusing to me (trying to get minimal examples for each demonstration running). – hhh Jan 03 '16 at 12:07
  • @hhh answer updated. – touhami Jan 03 '16 at 12:36
  • As of "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." -- I am able to get precisely what I want with tableof but I am not able get this working with only the first two lines, I uncommented all tableof-\toftagstart-\toftagstop{myfile3} parts and no tableofcontents (no main TOC, no sub TOCs) produced. Can you clarify the initial parts? – hhh Jan 03 '16 at 12:40
  • I am not sure I understand your comment, but if you don't want package you need to remove command related to the package tableof or tiletoc (for titletoc these line between commented dush rules and for tableof commands with of like \...of... then add \tableofcontents \let\tableofcontents\relax in the main file and \tableofcontents in each subfile. – touhami Jan 03 '16 at 13:07
  • @touhami do I misunderstand the first sentence of your answer? This paste demonstrates about how I see it so far -- it does not work at all so I am sure I am misunderstanding the initial part. Can you help to clarify it? I have demonstrated my understanding here where I try to keep my ideas separate because of possible misunderstandings: \let...\relax structure in the case of \tableofcontents removes the value so far assigned to it, is it right? Then it assigns new vals until the next \tableofc..\let\...\relax? – hhh Jan 03 '16 at 13:36
  • The minitoc demonstration not working, codes as instructed copied to here -- only the main TOC appearing while the sub-TOCs not appearing. – hhh Jan 03 '16 at 15:50
3

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

enter image description here

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).

enter image description here

[3th demonstration] Minitoc and its README

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

enter image description 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.

hhh
  • 8,743
  • 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 03 '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 03 '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 03 '16 at 19:15
  • 1
    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 http://tex.stackexchange.com/questions/284773/toc-and-subfiles-which-toc-pkg-for-unique-toc-compilation-with-each-subfile/285741?noredirect=1#comment687264_284773. This is not necessary for tableof and titletoc – touhami Jan 03 '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 04 '16 at 12:30
  • Please make the problem with titletoc more clear. For me no problem. – touhami Jan 04 '16 at 12:31
  • @touhami added more information and a demonstration with a picture about titletoc (directly used your second codes without any change i.e. "2 - titletoc"), is it clear now? – hhh Jan 04 '16 at 18:23
  • I see it now. I will edit the code later. – touhami Jan 04 '16 at 20:35