4

Any idea how to add a string "Chapter" to TOC to a selected positions in TOC?

For now, TOC looks like this:

Chapter without any number A
Chapter without any number B
1. This is chapter 1.
2. This is chapter 2.
3. This is chapter 3.
4. This is chapter 4.

I want to add "Chapter" to some of them. The result should be like that:

Chapter without any number A
Chapter without any number B
1. This is chapter 1.
2. This is chapter 2.
Chapter 3. This is chapter 3.
Chapter 4. This is chapter 4.
Werner
  • 603,163
Adam
  • 41

1 Answers1

7

You can patch the macro \l@chapter mid-document using regexpatch and some help from Patching arguments inside a macro:

enter image description here

\documentclass{report}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\makeatletter
\newcommand{\patchlchapter}{%
  \xpatchcmd{\l@chapter}{##1}{Chapter~##1}{}{}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter*{Chapter without any number A}
\addcontentsline{toc}{chapter}{Chapter without any number A}
\chapter*{Chapter without any number B}
\addcontentsline{toc}{chapter}{Chapter without any number B}
\chapter{This is chapter 1}
\chapter{This is chapter 2}
\addtocontents{toc}{\protect\patchlchapter}
\chapter{This is chapter 3}
\chapter{This is chapter 4}
\end{document}

You have to patch this mid-document since the .toc that is read with a call to \tableofcontents is executed all-at-once. A mid-document placement of the patch ensures that it will be executed at the correct spot inside the .toc:

\contentsline {chapter}{Chapter without any number A}{2}
\contentsline {chapter}{Chapter without any number B}{3}
\contentsline {chapter}{\numberline {1}This is chapter 1}{4}
\contentsline {chapter}{\numberline {2}This is chapter 2}{5}
\patchlchapter 
\contentsline {chapter}{\numberline {3}This is chapter 3}{6}
\contentsline {chapter}{\numberline {4}This is chapter 4}{7}
Werner
  • 603,163