TransWikia.com

Table of contents after renaming chapters in book document?

TeX - LaTeX Asked on January 9, 2021

After changing default chapter notation to Lecture, How do I get it to table of contents?
I’m getting only main chapter numbers. But I want my table of content to look like this,

Lecture 1

1.1 Introduction………..3

Lecture 2

2.2 GrahQL……………..5

I have attached a M. W. E. below.

documentclass[12pt,a4paper]{book}
begin{document}
tableofcontents
makeatletter
renewcommand{@chapapp}{Lecture}
makeatother
chapter{}
section{Introduction}
chapter{}
section{GraphQL}
end{document} 

Your help is highly appreciated.

3 Answers

Use the tocloft package.

% lecturetocprob.tex  SE 569160

documentclass[12pt,a4paper]{book}
usepackage{tocloft}
addtolength{cftchapnumwidth}{4em}  % more space for number + Lecture
renewcommand{cftchappresnum}{Lecture }
begin{document}
tableofcontents
makeatletter
renewcommand{@chapapp}{Lecture}
makeatother
chapter{}

section{Introduction}
chapter{}
section{GraphQL}

appendix

chapter{In appendix}

end{document} 

I have extended your MWE to include an Appendix just to check that that would be OK, even though you are probably not having any appendices.

enter image description here

Correct answer by Peter Wilson on January 9, 2021

Perhaps this would work:

documentclass[12pt,a4paper]{book}

usepackage{titletoc}
titlecontents*{chapter}[0pt]{}
    {bfseriesphantomsectionhyperlink{chapter.thecontentslabel}{Lecture~thecontentslabel}}{}
    {hfillbfseriescontentspage}

usepackage{hyperref}

begin{document}
tableofcontents
makeatletter
renewcommand{@chapapp}{Lecture}
makeatother
chapter{}
section{Introduction}
chapter{}
section{GraphQL}
end{document} 

Answered by Jinwen on January 9, 2021

You can patch @chapter so it issues chapternumberline instead of numberline: then we can define chapternumberline to do what we prefer.

documentclass[12pt,a4paper]{book}
usepackage{etoolbox}
%usepackage{hyperref}

renewcommand{chaptername}{Lecture}
makeatletter
@ifpackageloaded{hyperref}
  {patchcmd{Hy@org@chapter}}
  {patchcmd{@chapter}}
  {numberline}{chapternumberline}{}{}
newcommand{chapternumberline}[1]{chaptername #1}
makeatother

begin{document}

tableofcontents

chapter{}
section{Introduction}
chapter{}
section{GraphQL}

end{document}

I made the patch working for the cases when hyperref is loaded or not.

Note that the correct command to redefine is chaptername rather than @chapapp.

enter image description here

Here's a picture when hyperref is used, showing that the entire entry is a link.

enter image description here

Answered by egreg on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP