My document is failing to generate a table of contents.
This is the latest MWE suggested by Nickie (perhaps messed up in translation, I don't know). When I compile this in TeXworks using pdflatex, it does not generate a table of contents. I put in some sample text to test that features I was using still work (like links), and they do. But the table of contents still does not display.
\documentclass[12pt]{book}
\usepackage[usenames,dvipsnames]{color}
\usepackage{titlesec}
\definecolor{DeepPink}{rgb}{0.8,0,0.4}
\definecolor{DarkRed}{rgb}{0.5,0,0}
\definecolor{DarkBlue}{rgb}{0,0,0.5}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries\color{DarkBlue}}{\thechapter.}{1em}{}
\makeatletter
% copied from mwe
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
% copied from book.cls and modified
\def\@chapter[#1]#2{%
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\addtocontents{toc}{\string\contentsline {chapter}%
{\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
\else
\addtocontents{toc}{\string\contentsline {chapter}%
{\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
\fi
\else
\addtocontents{toc}{\string\contentsline {chapter}%
{\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
\fi
\chaptermark{#1}%
\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi}
\makeatother
\usepackage[colorlinks,citecolor=DeepPink,linkcolor=DarkRed,urlcolor=DarkBlue]{hyperref}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{C++ First Blood}
This is a test.
Review Questions
\begin{enumerate}
\item What color is George Washington's white horse?
\item What function is the entry point to every C++ program?
\end{enumerate}
\href{http://en.wikipedia.org/wiki/Graph_theory}{Graph Theory}
\chapter{Java: More Delicious than C++}
\chapter{Python Swallows Those other Languages, Whole}
\chapter{D for Those who Cannot Spell C++}
\end{document}
Best Answer
The problem is in the way that you redefine
\@chapter
, which is incompatible withhyperref
. You can see this immediately if you comment out the line loadinghyperref
, just before\begin{document}
.The
hyperref
package plays a lot with everything containing references; lines in the table of contents are just one such thing. It achieves this by redefining commands such as\addcontentsline
, effectively adding hyperlinks to all references. Afterhyperref
is loaded, the command\contentsline
(which normally takes 3 arguments) requires also a fourth argument: the anchor where the line links to. In your case, your definition of\@chapter
uses\contentsline
directly and only provides three arguments. The fourth one will never be introduced byhyperref
, thus latex complains that it's missing when reading thetoc
file.An immediate solution that works is to replace the following line (occurring three times) in your definition of
\@chapter
:with an equivalent one that does not explicitly use
\contentsline
:(I changed the width here to avoid an overfull
\hbox
.) This will givehyperref
the chance to properly introduce the fourth argument to\contentsline
. Now,hyperref
(in fact the PDF driver) will warn you that you shouldn't be using\hbox
in content lines, but you can live with it.Of course, to see the table of contents, you need to invoke
pdflatex
multiple times. I recommend thelatexmk
utility: by runninglatexmk -pdf yourfile.tex
your PDF will be made automatically.I understand that the only change you want, over what
book.cls
normally does, is to introduce the word "Chapter" before chapter titles in the table of contents. If this is so, a probably better solution would be to leave the definitions of\chapter
and\@chapter
untouched and to redefine\l@chapter
(which is used by\contentsline
) to suit your needs.An even better solution, as you already use the
titlesec
package, is to also usetitletoc
in order to introduce the word "Chapter" in the table of contents.