# Incompatibility of \setcounter{secnumdepth}{0} with \titlecontents{section}{\thecontentslabel\quad}{\underline}{}{\hfill\thecontentspage}

The following (other than the \setcounter{secnumdepth}{0}) is taken from User math's answer in Underline the section title text in table of contents.

\documentclass{article}
\usepackage{titletoc}
% The line below seems to be incompatible with the underlining of the line beneath it
% \setcounter{secnumdepth}{0}
\begin{document}
\tableofcontents
\section{Foobar}
\end{document}


If one un-comments the commented line of code, one can not have both \setcounter{secnumdepth}{0}, and have the underlining working at the same time.

I wonder why, and whether there is a way to circumvent this, without resorting to the soulpackage (as had done User math in his answer; and others; in the mentioned OP), thus by still using the 2 currently incompatible lines up to a great extent at least.

There is no incompatibility, only errors in the syntax for \titlecontents.

The first argument is for code preceding the section label. Furthermore, there an optional-mandatory argument, which is the distance form the left margin. Also, don't use \underline here: you might have long titles, and \underline does not break across lines. Use \ul from soul(utf8) instead.

Edit:

However, there is an incompatibility if using hyperref. I added a hack, due to Heiko Oberbiek, to circumvent the problem.

\documentclass{article}
\usepackage[utf8]{inputenc} \usepackage{titletoc}
\usepackage{soulutf8} % The line below seems to be incompatible with the underlining of the line beneath it
\setcounter{secnumdepth}{0}
\makeatletter
\def\myul#1{%
\ulxxx#1\@ulxxxend
}%
}%
\makeatother

\begin{document}

\tableofcontents

\section{Foobar. A quite long, long section title. A still longer, longer, longer subsection title}
Some test.
\newpage

\section{Another, shorter, section}
Some more text.

\end{document}


• Thanks! This answer is incompatible with \usepackage{hyperref} though... – Vincent Mia Edie Verheyen May 25 '16 at 6:21
• Could you explain in what way it is incompatible? – Bernard May 25 '16 at 9:22
• If you add that line (\usepackage{hyperref}) in your MWE, then it is no longer compilable (I'm on Texshop, using LaTeX as a Program, where it still compiles once, but not twice and more). I get the following error, from the .toc: \hyper@linkstart has an extra }`. – Vincent Mia Edie Verheyen May 25 '16 at 9:25
• @Vincent Verheyen: Problem solved (Heiko Oberdiek's hack)! Please take a look at my updated answer. – Bernard May 25 '16 at 10:09