2

I'm having some trouble getting fancyhdr and the table of contents to play nice. I have below a MWE which attempts to create the document as I want it. Essentially, I have a title page, some preamble page, and a table of contents, followed by the actual document content. I have defined a footer on my document which should appear with the pages.

The problem appears to come from the renewcommand used to specify the table of contents title. If I don't include it in the document, the document compiles fine and looks and acts as I'd expect. When I attempt to use that command to change the ToC title, suddenly it cannot compile and throws an error on the line with \tableofcontents. What's going on here and how can I achieve a footer and a ToC title change?

\documentclass{article}

\usepackage[letterpaper,margin=1in]{geometry} % Handles geometry of page layout
\usepackage{fancyhdr}
\usepackage{lipsum}

% Define footer
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[L]{Some long footer at the bottom of the page. \\ \centering \normalfont \thepage}
\renewcommand{\footrulewidth}{0pt}
\renewcommand{\headrulewidth}{0pt}

% This is the problem line!
\renewcommand{\contentsname}{\centering Table of Contents\\\small Section \hfill Page}

\begin{document}

    \pagenumbering{roman}

    \begin{titlepage}
        Title of Document
    \end{titlepage}

    \newpage
    \lipsum

    \newpage
    \tableofcontents % Compilation shows error here!

    \newpage
    \pagenumbering{arabic}
    \section{One}
    \lipsum

\end{document}

I do believe this question was asking about the same problem, but it was not clear and no answer was provided as far as I can see.


P.S. I do know about the \thispagestyle issue with ToC, that is not the issue or concern with this problem.

  • There is no conflict. It is just the case that you can't typeset the table of contents header with the command \contentsname. It is only meant for the language specific translation or synonym for "Table of Contents". Replace the current contents by foo bar, and it will compile fine showing the heading foo bar. – gernot Nov 30 '16 at 17:46
  • @gernot I find that to be a very odd restriction. What's more, using other titles and styles causes all sorts of weird problems. Is there a way to address the problem at hand to allow whatever title I want while still retaining the footer? – zephyr Nov 30 '16 at 17:53
  • There is no restriction. It is just a misunderstanding what \contentsname is about. It is a shorthand for "Table of Contents" or any other equivalent string that one might want to use in its place, and \contentsname is used in all places where this string is needed. The meaning of \contentsname is not "typeset the table of contents header". – gernot Nov 30 '16 at 18:12
  • The problem is caused by you having all kinds of fragile commands in \contentsname. As others have said, that'snot what it is meant for – Piet van Oostrum Nov 30 '16 at 18:12
  • Okay, I see the issue. I was following other suggestions which said that was the way to change the table of contents title. The provided answer appears to solve this issue. – zephyr Nov 30 '16 at 18:15
2

Use \contentsname to define how the table of contents should be labeled (this string is used in different places, e.g. in headers and footers, and therefore may not contain other stuff which doesn't make sense in these other places).

To center the headline of the table of contents, a minimal invasive method is to add the center command in the right place, as follows. The additional line (Section ... Page) can be inserted using \addtocontents. In the preamble, load the package xpatch.

\usepackage{xpatch}

The \tableofcontents command has to be replaced by the following lines:

\bgroup
\makeatletter
\xpatchcmd\@ssect{#5}{\centering #5}{}{}
\tableofcontents
\makeatother
\egroup
\addtocontents{toc}{{\small\bfseries Section\hfill Page\bigskip\par}}

If for consistency reasons you want to center all headlines introduced by \section* (including all "List of" sections), omit \bgroup and \egroup.

The following table of contents is generated from the code below.

enter image description here enter image description here

\documentclass{article}

\usepackage[letterpaper,margin=1in]{geometry} % Handles geometry of page layout
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot[L]{Some long footer at the bottom of the page. \\ \centering \normalfont \thepage}
\renewcommand{\footrulewidth}{0pt}
\renewcommand{\headrulewidth}{0pt}

\usepackage{xpatch}
\renewcommand{\contentsname}{Table of Contents}

\begin{document}

\pagenumbering{roman}
\begin{titlepage}
  Title of Document
\end{titlepage}

\newpage
\lipsum

\newpage
\bgroup
\makeatletter
\xpatchcmd\@ssect{#5}{\centering #5}{}{}
\tableofcontents
\makeatother
\egroup
\addtocontents{toc}{{\small\bfseries Section\hfill Page\par}}

\newpage
\pagenumbering{arabic}
\section{One}
\lipsum
\subsection{One One}
\lipsum
\subsubsection{One One One}
\lipsum
\subsubsection{One One Two}
\lipsum

\end{document}
  • Thanks for this! One small question, using this causes the tabbing to be different. Previously each subsections (and subsubsections) were tabbed slightly in the ToC, but now they're all at the same tab level. Anyway to restore that functionality? – zephyr Nov 30 '16 at 19:32
  • @zephyr I have modified the solution. Now the indentation of the table of contents is not influenced. With packages like titlesec it is easier to adapt headings (as well as headers and footers), but is requires some reading. – gernot Nov 30 '16 at 22:31

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.