1

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}
\titlecontents{section}{\thecontentslabel\quad}{\underline}{}{\hfill\thecontentspage}
\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.

O0123
  • 1,773

1 Answers1

1

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
}%
\def\ulxxx\hyper@linkstart#1#2#3\hyper@linkend\@ulxxxend{%
\hyper@linkstart{#1}{#2}{\ul{#3}}\hyper@linkend
}%
\makeatother

\usepackage[colorlinks]{hyperref}
\titlecontents{section}[0em]{}{\thecontentslabel\quad\myul}{\myul}{\hfill\contentspage}

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

enter image description here

Bernard
  • 271,350
  • Thanks! This answer is incompatible with \usepackage{hyperref} though... – O0123 May 25 '16 at 06:21
  • Could you explain in what way it is incompatible? – Bernard May 25 '16 at 09: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 }. – O0123 May 25 '16 at 09:25
  • @Vincent Verheyen: Problem solved (Heiko Oberdiek's hack)! Please take a look at my updated answer. – Bernard May 25 '16 at 10:09