Hopefully this is easy. I would like to underline the text of the section title in the table of contents (with the section number removed). Similar to this picture, with the red line where the underline should be:

Please note that I don't want to underline the section title in the document, as per this question, only its entry in the table of contents.

I have tried the following code, but it doesn't work, as \underline expects the text to be supplied as a parameter.

\documentclass[a4paper]{article}

\usepackage{lipsum}
\RequirePackage[titles]{tocloft}

\makeatletter
\renewcommand{\cftsecpresnum}{\begin{lrbox}{\@tempboxa}}
\renewcommand{\cftsecaftersnum}{\end{lrbox}}
\setlength{\cftsecnumwidth}{0pt}
\renewcommand{\cftsecfont}{\underline}    % Doesn't work
\makeatother

\begin{document}
\tableofcontents
\section{Section 1 title}
\lipsum[1-2]
\section{Section 2 title}
\lipsum[3-4]
\end{document}

-
The basis of an answer would be simply to use \section[\underline{Section 1 title}]{Section 1 title}. Depending how often you are doing this (hopefully not often), you may want to automate the approach with a new command like \ulsection... –  jon Apr 4 at 14:12

I won't recommend it, as it hard to read: see http://practicaltypography.com/underlining.html You can do this by:

\documentclass{article}
\usepackage{soul,xcolor}
\setul{1ex}{0.8ex}
\setulcolor{red}
\begin{document}
\tableofcontents
\section[\ul{Foobar}]{Foobar}
\end{document}


EDIT: Maybe this helps, but I didn't get it managed to work with soul, nor to get the correct spacing:

\documentclass{article}
\usepackage{titletoc}
\begin{document}
\tableofcontents
\section{Foobar}
\end{document}


-
Thanks, but is there a way of doing it outside of the document body? I'm trying to do all my formatting in a class, and apply that to a number of documents that have minimal formatting. –  abeverley Apr 4 at 14:20
@abeverley -- Do you want all sections underlined, or only some? If only some, compose your own macro: \newcommand{\ulsection}[1]{\section[\ul{#1}]{#2}. Otherwise, you want to redefine the \seciton command more generally. –  jon Apr 4 at 14:23
@abeverley maybe this helps: tex.stackexchange.com/questions/112006/… –  math Apr 4 at 14:29
@jon - yes, every section title in the TOC. I'm trying with \renewcommand{\section}[1]{\section[\underline{#1}]{#1}} but I get the error TeX capacity exceeded, sorry [input stack size=5000] - is that the correct way of doing it? –  abeverley Apr 4 at 14:38
@math - yes, thanks, I didn't come across that despite many searches. However, it's a lot more complicated than the methods discussed above! –  abeverley Apr 4 at 14:42

If you want to suppress the number, or alternatively include it in the underlining you may try this. If your section titles are short enough then, using souls \ul (which may have problems depending on the encountered material) is too much, a simple \underline or home-made box+rule would work.

\documentclass[a4paper]{article}
\usepackage{color}
\usepackage{soul}
\usepackage{lipsum}
\usepackage{etoc}

\makeatletter
\newcommand*\TableOfContents {%
\setul{1ex}{0.8ex}
\setulcolor{red}
\let\standardpartline\l@part
\let\standardsectionline\l@section
\let\standardsubsectionline\l@subsection
\etocsetstyle{part}{}{}
{\standardpartline{\etocnumber\hspace{1em}\etocname}{\etocpage}}{}%
%
\etocsetstyle{section}{}{}
{\standardsectionline{\rule[-3ex]{0pt}{0pt}% FOR EXTRA VERTICAL SPACE
\expandafter\ul\expandafter{\etocthename}}{\etocpage}}{}%
%
\etocsetstyle{subsection}{}{}
{\standardsubsectionline{\numberline{\etocnumber}\etocname}{\etocpage}}{}%
\tableofcontents
}

\makeatother

\begin{document}
\TableOfContents

\section{Section 1 title}

\lipsum[1]

\subsection{Subsection 1 title}

\lipsum[2]

\section{Section 2 title}

\lipsum[3]

\subsection{Subsection 2 title}

\lipsum[4]

\section{A very very very long section title to see what happens them
with the \textbackslash ul macro does it work? well, yes it does.}

\lipsum[3]

\end{document}

-
Interesting - I hadn't come across etoc. Looks like it gives a lot more flexibility, so I'm in the process of trying to convert my TOC formatting to use that package (it will make it easier to do this). Almost there, but having trouble removing the vertical spacing between section titles. –  abeverley Apr 4 at 17:44
@abeverley the code here has \rule[-3ex]{0pt}{0pt} precisely to add some add vertical spacing. Is this what you wnat to get rid of? –  jfbu Apr 4 at 18:32
Yes, thank you @jfbu, I've been staring at this screen too long ;-). Going a bit OT now, but how do you change the title of the TOC from "Contents"? There are advanced examples in the manual, but I can't get them to work. –  abeverley Apr 4 at 18:39
Well, \renewcommand{\contentsname}{New TOC title} should work. This is not etoc specific: \contentsname is provided by the class. –  jfbu Apr 4 at 18:41
Great, thanks, that does it, although any idea why it is indented slightly compared to the section titles in the remainder of the document? I'm guessing it's something to do with being a label-less title? –  abeverley Apr 4 at 19:02