1

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.

1

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

1
  • Chose this as the answer, because this not only answers my question but also has given how to separate by chapters by small spacing in the toc.
    – emil
    Nov 14 '20 at 3:36
2

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

1

Perhaps this would work:

\documentclass[12pt,a4paper]{book}

\usepackage{titletoc}
\titlecontents*{chapter}[0pt]{}
    {\bfseries\phantomsection\hyperlink{chapter.\thecontentslabel}{Lecture~\thecontentslabel}}{}
    {\hfill\bfseries\contentspage}

\usepackage{hyperref}

\begin{document}
\tableofcontents
\makeatletter
\renewcommand{\@chapapp}{Lecture}
\makeatother
\chapter{}
\section{Introduction}
\chapter{}
\section{GraphQL}
\end{document} 
2
  • This works. kudos! but has a slight edge. hyperef in heading is gone now. Is there any fix to that?
    – emil
    Nov 1 '20 at 6:31
  • 1
    @emil Thanks for pointing this out! I've edited my answer to support hyperlinks, check it ~
    – Jinwen
    Nov 1 '20 at 7:03

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.