51

I would like to know how I can hide a section from the table of contents but without loosing the section number in the body of the document. For example, in this tex file I loose the number for hide, and all the sequences are damaged:

\documentclass{article}

\begin{document}
\tableofcontents
\section{uno}
\section{dos}
\section*{hide}
\section{tres}
\end{document}
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
mjsr
  • 7,410
  • 18
  • 57
  • 83
  • it will be nice also ommit the page number in the right..i just need SectionName.................SectionNumber SectionName2................SectionNumber2 ...etc. – mjsr May 06 '10 at 23:54

3 Answers3

75

I think you are looking for

\section*{hide}
\addtocounter{section}{1}

or make it into a command:

\newcommand{\toclesssection}[1]{\section*{#1}\addtocounter{section}{1}}

EDIT:

Okay, I think I understand what is wanted now (and it makes more sense then the answer I gave). Here is a command that you can use to suppress adding a section, subsection, etc. to the TOC. The idea is to temporarily disable \addcontentsline.

\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
...
\tocless\section{hide}
\tocless\subsection{subhide}
Ivan Andrus
  • 5,221
  • 24
  • 31
  • 1
    OP said "without losing the section number in the body" – Geoff May 07 '10 at 13:15
  • 1
    the problem remains and appear other incongruency. in Toc the section tres has the number 3, in the body has the number 4. – mjsr May 07 '10 at 15:11
  • Sorry, I misunderstood what was wanted. – Ivan Andrus May 07 '10 at 16:18
  • 1
    mmm I don't understand the logic in the set of command, can you give me a hint in English?...im trying to use the tocless command but doesn't work. – mjsr May 07 '10 at 21:46
  • Dang it! I forgot to add the \nocontentsline command. I don't know what my problem is. Anyway, the idea is to set \addcontentsline to a no-op when evaluating the \section command. – Ivan Andrus May 08 '10 at 04:34
  • 8
    A small improvement: to reference the hidden sections elsewhere in the document you need to add the `\label` inside of the group. For example, `\newcommand{\toclesslab}[3]{\bgroup\let\addcontentsline=\nocontentsline#1{#2\label{#3}}\egroup}` will fix it. Usage: `\toclesslab\section{Motivation}{s:motivation}` will keep the section from appearing the TOC, but you can still reference it with `\ref{s:motivation}` or similar. – Nathan VanHoudnos Apr 25 '14 at 15:49
  • 2
    See http://stackoverflow.com/a/3805470/431528 for an answer which doesn't cause formatting issues. – hiddensunset4 Sep 29 '16 at 13:30
  • thanks! works like a charm for subsubsections as well :-) – Sickboy Jun 29 '17 at 17:56
  • my opinion is the EDIT should be first in this answer - in short, define `\tocless` then use it in front of whatever – Richard DiSalvo Jun 25 '19 at 22:29
6

Just wanted to say thanks for Ivans great hint! (I was just googling for something similar for my customized (Sub)Appendix{} commands:

\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}

\newcommand{\Appendix}[1]{
  \refstepcounter{section}
  \section*{Appendix \thesection:\hspace*{1.5ex} #1}
  \addcontentsline{toc}{section}{Appendix \thesection}
}
\newcommand{\SubAppendix}[1]{\tocless\subsection{#1}}

Maybe this is useful for someone else, too...)

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Daniel
  • 69
  • 1
3

have just come here from a similar question. The answer above didn't quite work as it gave some formatting issues, but a similar solution seemed to do the trick

Community
  • 1
  • 1
second
  • 28,029
  • 7
  • 75
  • 76