2

I added "chapter" word to my TOC, but when i use package hyperref,this word remove from TOC. this is my code:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{hyperref} % if commented all fine

\begin{document}

\makeatletter
\patchcmd{\@chapter}{\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}{\addcontentsline{toc}{chapter}{\protect{\numberline{\chaptername :  #1}}}}{}{}
\makeatother

\tableofcontents
\chapter{my name}
my name is ...
\section{structure}
\chapter{your name}
 your name is .....
\end{document}
  • Hi and welcome, hyperref usually is the last package loaded. – Johannes_B May 7 '15 at 17:49
  • thanks, but don't repair – mojtaba mansouri May 7 '15 at 17:50
  • To be honest, i cannot figure out how you managed to write the word chapter to the contents. Can you strip down your example to make it real minimal? For example, most of the packages aren't needed. – Johannes_B May 7 '15 at 18:08
  • I delete additional package. But this is minimal example. – mojtaba mansouri May 7 '15 at 18:14
  • do you have any code that do this work for me and don't have any problem whit 'hyperref'? – mojtaba mansouri May 7 '15 at 18:16
2

The solution is to patch \@chapter command befor loading hyperref but you will need somme extra space in \l@chapter you can do like this

\documentclass{book}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@chapter}{\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}{\addcontentsline{toc}{chapter}{\protect\numberline{\chaptername}#1}}{}{}
\patchcmd{\l@chapter}{\setlength\@tempdima{1.5em}}{\setlength\@tempdima{4.5em}}{}{}
\makeatother
\usepackage{hyperref}

\begin{document}
\makeatletter
\let\stdl@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{\stdl@chapter{{#1}}{}}
\makeatother

\tableofcontents
\chapter{my name}
my name is ...
\section{structure}
\chapter{your name}
 your name is .....
\end{document}
  • See, wasn't that hard. I just tried to patch around the hyperref internals. :-/ I should have seen that. – Johannes_B May 7 '15 at 19:03
  • You could add that hyperref redefines chapter, and many other commands, to enable hyperlinking. It saves the original definition first. – Johannes_B May 7 '15 at 19:04
  • @Johannes_B you are right, if you want to edit my answer, thank you. – touhami May 7 '15 at 19:06
  • very good , I think true that you can.thanks a lot @Johannes_B – mojtaba mansouri May 7 '15 at 19:11
  • This is indeed a way to go. Some remarks: (a) The chapter number is gone, already a problem of the question. (b) \@chapapp is better than \chaptername, because it is automatically redefined for appendix chapters. (c) The 4th argument of \patchcmd should never be empty. A simple error message \errmessage{Patching of \noexpand\@chapter failed} will inform the user, if the \patchcmd was not successful. (d) The original \@chapter command contains three versions of \addcontentsline{toc}{chapter} for different cases, not just one. – Heiko Oberdiek May 7 '15 at 19:12

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.