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.


% copied from mwe

% copied from book.cls and modified
  \ifnum \c@secnumdepth >\m@ne
      \addtocontents{toc}{\string\contentsline {chapter}%
        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
      \addtocontents{toc}{\string\contentsline {chapter}%
        {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%
    \addtocontents{toc}{\string\contentsline {chapter}%
      {\hbox to .65in{Chapter}\protect\numberline{\thechapter}#1}{\thepage}}%



\chapter{C++ First Blood}

This is a test.

Review Questions
\item What color is George Washington's white horse?
\item What function is the entry point to every C++ program?

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

  • 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

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:

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