[Tex/LaTex] Include sections of some chapters but not others in table of contents

sections-paragraphstable of contents

The title pretty much says it all. I have a long document, which contains chapters, sections, subsections…. The table of contents should only include sections for most of the chapters, but subsections within one specific chapter like this:

Chapter 1
    section 1
    section 2
Chapter 2
    section 1
    section 2
Chapter 3
    section 1
        subsection 1
        subsection 2
    section 2
        subsection 1
        subsection 2
Chapter 3
    section 1
    section 2

Is this possible? If so, how does one do it?

[Edit: some of the sections in e.g. Chapter 1 will also have (numbered) subsections, but I don't want them to show up in the table of contents. Basically, I want the TOC depth to vary from chapter to chapter.]

Best Answer

you can reset the tocdepth in the toc by inserting change commands in appropriate places.

say you start with \setcounter{tocdepth}{1} to include nothing lower than sections. to reset this in another chapter to include subsections, insert this command just after the \chapter command for the relevant chapter:

\addtocontents{toc}{\setcounter{tocdepth}{2}}

the command needs to go into the file with the chapter data; if you put it into your main file, and use \include to pull in the chapter files, latex will delay its application until after the \included file, so it's better not to try to do this from the main file. as for where it goes in the chapter file, it can be put anywhere before the first section that is to be listed. if you put it right after the \chapter line, it'll be easy to remember and to find if you decide later that you want to change it.

you can restore the original setting in the next (or next applicable) chapter with another \addtocontents insertion.

there are other ways to limit the scope of the tocdepth setting, but this is probably the most straightforward.