Here's one possible solution (feel free to adapt it according to your needs); the tocloft
package was used to change the formatting for chapter entries in the ToC and the sectsty
package was used to change the formatting for chapter headings in the document:
\documentclass{book}
\usepackage{sectsty}
\usepackage{tocloft}
\usepackage{lipsum}
\newlength\mylen
\renewcommand\cftchappresnum{\chaptername~}
\settowidth\mylen{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylen}
\renewcommand\cftchapaftersnumb{\\\hspace*{\dimexpr-\mylen-1.5em\relax}}
\addtocontents{toc}{\cftpagenumbersoff{chapter}}
\makeatletter
\renewcommand{\@tocrmarg}{0em}
\renewcommand\cftchapafterpnum{%
\par\vskip-1ex\hspace*{\dimexpr-\mylen-1.5em\relax}\hrulefill\par}
\makeatother
\renewcommand\cftchapfont{\sffamily}
\chapterfont{\sffamily}
\begin{document}
\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One One}
\section{Test Section One Two}
\chapter{Test Chapter Two}
\section{Test Section Two One}
\section{Test Section Two Two}
\end{document}
tocloft
inserts \cftsecpresnum
before placing the section number, and \cftsecaftersnum
after it. These are hooks you can use to tie into and grab the number:
\documentclass{book}
\usepackage{tocloft}% http://ctan.org/pkg/tocloft
\makeatletter
\renewcommand{\cftsecpresnum}{\begin{lrbox}{\@tempboxa}}
\renewcommand{\cftsecaftersnum}{\end{lrbox}}
\makeatother
\begin{document}
\tableofcontents
\chapter{A chapter}
\section{First section}
\section{Second section}
\section{Last section}
\end{document}
We effective wrap the numbering mechanism of \section
within the ToC inside a box \@tempboxa
and store it, but never use it again. This gobbles the contents.
Note that this redefinition is global in terms of the section number display. It can be made local if you only want to display specific parts with/without section numbers. It also maintains the numbering width (set by \cftsecnumwidth
). If this should be removed, add
\setlength{\cftsecnumwidth}{0pt}
to the document preamble as well.
Best Answer
Update: for a solution working with hyperref, see below.
An option, patching
\l@section
:If your section titles contain math or commands (such as
\label
), it's best to use\MakeTextUppercase
from thetextcase
package:The solutions above won't work if the
hyperref
package is used. In this case, one can tamper with\contentsline
(this borrows some code from Heiko Oberdiek's answer to this question incomp.text.tex
):The above approach also works using
\MakeUppercase
instead of\MakeTextUppercase
; if this is so, thentextcase
is not necessary.