# Change the chapter font in TOC without using “tocloft” package

As using tocloft in book class produces some unexpected side-effect, I wonder how to change the chapter font (e.g. 1 Introduction) as sans serif without using any other packages?

For example, change the highlighted text to sans serif font.

MWE:

\documentclass{book}
\usepackage[UKenglish]{babel}
\usepackage{color}
\usepackage[lining ]{libertine}
\usepackage[ T1 ]{fontenc}

\definecolor{DarkBlue}{RGB}{0,51,153}
\usepackage[ ]{titlesec}
\titleformat{\chapter}[display]
{\LARGE \color{DarkBlue}}
{\vspace{-1 em} \flushright   \normalsize \color{black}  \MakeUppercase{  \bfseries \sffamily  \chaptertitlename } \hspace{1 em}  { \fontsize{70}{70}\selectfont \color{black} \sffamily \thechapter }} {20 pt} { \bfseries \sffamily \LARGE}

\titleformat{\section} {\Large \bfseries \sffamily \color{DarkBlue}}     {\thesection}{1 em}{}
\titleformat{\subsection}  {\large \bfseries \sffamily  \color{DarkBlue}}      {\thesubsection}{1 em}{}
\titleformat{\subsubsection}  {\normalsize \sffamily  \bfseries      \color{DarkBlue}}  {\thesubsubsection}{1 em}{}  %\slshape
\setcounter{tocdepth}{3}   % table of content depth
\setcounter{secnumdepth}{3}

\usepackage{lipsum}

\begin{document}

\frontmatter
\tableofcontents
\chapter{Preface}

\lipsum[1-3]
\mainmatter
\chapter{Introduction}
\section{Foo}
\lipsum[1-12]
\chapter{How to make nuclear bomb}
\section{Equations}
\lipsum[1-12]
\backmatter
\chapter{Appendix~A}
\lipsum[1-12]
\chapter{Appendix~B}
\lipsum[1-12]
\end{document}


-
Without using any package or also without providing a minimal working example (MWE)? – lockstep Feb 5 '13 at 16:29
The simplest manner would be to redefine \l@chapter, the document class must be known before saying more. – jfbu Feb 5 '13 at 16:30
What is the unexpected side-effct you are seeing? – Peter Wilson Feb 5 '13 at 19:06
@PeterWilson, the spacing of TOC is changed as well as the blank page for LOF,LOT with book class – KOF Feb 5 '13 at 19:54
I don't think that the spacing of the TOC is changed. The manual does say that with chapters (e.g., book class) the LOF and LOT do not necessarily start on new pages. If you want that then add \clearpage or \cleardoublepage before calling for the LOF or LOT. – Peter Wilson Feb 5 '13 at 20:49

This is not really tested for lack of copy-pastable code, but should work in the book class. Put in the preamble:

\makeatletter
\def\l@chapter#1#2{%
\ifnum \c@tocdepth >\m@ne \addpenalty {-\@highpenalty }\vskip 1.0em \@plus\p@
\setlength \@tempdima {1.5em}\begingroup \parindent \z@ \rightskip\@pnumwidth
\parfillskip -\@pnumwidth \leavevmode \bfseries \advance \leftskip \@tempdima
\hskip -\leftskip {\sffamily #1}\nobreak \hfil \nobreak \hb@xt@ \@pnumwidth
{\hss #2}\par \penalty \@highpenalty \endgroup \fi}
\makeatother


Update: if you want to achieve similar effect with memoir or scrbook then use rather the following code:

\makeatletter
\let\originall@chapter\l@chapter
\def\l@chapter#1#2{\originall@chapter{{\sffamily #1}}{#2}}
\makeatother


The above is actually preferable to the one I proposed earlier, also for the book class, as it will less encumber your preamble. Nevertheless the advantage of the first proposal is that you can also in case of need customize the hard-coded lengths therein.

\documentclass{book}
\usepackage{lipsum}

\makeatletter \def\l@chapter#1#2{%
\ifnum \c@tocdepth >\m@ne \addpenalty {-\@highpenalty }\vskip 1.0em \@plus\p@
\setlength \@tempdima {1.5em}\begingroup \parindent \z@ \rightskip\@pnumwidth
\parfillskip -\@pnumwidth \leavevmode \bfseries \advance \leftskip \@tempdima
\hskip -\leftskip {\sffamily #1}\nobreak \hfil \nobreak \hb@xt@ \@pnumwidth
{\hss #2}\par \penalty \@highpenalty \endgroup \fi} \makeatother

\begin{document}

\tableofcontents

\chapter{One}

\section{A}

\lipsum[1]

\section{B}

\lipsum[2]

\chapter{Two}

\section{C}

\lipsum[3]

\section{D}

\lipsum[4]

\end{document}


And with the code provided in the question, one obtains:

-
It works perfectly! – KOF Feb 5 '13 at 16:47
rather than redefining \l@chapter the way I did, one could save the old definition, and encapsulate it in a new one with the first parameter #1 replaced by {\sffamily #1}. Both methods are compatible with hyperref. Note though that I had to put \sffamily #1 in a group to limit the scope of the font change. I think this will be in 99.9% of the cases with no after-effect elsewhere. – jfbu Feb 5 '13 at 16:54
@KOF: please not that if you change your document class to memoir or scrbook then the proposed method is not the good one. It would be better to do the alternative I mentioned in my comment. – jfbu Feb 5 '13 at 16:57
only original document classes are used for my writting :), because I don't know what kind of weird settings in these classes. – KOF Feb 5 '13 at 17:22
memoir actually has most (if not all, I don't know) of the tocloft commands and functionalities. Both memoir and scrbook are well documented: memman and scrguien – jfbu Feb 5 '13 at 17:31