problem with hyperref and TOC

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
\makeatother

\tableofcontents
\chapter{my name}
my name is ...
\section{structure}
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

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{\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}

• 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
• 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