[Tex/LaTex] How to prevent “Contents” from appearing twice in the header when I use \leftmark and \rightmark with fancyhdr

fancyhdrheader-footertable of contents

I would like to use \leftmark and \rightmark, but have "Contents" appear only once (on the left) in the header. Instead, it appears as both the \leftmark and the \rightmark.

The following MWE illustrates the problem:

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{\leftmark}
\rhead{\rightmark}

\begin{document}
\tableofcontents
\end{document}

Best Answer

Change the definition of \tableofcontents.

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{\leftmark}
\rhead{\rightmark}

\makeatletter
\renewcommand\tableofcontents{%
    \section*{\contentsname
        \@mkboth{%
%           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}% DELETED
           \MakeUppercase\contentsname}{}}% ADDED
    \@starttoc{toc}%
    } 
\makeatother

\begin{document}
\tableofcontents
\end{document}