22

Using \setcounter{tocdepth}{0} displays just the chapter's title in the table of contents. In the last chapter, there's a subsection. I want to use \setcounter{tocdepth}{0}! Therefore no subsection should be displayed in the table of content. However, using the option 0, a problem shows up. The subsections are not displayed in the table of contents, but there's a gap between the last chapter and the references in the end. It seems like the option 0 just doesn't print the subsection in the contents, leaving some space between the last chapter and the references chapter. I added a picture, as well as the used template (not the complete, it's too long). If I use \setcounter{tocdepth}{1} there is no gap, and everything looks fine! But I do not want to have the sections in the table of content

Here is a test code, but without the thesis.cls it won't compile.

\documentclass[10pt,twoside,openright]{thesis}
\usepackage{graphicx}   % if you want to include graphics files
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\begin{document} 
%\titlepage             % Print titlepage   
\newpage               % blank page
%\copyrightpage        % optional         
\tableofcontents       % required 
%\listoftables          % required if there are tables
%\listoffigures         % required if there are figures


\chapter{test1}
some text
\chapter{test2}
more text
\section{test3}
ds
\chapter{test4}
\end{document}

table of content with gap

Some lines from the thesis.cls file.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            SECTION HEADINGS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
\newcommand\chaptersize{\large}
\newcommand\sectionsize{\large}
\newcommand\subsectionsize{\normalsize}
\newcommand\subsubsectionsize{\normalsize}
\newcounter{firstchapter}
\setcounter{firstchapter}{0}

\setcounter{secnumdepth}{3}    % Number subsubsections in the chapters
\setcounter{tocdepth}{0}       % Put subsubsections in the table of contents

% Print "CHAPTER" if chap option is specified:
\ifchap
  \renewcommand\@chapapp{\chaptername}
\else
  \renewcommand\@chapapp{}
\fi

\def\specialhead#1{%   GENERAL HEADING WITHOUT A NUMBER (for abstract, etc.)
     \ifx\phantomsection\undefined
     \else
        \clearpage\phantomsection
     \fi
     \addcontentsline{toc}{chapter}{#1}
     \chapter*{\centering #1 \@mkboth{#1}{#1}}}


\def\@chapter[#1]#2{\ifnum\c@firstchapter=0    % start of rpi added stuff
                      \if@twoside\cleardoublepage\suppressfloats[t]\fi
                      \pagenumbering{arabic} 
                      \setcounter{firstchapter}{1}
                    \fi
                    \renewcommand{\thepage}{\arabic{page}}
                    \thispagestyle{plain}
                    \pagestyle{myheadings}      % end of rpi added stuff
                    \ifnum \c@secnumdepth >\m@ne
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thechapter.}#1}%
                    \else
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi
                    \chaptermark{#1}% 
%                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
%                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\def\@makechapterhead#1{%
  \vspace*{0\p@}%
  {\parindent \z@ \raggedright \centering \normalfont \chaptersize
    \ifnum \c@secnumdepth >\m@ne
      \ifchap
         \bfseries \@chapapp{} \thechapter    % print "Chapter" and number
         \vskip -3pt           %\par\nobreak (original)
       \else
         \bfseries \thechapter. 
       \fi
    \fi
    \interlinepenalty\@M
    \bfseries #1\par\nobreak
    \vskip 15\p@
  }}

\def\@makeschapterhead#1{%    heading for chapter* command (no numbering)
  \vspace*{0\p@}%
  {\parindent \z@ \raggedright \centering
    \normalfont  \chaptersize 
    \interlinepenalty\@M
    \bfseries  #1\par\nobreak
    \vskip 15\p@
  }}

\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {3.5ex \@plus 1ex \@minus .2ex}%
                                   {.5ex \@plus .3ex}%{1.4ex \@plus.2ex}%
                                   {\normalfont\sectionsize\bfseries}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                     {3.25ex\@plus 1ex \@minus .2ex}%
                                     {.3ex \@plus .2ex}%{1.2ex \@plus .2ex}%
                                     {\normalfont\subsectionsize\bfseries}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                     {3.25ex\@plus 1ex \@minus .2ex}%
                                     {.2ex \@plus .1ex}%{1ex \@plus .2ex}%
                                     {\normalfont\subsubsectionsize\bfseries}}
% \paragraph and \subparagraph headings unchanged from report.cls.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  TABLE of CONTENTS,  LIST OF TABLES,  LIST OF FIGURES 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand\tableofcontents{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \vskip -1em \begin{singlespace}      % singlespacing
    \@starttoc{toc}%
    \if@restonecol\twocolumn\fi
    \end{singlespace}
    }

\renewcommand*\l@chapter{\pagebreak[3]\vskip 10pt plus 1pt minus 1pt
                         \@dottedtocline{0}{0em}{1.4em}}
\renewcommand*\l@section{\vskip 6pt plus 1pt minus 1pt
                         \@dottedtocline{1}{1.5em}{2.3em}}
\renewcommand*\l@subsection{\ifnum\c@tocdepth>1\vskip 4pt minus 1pt \fi
                         \@dottedtocline{2}{3.8em}{3.2em}}
\renewcommand*\l@subsubsection{\ifnum\c@tocdepth>2 \vskip 3pt minus 1pt \fi
                         \@dottedtocline{3}{7.0em}{4.1em}}

% The following removed because it's not consistent with entries from longtable
%% modify the definition below (taken from latex.ltx) to include 
%% "Table" and "Figure" in entries for lot and lof: 
%\long\def\@caption#1[#2]#3{%\baselineskip 14.5 pt
%  \addcontentsline{\csname ext@#1\endcsname}{#1}%
%  {\protect\numberline{\csname fnum@#1\endcsname}{\ignorespaces #2}}%
%  \begingroup
%    \@parboxrestore
%    \normalsize
%    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
%  \endgroup}

\renewcommand\listoffigures{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \ifx\phantomsection\undefined
      \else
        \clearpage\phantomsection
      \fi
    \addcontentsline{toc}{chapter}{\listfigurename}  % add lof to toc
    \chapter*{\centering\listfigurename  % center it
      \@mkboth{\MakeUppercase\listfigurename}%
              {\MakeUppercase\listfigurename}}%
    \vskip -1em \begin{singlespace}  % singlespacing
    \@starttoc{lof}%
    \if@restonecol\twocolumn\fi
    \end{singlespace}
    }

\renewcommand\listoftables{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \ifx\phantomsection\undefined
      \else
        \clearpage\phantomsection
      \fi
    \addcontentsline{toc}{chapter}{\listtablename}  % add lot to toc
    \chapter*{\centering\listtablename  % center it
      \@mkboth{\MakeUppercase\listtablename}%
              {\MakeUppercase\listtablename}}%
        \vskip -1em \begin{singlespace}  % singlespacing
    \@starttoc{lot}%
    \if@restonecol\twocolumn\fi
    \end{singlespace}
    }

%remove following at same time as remove code to put "Figure" in LOF
%\renewcommand*\l@figure{\vskip 10pt plus 1pt minus 1pt
%                       \@dottedtocline{1}{0em}{5.8em}}

\renewcommand*\l@figure{\vskip 10pt plus 1pt minus 1pt
                       \@dottedtocline{1}{0em}{2.8em}}
\let\l@table\l@figure

\let\rpicaption\caption
\let\lrpicaption\caption


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  BIBLIOGRAPHY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RPI def: for use in making an unnumbered bibliography with hanging indents
\def\bibentry{\vskip10pt\par\noindent\hangindent=40pt\frenchspacing}

% report definition modified for no automatic heading and use ragged right
\newcommand{\bibalign}{\raggedright}

\renewenvironment{thebibliography}[1]
     {\bibalign\frenchspacing
      \list{\@biblabel{\@arabic\c@enumiv}}%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \@openbib@code
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\@arabic\c@enumiv}}%
      \sloppy\clubpenalty4000\widowpenalty4000%
      \sfcode`\.=\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \endlist}
3

2 Answers 2

32

If you wish to selectively include/exclude things in the ToC that differ from your "default" choice for tocdepth, then you need to insert these variations into the ToC file itself. The reason for this is because the ToC is processed before (and therefore separate from) the sectional units. So, by the time you're possibly making momentary changes to tocdepth, the ToC has already been typeset.

Here's a small example of how you could incorporate an inclusion of a \section even though tocdepth has been set to 0 (only allowing \chapters to be included):

enter image description here

\documentclass{report}
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\setcounter{tocdepth}{0}% Allow only \chapter in ToC
\begin{document}
\tableofcontents
\chapter{Introduction}
\section{A section}
\chapter{Another chapter}
\chapter{Yet another chapter}
\chapter{Test}
\chapter{Applications}
\addtocontents{toc}{\setcounter{tocdepth}{1}}% Allow \section in ToC
\section{Another section}
\addtocontents{toc}{\setcounter{tocdepth}{0}}% Allow only \chapter in ToC
\chapter{References}
\end{document}

You also need to update your \l@section macro to only insert a vertical space when tocdepth allows it. Here's update version:

\makeatletter
% Original \l@section:
%\renewcommand*\l@section{\vskip 6pt plus 1pt minus 1pt
%                         \@dottedtocline{1}{1.5em}{2.3em}}
% Modified \l@section:
\renewcommand*\l@section{\ifnum\c@tocdepth>\z@\vskip 6pt plus 1pt minus 1pt \fi
                         \@dottedtocline{1}{1.5em}{2.3em}}
\makeatother
4
  • adding \addtocontents{toc}{\setcounter{tocdepth}{0}} after the section, which should not be in the table of content does not solve my probelm. I still get this gap between "Application" and "References"
    – m. th
    Jun 8, 2012 at 17:08
  • 1
    @math: You need to have a modified version of \l@section which conditionally inserts \vskip. Yours currently inserts it regardless of whether tocdepth is 0 or not. That's what causing the strange spacing in your ToC. I've included an updated version at the end of my answer.
    – Werner
    Jun 8, 2012 at 17:32
  • @ Wener: Now everything is fine! Thank you so much!
    – m. th
    Jun 8, 2012 at 17:43
  • 3
    Quick comment in case others have the same issue with this that I did: if you are using certain other packages, you may have to \protect the \setcounter as per this answer. Apr 5, 2014 at 13:03
11

before the chapter for which you want to include the sections in the toc, add the line

\addtocontents{toc}{\setcounter{tocdepth}{1}}

you can later reset it to include just the chapters with

\addtocontents{toc}{\setcounter{tocdepth}{0}}
6
  • @ barbara beeton: Thank you for your answer. I tried to use \addtocontents{toc}{\setcounter{tocdepth}{0}} in the section. The gap is still there in the table of content. Maybe my question was not that clear. I edited it.
    – m. th
    Jun 8, 2012 at 17:32
  • @math -- thanks for editing the question; i interpreted it differently when i first read it. i tried to replicate your problem with the report class (since i don't have thesis), and the problem doesn't occur there. so werner's interpretation and solution are applicable. Jun 8, 2012 at 17:48
  • @ barbara beeton: mea culpa for the unclarity. Nevertheless, thank you for help!
    – m. th
    Jun 8, 2012 at 18:44
  • It seems that using this will render the \listoffigures blank. Any idea on this? I am setting it to 0 just before including some javadoc API pdf, then resetting it back to 5.
    – Timotei
    Jul 3, 2013 at 12:26
  • @Timotei -- sorry, i have no idea why something added explicitly to the toc would affect the \listoffigures. are you saying you added \addtocontents{lof}{...}? is there anything in your .lof file? i'm not sure how tocdepth interacts with lists of figures or tables. Jul 3, 2013 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .