when using hyperref package undefined control sequence \chapter error

I am using a custom class provided by my university for writing my thesis. Recently, I included the hyperref package and immediately I got a host of erros related to the \addtocontents command. I used the solution provided at this link. That removed all the errors but now when I compile, latex gives me the error "undefined control sequence \chapter. If I remove the hyperref package, it works fine.

I have wasted my whole day trying to make it work. I am pasting the definition of \chapter in the .cls file below. The changes I have made are marked with the comment "%changes added by Waqas"

\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
{\protect {CHAPTER }\numberline{\thechapter}\MakeUppercase{#1}}%
\else
\fi
\if@twocolumn
\else
\fi}
\vspace*{50\p@}%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\centering \Large \bfseries \MakeUppercase{\@chapapp}\space \thechapter
\par\nobreak
\vskip 20\p@
\fi
\interlinepenalty\@M
\Huge \bfseries \MakeUppercase{#1}\par\nobreak
\vskip 40\p@
}}
\def\@schapter#1{\if@twocolumn
\else
\fi}
\vspace*{50\p@}%
{\parindent \z@ \raggedright
\normalfont
\interlinepenalty\@M
\Huge \bfseries  #1\par\nobreak
\vskip 40\p@
}}


Any help would be highly appreciated.

The following issues deal with hyperref:

• In \protect{CHAPTER } only the open curly brace { is protected, because \protect only acts on the next token (not a group of tokens). Anyway, the ugly prefix CHAPTER wastes valuable space in the bookmarks, thus I had changed it to \texorpdfstring{CHAPTER }{}.

• \MakeUppercase does not work in bookmarks, replaced by \texorpdfstring\MakeUppercase{} to disable it in bookmarks.

• I do not see an error about \chapter, if I extend the code snippet to full document, based on class book.

Complete example:

\documentclass{book}

\makeatletter
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
\def\@chapter[#1]#2{%
\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\texorpdfstring {CHAPTER }{}%
\numberline{\thechapter}%
\texorpdfstring\MakeUppercase{}{#1}}%
\else
\fi
\if@twocolumn
\else
\fi
}
\vspace*{50\p@}%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\centering \Large \bfseries \MakeUppercase{\@chapapp}\space \thechapter
\par\nobreak
\vskip 20\p@
\fi
\interlinepenalty\@M
\Huge \bfseries \MakeUppercase{#1}\par\nobreak
\vskip 40\p@
}}
\def\@schapter#1{\if@twocolumn
\else
\fi}
\vspace*{50\p@}%
{\parindent \z@ \raggedright
\normalfont
\interlinepenalty\@M
\Huge \bfseries  #1\par\nobreak
\vskip 40\p@
}}
\makeatother

\usepackage{hyperref}
\usepackage{bookmark}
\bookmarksetup{numbered}

\begin{document}
\tableofcontents
\chapter{Foobar}
\end{document}

• That solved my problem. I edited the newcommand definition of chapter in my custom class file with these modifications and now its working with the hyperref package. Thanks man! you are a life saver! Aug 18, 2015 at 17:55