# Excluding subsection from ToC and keep the references

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

• Don't follow the advice in the link. Rather set \setcounter{tocdepth}{1}. – Werner Mar 6 '14 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. – user3389689 Mar 7 '14 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 '14 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. – user3389689 Mar 11 '14 at 8:37

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.

\documentclass{article}

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

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

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}
• 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! – user3389689 Mar 11 '14 at 8:27

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}