5

I need to remove my subsection from ToC. I got that part right without any problems by using this link. Thanks!

But I am unable to use \ref{"label from my subsection"} for the selected subsection I just removed from my ToC. Well, I can still link it, but I will only get the reference to the section above my subsection.

Example: (My subsection isn't shown in ToC)

\section{Hello World!}
\label{sec:hello_world}

\subsection{Goodbye}
\label{sec:goodbye}

\ref{sec:goodbye}

--- Latex will return with the result as below ---

1. Hello World!

1.1. Goodbye

1

4
  • 1
    Don't follow the advice in the link. Rather set \setcounter{tocdepth}{1}.
    – Werner
    Mar 6, 2014 at 22:00
  • The setcounter is better indeed though I still want the toc depht to be unspoiled for all my other sections, subsection etc. Is there any way you can apply a specific tocdepth for one section only. I was thinking something like: \setcounter{tocdepth}{1}{References} But it doesn't work either. Mar 7, 2014 at 9:08
  • Removing only a certain subsection from the toc sounds like a bad idea. Any reader will assume an error. If you inform us a bit more about the reasons, maybe someone here will come up with a better idea.
    – Keks Dose
    Mar 7, 2014 at 18:06
  • Removing subsections from TOC are a bad idea since it will confuse the reader. I agree with that. But in my case the subsections are used to define my references from websites and books. I need all my references to be at least \subsubsections{www.stackexchange.com} or above to keep the numbers intact when I use \ref{"%"}. I use lot of these references in my document to point out where the literature is used. The question would have been more precise in the first place if I have stated this. Mar 11, 2014 at 8:37

3 Answers 3

6

If you want all \subsections to be removed from the ToC, then all you need in your document preamble is

\setcounter{tocdepth}{1}

which would set the maximum depth of entries in the ToC to 1 (or \section).

If you only want selected \subsections to be inserted/not in the ToC, then you should use a different technique. The following defines \stoptocentries and \starttocentries. The former disables inserting content in the ToC by disabling \addcontentsline. The latter restores the functionality.

enter image description here

\documentclass{article}

\let\oldaddcontentsline\addcontentsline
\newcommand{\stoptocentries}{\renewcommand{\addcontentsline}[3]{}}
\newcommand{\starttocentries}{\let\addcontentsline\oldaddcontentsline}

\begin{document}
\tableofcontents
\section{A section}
\stoptocentries% Stop adding content to the ToC
\subsection{A subsection}
\starttocentries% Resume adding content to the ToC
\subsection{Another subsection}
\end{document}

Of course, usage of \stoptocentries also pertains to other sectional units that use \addcontentsline.

2

You can do this out of the box with package etoc.

emove sub section

The code:

\documentclass{article}
\usepackage{etoc}
\begin{document}

\tableofcontents

\section{First Section}

Hello

\subsection{A}

\subsection{B}

\section{Second Section}

\etocsettocdepth.toc {section}
\subsection {EXCLUDE ME}\label{ssec:excluded}
\etocsettocdepth.toc {subsection}

\subsection {D}

Too bad we have excluded subsection \ref{ssec:excluded} from the TOC!


\end{document}
1
  • Hi Werner, It is excatly what I want. Though the reader might find the section weird since it is missing in the TOC as Keks Dose pointed out. But thanks! Mar 11, 2014 at 8:27
0

I see that the above answer completely answers the question, but I found another method on the LaTeX website.

If you want to hide all subsections inside the toc, you should set the counter tocdepth to 1:

\setcounter{tocdepth}{1}

But if you only want this to apply to certain (sub)sections, you can change tocdepth inside the document:

\documentclass[a4paper,10pt]{book}
\begin{document}
\tableofcontents
\chapter{One}
\section{One}
\subsection{One}
\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\subsection{Two}
\addtocontents{toc}{\protect\setcounter{tocdepth}{2}}
\subsection{Three}
\end{document}

Minimal worked example can also be found on the LaTeX website.

You must log in to answer this question.

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