[Tex/LaTex] Conflict between fancyhdr and table of contents

fancyhdrformattingtable of contents

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.

Best Answer

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}