Exclude numbered section from toc

I want to exclude a section from the table of contents, but would still to assign it a number and be able to refer to it. This was basically adressed here.

However, I also modified my section headers and don't know how this modification can be taken into account when typesetting the section that is excluded from the toc.

\documentclass{article}

\usepackage{titlesec}
\titleformat{\section}{\large\bf}{\thesection}{8pt}{\large}

\begin{document}

\section*{\refstepcounter{section}\label{sec:A}\thesection Section A} % from link provided
\section*{\refstepcounter{section}\label{sec:B}\thesection\hspace{8pt} Section B}
\section{Section C}

\end{document}


The problem is visualised here:

If all you need is to exclude a section from the TOC, you need simply change the tocdepth before you write the section information to the TOC.

\documentclass{article}

\usepackage{titlesec}
\titleformat{\section}{\large\bfseries}{\thesection}{8pt}{\large}
\section[#1]{#2}\restoretoc}

\begin{document}
\tableofcontents

\section{Section A\label{sec:A}}
\notocsection{Section B\label{sec:B}}
\section{Section C}

\end{document}


If you do not want to adapted the spacing in every individual \section*{} you can define your own command for the space

\documentclass{article}

\usepackage{titlesec}
\newcommand{\myspace}{8pt}

\titleformat{\section}{\large\bf}{\thesection}{\myspace}{\large}

\begin{document}

\section*{\refstepcounter{section}\label{sec:A}\thesection\hspace{\myspace}Section A} % from link provided
\section*{\refstepcounter{section}\label{sec:B}\thesection\hspace{\myspace}Section B}
\section{Section C}

\end{document}


• yes, it does. But I was hoping for a way to not have to add the 8pt manually, as I would like to use this in various documents with differing layouts. Will change the question later accordingly – D.Roepo Nov 26 '13 at 14:03
• Modified the answer to have the 8pt stored in only one place – user36296 Nov 26 '13 at 15:46