1

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}

before the word Chapter is added

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
     \addpenalty{-\@highpenalty}%
     \vskip 1.0em \@plus\p@
     \setlength\@tempdima{1.5em}%
     \begingroup
     \settowidth{\@tempdimb}{\normalsize\bfseries \chaptername~}%
     \advance\leftskip by \@tempdimb
     \parindent \z@ \rightskip \@pnumwidth
     \parfillskip -\@tempdima
     \leavevmode \bfseries
     \advance\leftskip\@tempdima
     \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
 \addpenalty{-\@highpenalty}%
 \vskip 1.0em \@plus\p@
 \setlength\@tempdima{1.5em}%
 \begingroup
 \settowidth{\@tempdimb}{\normalsize\bfseries \chaptername~}%
 \advance\leftskip by \@tempdimb
 \parindent \z@ \rightskip \@pnumwidth
 \parfillskip -\@tempdima
 \leavevmode \bfseries
 \advance\leftskip\@tempdima
 \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}

After Implementing the code (After the Word Chapter is Added)

  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 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 at 21:44
  • 1
    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 at 21:54
  • @bernard excuse me i forgot to mention that! I am using the one sided option – Ahmed Sayed Jul 12 at 22:25
  • @Bernard and i am not using fencyheaders – Ahmed Sayed Jul 12 at 22:55
3

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
 \addpenalty{-\@highpenalty}%
 \vskip 1.0em \@plus\p@
 \setlength\@tempdima{1.5em}%
 \begingroup
   \let\oldnumberline=\numberline
   \def\numberline{\chaptername~\oldnumberline}%
   \settowidth{\@tempdimb}{\normalsize\bfseries \chaptername~}%
   \advance\leftskip by \@tempdimb
   \parindent \z@ \rightskip \@pnumwidth
   \parfillskip -\@tempdima
   \leavevmode \bfseries
   \advance\leftskip\@tempdima
   \hskip -\leftskip
   #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
   \penalty\@highpenalty
 \endgroup
 \fi}

 \def\ps@plain{% first page of a chapter
      \def\@oddhead{\normalfont\hfil\thepage}%
      \let\@evenhead\@oddhead
      \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.}%
                         \settowidth{\tocindent}{\normalsize\bfseries \@chapapp\space}% complute additonal indentation
                         \addtocontents{toc}{\protect\tocindent=\the\tocindent}% store in TOC
                         \addcontentsline{toc}{chapter}%
                                   {\@chapapp~\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}

\renewcommand*\l@chapter[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
      \advance\leftskip\tocindent% only change
      \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
      \def\@oddhead{\normalfont\hfil\thepage}%
      \let\@evenhead\@oddhead
      \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 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 at 16:25
1

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

enter image description here

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.