# Exclude sections with empty titles from TOC

With the preamble that I built from the answer on \section with empty title. Show number next to first paragraph, I use \section{} and related with empty title, which will mark the first paragraph of the section with a number. This is perfect for my use case, however:

How do I exclude the sections with empty title from the TOC?

What I've got so far is this:

\documentclass{report}
\usepackage[a4paper, margin=1.3in]{geometry}

\usepackage{etoolbox}
\usepackage[explicit]{titlesec}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set section numbers in the margin and center the chapter headings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls the hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{1.35cm}

\titleformat{\chapter}[block]
{\normalfont\huge\bfseries\center}{}{0pt}{\hspace*{-\titleindent}#1}

\titleformat{\section}
{\normalfont\Large\bfseries}
{\llap{\parbox[t]{\titleindent}{\thesection\hfill}}}{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr2.3ex+0.5\baselineskip\relax}}
\else#1
\fi
}

\titleformat{\subsection}
{\normalfont\large}{\llap{\parbox[t]{\titleindent}{\thesubsection\hfill}}}
{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
\else\bfseries#1
\fi
}

\titleformat{\subsubsection}
{\normalfont\normalsize}{\llap{\parbox[t]{\titleindent}{\thesubsubsection}}}
{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
\else\bfseries#1
\fi
}

\titleformat{\paragraph}[runin]
{\normalfont\large}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
{\normalfont\normalsize}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\section}
{0pt}{3.5ex}{2.3ex}
\titlespacing*{\subsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{3.25ex}{1.5ex}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make chapters show up in TOC and NOT reset the section counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{chapter}{0}
\newcommand{\mychapter}[1]{
\chapter*{#1}
}

\begin{document}

\tableofcontents
\cleardoublepage

\mychapter{INITIAL CLAUSES}

\section{A Section title}
\subsection{A subsection with a title}
Some text

\subsection{} This subsection has no title, so the numbers are displayed next to this text instead.

\subsection{} These subsections should not be included in the TOC

\end{document}


• I tried it. Does not make a difference. I want to configure the preamble so it checks for empty titles and does not include them – Eldamir Mar 10 '15 at 12:45
• I've updated my post with an example – Eldamir Mar 10 '15 at 13:09

As part of your conditional test to see if the title is empty you can add the code required to change the ToC depth:

\documentclass{report}
\usepackage[a4paper, margin=1.3in]{geometry}

\usepackage{etoolbox}
\usepackage[explicit]{titlesec}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set section numbers in the margin and center the chapter headings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls the hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{1.35cm}

\titleformat{\chapter}[block]
{\normalfont\huge\bfseries\center}{}{0pt}{\hspace*{-\titleindent}#1}

\titleformat{\section}
{\normalfont\Large\bfseries}
{\llap{\parbox[t]{\titleindent}{\thesection\hfill}}}{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr2.3ex+0.5\baselineskip\relax}}
\else#1
\fi
}

\titleformat{\subsection}
{\normalfont\large}{\llap{\parbox[t]{\titleindent}{\thesubsection\hfill}}}
{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
\else\bfseries#1
\fi
}

\titleformat{\subsubsection}
{\normalfont\normalsize}{\llap{\parbox[t]{\titleindent}{\thesubsubsection}}}
{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
\else\bfseries#1
\fi
}

\titleformat{\paragraph}[runin]
{\normalfont\large}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
{\normalfont\normalsize}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\section}
{0pt}{3.5ex}{2.3ex}
\titlespacing*{\subsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{3.25ex}{1.5ex}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make chapters show up in TOC and NOT reset the section counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{chapter}{0}
\newcommand{\mychapter}[1]{
\chapter*{#1}
}

\begin{document}

\tableofcontents
\cleardoublepage

\mychapter{INITIAL CLAUSES}

\section{A section with title}
\subsection{A subsection with a title}
Some text

\subsection{} This subsection has no title, so the numbers are displayed next to this text instead.

\subsection{} These subsections should not be included in the TOC

\subsection{Another subsection with a title}
Some text

\section{} This section has no title
\section{} This section has no title

\section{Another section with title}
\subsection{Another subsection with a title}

\end{document}


Not that I approve what you are doing, but here it is.

You can define two new commands, \emptysection and \emptysubsection

\newcommand{\emptysection}[1]{%
\section{#1}\endgroup%
}

\newcommand{\emptysubsection}[1]{%
\subsection{#1}\endgroup%
}


and use them instead of \section and \subsection when they must have no contents.

MWE:

\documentclass{report}
\usepackage[a4paper, margin=1.3in]{geometry}

\usepackage{etoolbox}
\usepackage[explicit]{titlesec}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set section numbers in the margin and center the chapter headings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls the hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{1.35cm}

\titleformat{\chapter}[block]
{\normalfont\huge\bfseries\center}{}{0pt}{\hspace*{-\titleindent}#1}

\titleformat{\section}
{\normalfont\Large\bfseries}
{\llap{\parbox[t]{\titleindent}{\thesection\hfill}}}{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr2.3ex+0.5\baselineskip\relax}}
\else#1
\fi
}

\titleformat{\subsection}
{\normalfont\large}{\llap{\parbox[t]{\titleindent}{\thesubsection\hfill}}}
{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
\else\bfseries#1
\fi
}

\titleformat{\subsubsection}
{\normalfont\normalsize}{\llap{\parbox[t]{\titleindent}{\thesubsubsection}}}
{0em}
{\if\detokenize{#1}\relax\relax
{\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
\else\bfseries#1
\fi
}

\titleformat{\paragraph}[runin]
{\normalfont\large}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
{\normalfont\normalsize}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\section}
{0pt}{3.5ex}{2.3ex}
\titlespacing*{\subsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{3.25ex}{1.5ex}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make chapters show up in TOC and NOT reset the section counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{chapter}{0}
\newcommand{\mychapter}[1]{
\chapter*{#1}
}

\newcommand{\emptysection}[1]{%
\section{#1}\endgroup%
}

\newcommand{\emptysubsection}[1]{%
\subsection{#1}\endgroup%
}

\begin{document}

\tableofcontents
\cleardoublepage

\mychapter{INITIAL CLAUSES}

\section{A Section title}
\subsection{A subsection with a title}
Some text

\emptysubsection{} This subsection has no title, so the numbers are displayed next to this text instead.

\emptysubsection{} These subsections should not be included in the TOC

\end{document}


Output (ToC):