9

At here Adding the word "Chap" before the chapters numbers in the TOC the word Chapter added in \documentclass{book}. Now I want to use them in \documentclass{report}. How can I do that?

minthao_2011
  • 4,534
  • 7
  • 36
  • 61
  • I don't understand the error. Can you please mention the code? right now in my thesis, \section are used for every chapter which displays chapter like 1 introduction, 2 literature. I'm using this \documentclass[12pt, chapterprefix]{article} \usepackage[a4paper, margin=20mm]{geometry}? – Sajed ahmad Mar 13 '24 at 04:44

2 Answers2

10

Here's the modification for the report class (basically, it's the same code but suppressing the conditional test for the \frontmatter); I defined two commands \AddChap and \SuppChap to activate (deactivate, resp.) the addition:

\documentclass{report}

\makeatletter
\let\orig@chapter\@chapter
\newcommand\SuppChap{%
  \let\@chapter\orig@chapter}
\newcommand\AddChap{%
\def\@chapter[##1]##2{\ifnum \c@secnumdepth >\m@ne
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {Chap~\protect\numberline{\thechapter}##1}%
                       \else
                         \addcontentsline{toc}{chapter}{##1}%
                       \fi
                    \chaptermark{##1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{##2}]%
                    \else
                      \@makechapterhead{##2}%
                      \@afterheading
                    \fi}%
}
\makeatother

\begin{document}

\tableofcontents

\chapter{Acknowledgements}

\AddChap
\chapter{Test Chapter One}
\chapter{Test Chapter Two}

\SuppChap
\appendix
\chapter{Test Appendix One}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
8

A much simpler solution with the tocloft package.

\documentclass{report}
\usepackage{tocloft}

\newlength\mylength

\renewcommand\cftchappresnum{Chap~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}

\begin{document}

\tableofcontents

\chapter{Intro}

\chapter{Test}

\end{document} 

Output (ToC):

enter image description here


EDIT

If you want to add the word "Appendix" instead of "Chap" for your appendices, you can load the package etoolbox and add the following lines in your preamble:

\apptocmd{\appendix}
  {\addtocontents{toc}{%  
   \protect\addtolength\protect\cftchapnumwidth{-\mylength}%
   \protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
   \protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
   \protect\addtolength\protect\cftchapnumwidth{\mylength}}%
  }{}{}

Complete MWE:

\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\apptocmd{\appendix}
  {\addtocontents{toc}{%  
   \protect\addtolength\protect\cftchapnumwidth{-\mylength}%
   \protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
   \protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
   \protect\addtolength\protect\cftchapnumwidth{\mylength}}%
  }{}{}

\newlength\mylength

\renewcommand\cftchappresnum{Chap~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}

\begin{document}

\tableofcontents

\chapter{Intro}

\chapter{Test}

\appendix

\chapter{Conclusions}

\end{document} 

Output (ToC):

enter image description here

karlkoeller
  • 124,410