# Add “chapter” word into TOC — Page number

I am tired of not successfully doing it right !! I am working on my thesis and I am using this template https://github.com/kks32/phd-thesis-template/. However, there are some changes I need to implement and I could not successfully do it myself.

1. I need to add the word chapter before the chapter number and chapter name in the table of content (using tocbibind package). This part is similar to this post. I already tried this code before but it added the word chapter before every level-1 heading and I want it to be limited to only the main chapters without the list of figures, acronyms or even the appendices.

below is the MWE:

       \documentclass{report}
\usepackage{tocbibind}

\begin{document}
\tableofcontents

\section{test 1}
First document. This is a simple example, with no
extra parameters or packages included.
\end{document}


I then added the following code to the template, it executed and gave what I want yet there is the word chapter before every level-one title in the TOC.

     \makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\settowidth{\@tempdimb}{\normalsize\bfseries \chaptername~}%
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@tempdima
\leavevmode \bfseries
\hskip -\leftskip
\chaptername~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}
\makeatother


Below is the last version after modification:

   \documentclass{report}
\usepackage{tocbibind}

\makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\settowidth{\@tempdimb}{\normalsize\bfseries \chaptername~}%
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@tempdima
\leavevmode \bfseries
\hskip -\leftskip
\chaptername~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}
\makeatother

\begin{document}
\tableofcontents

\chapter{Test 1}
First document. This is a simple example, with no extra parameters or packages included.
\end{document}


1. In chapters first pages, there are no page numbers and I want the page number to be visible at the top right of the page... I have been trying to figure out where is the exact code that suppresses the page number from being visible at the chapter title page with no success.
• Could you post a minimal, yet compilable , code? – Bernard Jul 12 '19 at 21:26
• @Bernard Excuse me !! could you please specify what do you want specifically ?! I am sorry i cant understand how minimal!? – Ahmed Sayed Jul 12 '19 at 21:44
• Just something short reproducing your problems. I have small idea concerning the page number on first pages of chapters, but I need to know which class options you have set (such as two-sided or one-sided, open right or open any, &c.) – Bernard Jul 12 '19 at 21:54
• @bernard excuse me i forgot to mention that! I am using the one sided option – Ahmed Sayed Jul 12 '19 at 22:25
• @Bernard and i am not using fencyheaders – Ahmed Sayed Jul 12 '19 at 22:55

Looking at the aux file I noticed that normal chapters used \numberline and others didn't. So I moved the addition of \chaptername to be inside \numberline.

\documentclass{report}
\usepackage{tocbibind}

\makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\let\oldnumberline=\numberline
\def\numberline{\chaptername~\oldnumberline}%
\settowidth{\@tempdimb}{\normalsize\bfseries \chaptername~}%
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@tempdima
\leavevmode \bfseries
\hskip -\leftskip
#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}

\def\ps@plain{% first page of a chapter
\let\@oddfoot\@empty
\let\@evenfoot\@empty}
\makeatother

\begin{document}
\tableofcontents

\chapter{Test 1}
First document. This is a simple example, with no extra parameters or packages included.
\end{document}


This solution adds the chapter/appendix name (\@chapapp) inside \addcontentsline. It uses a separate call to \addtocontents to set the indentation for long titles.

\documentclass{report}
\usepackage{tocbibind}
\usepackage{appendix}

\newlength{\tocindent}

\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
{\@chapapp~\protect\numberline{\thechapter}#1}%
\else
\fi
\chaptermark{#1}%
\if@twocolumn
\else
\fi}

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

\def\ps@plain{% first page of chapter
\let\@oddfoot\@empty
\let\@evenfoot\@empty}
\makeatother

\begin{document}
\tableofcontents

\chapter{Test 1}
First document. This is a simple example, with no extra parameters or packages included.

\chapter{This is a very long chapter title, which goes on and on until the next line}

\appendix
\def\chaptername{Appendix}
\chapter{Test 2}
\end{document}

• Thank you! It worked for me !! could you update it for the community to exclude the appendices as well !! Appendices have \numberline as well !! yet the word Appendix is written before the \number line in the aux file – Ahmed Sayed Jul 13 '19 at 17:11
• That would require an entirely different approach, changing the \definition of \chapter or perhaps \addcontentsline instead of \l@chapter. – John Kormylo Jul 14 '19 at 16:25

Here is a suggestion using package tocbasic:

\documentclass{report}
\usepackage{tocbibind}

\usepackage{tocbasic}
\DeclareTOCStyleEntry[
entrynumberformat=\entrywithprefix{\chaptername},
dynnumwidth
]{tocline}{chapter}
\newcommand*\entrywithprefix[2]{#1~#2}

\begin{document}
\tableofcontents
\chapter{Foo}

\section{Bar}
First document. This is a simple example, with no
extra parameters or packages included.
\end{document}


Run three times to get