5

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:

enter image description here

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}
abeverley
  • 609
  • 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 04 '14 at 14:12

3 Answers3

6

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}

Underlined

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}
\titlecontents{section}{\thecontentslabel\quad}{\underline}{}{\hfill\thecontentspage}
\begin{document}
\tableofcontents
\section{Foobar}
\end{document}

enter image description here

math
  • 3,135
  • 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 04 '14 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 04 '14 at 14:23
  • @abeverley maybe this helps: http://tex.stackexchange.com/questions/112006/change-toc-to-use-underline-in-subsubsections – math Apr 04 '14 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 04 '14 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 04 '14 at 14:42
  • @abeverley -- No. You are creating an infinite loop. Making this work so you can just juse \section is tricky because sectional divisions interact with many different things 'under the hood'. So there is my easy way, which will only work in simple circumstances, or you need to resort to deeper surgery, as in the link above. – jon Apr 04 '14 at 14:57
  • Thanks @math and @jon. I've created a custom command as suggested that does the underlining, which achieves the aim of keeping as much formatting out of the document as possible; I just need to change \section to ulsection, which is not too onerous. – abeverley Apr 04 '14 at 15:21
6

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.

underlined sections in toc

\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 04 '14 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? –  Apr 04 '14 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 04 '14 at 18:39
  • Well, \renewcommand{\contentsname}{New TOC title} should work. This is not etoc specific: \contentsname is provided by the class. –  Apr 04 '14 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 04 '14 at 19:02
  • Hmmm, I've hacked it by adding \titlespacing{name=\section,numberless} with a slightly different offset to the \titlespacing{\section} definition. Would be nice to fix it properly though. – abeverley Apr 04 '14 at 19:12
  • @abeverley you are using titletoc right? etoc here does very little, it just relies on the toc typesetting as customized by titletoc or anything else, and its job is only to remove the number and add \ul to the section title. It can't do anything about your spacing issues. However you can also have etoc take the hand over everything. But here I can't answer your questions, they do not originate in use of etoc. –  Apr 04 '14 at 19:20
  • Okay, thanks, yes using titletoc. I'll stick with my hack for the time being - I've been on this 12 hours now and have had enough! – abeverley Apr 04 '14 at 20:40
0

If you want to underline the whole line (not only the title of the section), you just write like this:

\titlecontents{section}{\thecontentslabel\quad}%
                       {}%
                       {}%
                       {\hfill\thecontentspage}[{\titlerule[1pt]}]%

This feature is included with the default titletoc package.

perror
  • 754
  • 1
  • 8
  • 21