2

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}
  • Appears this error message: ! Argument of \contentsline has an extra }. <inserted text> \par l.2 \contentsline {chapter}{\hbox to .65in{Chapter}\numberline {2}ch2}{3} ? ! Emergency stop. <inserted text> \par l.2 \contentsline {chapter}{\hbox to .65in{Chapter}\numberline {2}ch2}{3} ! ==> Fatal error occurred, no output PDF file produced! Transcript written on olatex_1151915_86EFfXpTk3Ex.log. – juanuni May 22 '15 at 4:55
  • Considering what you now ask (about pdflatex) see my addition (third paragraph from the end of my answer). – nickie May 22 '15 at 14:21
6

The problem is in the way that you redefine \@chapter, which is incompatible with hyperref. You can see this immediately if you comment out the line loading hyperref, 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. After hyperref 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 by hyperref, thus latex complains that it's missing when reading the toc file.

An immediate solution that works is to replace the following line (occurring three times) in your definition of \@chapter:

      \addtocontents{toc}{\string\contentsline {chapter}%
        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%

with an equivalent one that does not explicitly use \contentsline:

      \addcontentsline{toc}{chapter}% 
        {\hbox to .68in{Chapter}\protect\numberline{\thechapter}#1}

(I changed the width here to avoid an overfull \hbox.) This will give hyperref 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 the latexmk utility: by running latexmk -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 use titletoc in order to introduce the word "Chapter" in the table of contents.

  • Actually, I do not want the word chapter. I just want chapter number. title. What do I do? Forgive me for my ignorance, Latex makes C++ look like child's play. All the weird little rules, side effects, it's mind boggling to a novice! – Dov May 22 '15 at 11:06
  • If you don't want the word "Chapter", then just remove all customization. Remove everything between \makeatletter and \makeatother (including those two). – nickie May 22 '15 at 11:13
  • I will just have to ask a separate question about texworks, but the command is right on the windows command line.... Thanks! – Dov May 24 '15 at 17:11

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.