3

Problem:

I wish to have bold style for certain chapter names but not for others.

Minimal Working Example:

\documentclass{book}

\usepackage[titles]{tocloft}

%: ----------------------- Table of contents ------------------------
\renewcommand{\contentsname}{Table of contents}
\renewcommand{\cftchapfont}{\normalfont\bfseries}% titles in bold
\renewcommand{\cftchappagefont}{\normalfont\bfseries}% page numbers in bold
\renewcommand{\cftdotsep}{1}
\renewcommand{\cftchapleader}{\bfseries\cftdotfill{\cftsecdotsep}}% dot leaders in bold

\begin{document}

\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One}
\section{Test Section One}
\subsection{Test Subsection A}
\subsection{Test Subsection B}

\pagenumbering{Roman}
\setcounter{page}{1}
\addcontentsline{toc}{chapter}{Test Chapter Two}

\end{document}

Output:

enter image description here

Desired output:

To get the second chapter name in the table of contents in normal style.

  • Is there a criterion to determine which should be bold (eg. numbered chapters)? – Bernard Mar 10 '18 at 22:38
  • @Bernard No criteria, but if it can facilitate a smooth solution, all chapters at the end which I wish to have in normal style are added with \addcontentsline- is it possible to modify it this way? – kexxcream Mar 11 '18 at 7:00
3

Another way: Writing the change of the \cftchap... etc. macros to the toc and make the font bold or just \normalfont.

I've adapted this for the page font as well as for the leaders...

The macro \enableboldchapterintoc enables this globally until \disableboldchapterintoc is used. Reenable it with another \enableboldchapterintoc etc, where needed.

\documentclass{book}

\usepackage[titles]{tocloft}


\newcommand*{\enableboldchapterintoc}{%
  \addtocontents{toc}{\string\renewcommand{\protect\cftchapfont}{\protect\normalfont\protect\bfseries}}
  \addtocontents{toc}{\string\renewcommand{\protect\cftchappagefont}{\protect\normalfont\protect\bfseries}}
  \addtocontents{toc}{\string\renewcommand{\protect\cftchapleader}{\protect\bfseries\protect\cftdotfill{\protect\cftsecdotsep}}}% dot leaders in bold
}
\newcommand*{\disableboldchapterintoc}{%
  \addtocontents{toc}{\string\renewcommand{\protect\cftchappagefont}{\protect\normalfont}}
  \addtocontents{toc}{\string\renewcommand{\protect\cftchapfont}{\protect\normalfont}}
  \addtocontents{toc}{\string\renewcommand{\protect\cftchapleader}{\protect\normalfont\protect\cftdotfill{\protect\cftsecdotsep}}}% 
}



%: ----------------------- Table of contents ------------------------
\renewcommand{\contentsname}{Table of contents}
\renewcommand{\cftchapfont}{\normalfont\bfseries\fi}% titles in bold
\renewcommand{\cftchappagefont}{\normalfont\bfseries}% page numbers in bold
\renewcommand{\cftdotsep}{1}
\renewcommand{\cftchapleader}{\bfseries\cftdotfill{\cftsecdotsep}}% dot leaders in bold


\begin{document}
\enableboldchapterintoc
\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One}
\section{Test Section One}
\subsection{Test Subsection A}
\subsection{Test Subsection B}

\pagenumbering{Roman}
\setcounter{page}{1}
\disableboldchapterintoc
\addcontentsline{toc}{chapter}{Test Chapter Two}

\enableboldchapterintoc

\chapter{Foo}

\end{document}

enter image description here

  • Spot on, hats off to you sir. – kexxcream Mar 11 '18 at 14:57
  • @kexxcream: You're welcome. Happy TeXing... – user31729 Mar 11 '18 at 14:59
3

You noted in the comments that the normal style chapters are added with \addcontentsline. Because you do not want to format them like normal chapters, you can create a new kind of TOC entry instead of reusing chapter. To do this with tocloft you can mostly copy the definitions for chapter from titletoc.sty. Here I used the name chapterNB:

\makeatletter
\newcommand*{\l@chapterNB}[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip \cftbeforechapNBskip
    {\leftskip \cftchapNBindent\relax
     \rightskip \@tocrmarg
     \parfillskip -\rightskip
     \parindent \cftchapNBindent\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima \cftchapNBnumwidth\relax
     \let\@cftbsnum \cftchapNBpresnum
     \let\@cftasnum \cftchapNBaftersnum
     \let\@cftasnumb \cftchapNBaftersnumb
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {\cftchapNBfont #1}\nobreak
     \cftchapNBfillnum{#2}}%
  \fi}%
\newlength{\cftbeforechapNBskip}
  \setlength{\cftbeforechapNBskip}{\cftbeforechapskip}
\newlength{\cftchapNBindent}
  \setlength{\cftchapNBindent}{\cftchapindent}
\newlength{\cftchapNBnumwidth}
  \setlength{\cftchapNBnumwidth}{\cftchapnumwidth}
\let\cftchapNBfont\cftchapfont
\let\cftchapNBpresnum\cftchappresnum
\let\cftchapNBaftersnum\cftchapaftersnum
\let\cftchapNBaftersnumb\cftchapaftersnumb
\newcommand{\cftchapNBleader}{\bfseries\cftdotfill{\cftchapNBdotsep}}
\let\cftchapNBdotsep\cftchapdotsep
\let\cftchapNBpagefont\cftchappagefont
\let\cftchapNBafterpnum\cftchapafterpnum
\newcommand{\cftchapNBfillnum}[1]{%
  {\cftchapNBleader}\nobreak
  \makebox[\@pnumwidth][\cftpnumalign]{\cftchapNBpagefont #1}\cftchapNBafterpnum\par
}
\makeatletter

After that, you can change the formatting of the new chapterNB:

\renewcommand{\cftchapNBfont}{\normalfont}
\renewcommand{\cftchapNBpagefont}{\normalfont}
\renewcommand{\cftchapNBleader}{\cftdotfill{\cftsecdotsep}}

To add a "chapter" in normal (non-bold) style to the table of contents, you can use:

\addcontentsline{toc}{chapterNB}{Test Chapter Two}

Combined this results in

\documentclass{book}

\usepackage[titles]{tocloft}

%: ----------------------- Table of contents ------------------------
\renewcommand{\contentsname}{Table of contents}
\renewcommand{\cftchapfont}{\normalfont\bfseries}% titles in bold
\renewcommand{\cftchappagefont}{\normalfont\bfseries}% page numbers in bold
\renewcommand{\cftdotsep}{1}
\renewcommand{\cftchapleader}{\bfseries\cftdotfill{\cftsecdotsep}}% dot leaders in bold

\makeatletter
\newcommand*{\l@chapterNB}[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip \cftbeforechapNBskip
    {\leftskip \cftchapNBindent\relax
     \rightskip \@tocrmarg
     \parfillskip -\rightskip
     \parindent \cftchapNBindent\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima \cftchapNBnumwidth\relax
     \let\@cftbsnum \cftchapNBpresnum
     \let\@cftasnum \cftchapNBaftersnum
     \let\@cftasnumb \cftchapNBaftersnumb
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {\cftchapNBfont #1}\nobreak
     \cftchapNBfillnum{#2}}%
  \fi}%
\newlength{\cftbeforechapNBskip}
  \setlength{\cftbeforechapNBskip}{\cftbeforechapskip}
\newlength{\cftchapNBindent}
  \setlength{\cftchapNBindent}{\cftchapindent}
\newlength{\cftchapNBnumwidth}
  \setlength{\cftchapNBnumwidth}{\cftchapnumwidth}
\let\cftchapNBfont\cftchapfont
\let\cftchapNBpresnum\cftchappresnum
\let\cftchapNBaftersnum\cftchapaftersnum
\let\cftchapNBaftersnumb\cftchapaftersnumb
\newcommand{\cftchapNBleader}{\bfseries\cftdotfill{\cftchapNBdotsep}}
\let\cftchapNBdotsep\cftchapdotsep
\let\cftchapNBpagefont\cftchappagefont
\let\cftchapNBafterpnum\cftchapafterpnum
\newcommand{\cftchapNBfillnum}[1]{%
  {\cftchapNBleader}\nobreak
  \makebox[\@pnumwidth][\cftpnumalign]{\cftchapNBpagefont #1}\cftchapNBafterpnum\par
}
\makeatletter
\renewcommand{\cftchapNBfont}{\normalfont}
\renewcommand{\cftchapNBpagefont}{\normalfont}
\renewcommand{\cftchapNBleader}{\cftdotfill{\cftsecdotsep}}% dot leaders in bold

\begin{document}

\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One}
\section{Test Section One}
\subsection{Test Subsection A}
\subsection{Test Subsection B}

\pagenumbering{Roman}
\setcounter{page}{1}
\addcontentsline{toc}{chapterNB}{Test Chapter Two}

\end{document}

The output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.