\firstmark on \tableofcontents produces unexpected result

So I am working on a rather small latex document and can't explaint the behavior of \firstmark on the table of content page.

Tell me if I'm wrong, but in my understanding \firstmark holds the first section heading on the current page. On the \tableofcontents page this should be CONTENTS I think.

In the table of content header this becomes CONTENTSCONTENTS and on the page itself it becomes just nothing. The CONTENTS on the left of the heading is irrelevant. I could remove this easily, but I just let it there to get the minimal source.

I got this small .tex source that reproduces this behavior:

\documentclass{article}

\usepackage{fancyhdr}

\pagestyle{fancy}

\begin{document}
\tableofcontents

foo

\firstmark

bar
\end{document}


And this is the pdf I get from this:

As you can see in the heading on the upper right, there is CONTENTSCONTENTS instead of just a single CONTENTS and where you should see CONTENTS between foo and bar, there is absolutely nothing.

I also copied this source to LaTeX Base, to exclude the possibility that this is an error on my computer.

Could someone explain this behavior to me?

Don't use the low-level \firstmark with latex. In latex the mark is split in two parts and you access the left and right part with \leftmark and \rightmark. Marks should be used only in the header and footer and not in the text body as they are reliable only when a page is shipped out.

\documentclass{article}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{Leftmark: \leftmark, rightmark: \rightmark, firstmark: \firstmark}

\begin{document}
\markboth{AAAA}{BBBB}

some text
\end{document}


• So I want to have the first section heading in the right header of the current page. Now do I have to mark every section with \markright{theSectionHeading} and set \fancyhead[R]{\rightmark}or how do i do this? – Melvin S. Aug 8 at 16:04
• \section issues by default \markright in article cls (and if you want to change this, redefine \sectionmark) – Ulrike Fischer Aug 8 at 16:06