[Tex/LaTex] when using hyperref package undefined control sequence \chapter error

chaptershyperref

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.}%
                         \addcontentsline{toc}{chapter}%
                        {\protect {CHAPTER }\numberline{\thechapter}\MakeUppercase{#1}}%
                    \else
                      \addcontentsline{toc}{chapter}{\MakeUppercase{#1}}%
                    \fi
                    \chaptermark{#1}%changes added by waqas (added {}{}
                    \addtocontents{lof}{\protect\addvspace{10\p@}{}{}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}{}{}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\def\@makechapterhead#1{%
  \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
                   \@topnewpage[\@makeschapterhead{#1}]%
                 \else
                   \@makeschapterhead{#1}%
                   \@afterheading
                 \fi}
\def\@makeschapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Huge \bfseries  #1\par\nobreak
    \vskip 40\p@
  }}

Any help would be highly appreciated.

Best Answer

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.}%
    \addcontentsline{toc}{chapter}{%
      \texorpdfstring {CHAPTER }{}%
      \numberline{\thechapter}%
      \texorpdfstring\MakeUppercase{}{#1}}%
  \else
    \addcontentsline{toc}{chapter}{\MakeUppercase{#1}}%
  \fi
  \chaptermark{#1}%changes added by waqas (added {}{}
  \addtocontents{lof}{\protect\addvspace{10\p@}{}{}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}{}{}}%
  \if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]%
  \else
    \@makechapterhead{#2}%
    \@afterheading
  \fi
}
\def\@makechapterhead#1{%
  \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
                   \@topnewpage[\@makeschapterhead{#1}]%
                 \else
                   \@makeschapterhead{#1}%
                   \@afterheading
                 \fi}
\def\@makeschapterhead#1{%
  \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}
Related Question