Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
If you have at least one \section command in the same page of your ToC, the problem disappears. –  Gonzalo Medina Mar 18 '12 at 0:04
    
@Gonzalo Medina Yes. Unfortunately the ToC takes up a full page. –  Will Mar 18 '12 at 0:15
add comment

3 Answers 3

up vote 7 down vote accepted

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}
share|improve this answer
    
Perfect, thank you. How would I make it appear only on the right? –  Will Mar 18 '12 at 0:14
    
@Will By removing the first \MakeUppercase\contentsname from the definition instead of the second one. –  lockstep Mar 18 '12 at 0:21
1  
@Will Note, however, that \tableofcontents is considered a section in the article class, and therefore should change \leftmark. Wether it should change \rightmark too is more debatable. –  lockstep Mar 18 '12 at 0:25
add comment

The user guide to the fancyhdr package notes on p. 10 that

It should be noted that the LATEX marking mechanism works fine with chapters (which always start on a new page) and sections which are reasonably long. It does not work quite as well with short sections and subsections. This is a problem with LATEX, not with fancyhdr.

AFAICT, an easy workaround, which doesn't require the redefinition of LaTeX's \tableofcontents command, consists of specifying \rhead{} before the \tableofcontents command, followed by \rhead{\rightmark} before the first section:

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\leftmark}
\begin{document}
\rhead{}   % set \rhead to empty
\tableofcontents

\clearpage   % OP has indicated that ToC spans a full page
\rhead{\rightmark} % reset \rhead to \rightmark

\section{Hello}
\end{document}
share|improve this answer
    
I prefer customization in the preamble, but +1 for the fancyhdr quote. –  lockstep Mar 18 '12 at 0:27
add comment

\rightmark uses the first right mark of the page. So you can also set before \tableofcontents an empty \markright to get rid of the right mark on the first page and use an addtocontents to insert a \markright in the toc to get rid of the right mark on the following pages:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{\leftmark}
\rhead{\rightmark}
\begin{document}
\markright{}
\tableofcontents
\addtocontents{toc}{\protect\markright{}}
%some text in the toc for test:
\addtocontents{toc}{blub}
\addtocontents{toc}{\protect\newpage}
\addtocontents{toc}{blub}

\end{document}

Btw: You are not saying which informations \leftmark and \rightmark will contain. You should be aware that some combinations (e.g. section/subsection) can led to confusing results. As a thumb of rule the \leftmark should be connected to something that starts a new page, like a \part or a \chapter.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.