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 cannot find in any latex forums a solution for my problem with chapter-specific headers.

I am compiling a one-sided report with various chapters. For all the chapters, I have the header showing the chapter's number and title. But for one of them I would like to have the header showing the section's number and title instead (this is due to the nature of that particular chapter, which is longer than the others).

Everything works fine and I manage to show the section in the header. However, all the following chapters will not go back to the initial settings that shows the chapter in the header.

This is a minimal example with 3 chapters and you can run it to see the results. Chapter 1 and 3 should show the chapter in the header and only Chapter 2 is supposed to have the sections in its header. But Chapter 3 keeps applying the settings of the preceding although I've changed them.

I try resetting to \pagestyle{plain} or \pagestyle{empty} before adding the \renewcommand{\chaptermark} but it doesn't work.

\documentclass[11pt,a4paper,oneside]{report}
\usepackage{fancyhdr}
\begin{document}

\pagestyle{fancy}
\fancyhead{} % clear all header fields
\fancyhead[LO,LE]{\leftmark}
\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter.\ #1}{}}

\chapter{My First Chapter}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage
\section{Section 3}
\clearpage      

\renewcommand{\sectionmark}[1]{%
\markboth{\thesection.\ #1}{}}    
\chapter{My Second Chapter}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage

\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter.\ #1}{}}    
\chapter{My Third Chapter}    
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage    

\end{document}
share|improve this question
    
You might want to increase \headheight -- see tex.stackexchange.com/questions/37585/… –  lockstep Dec 8 '11 at 14:47
    
ill check that, thank you. –  Marie Dec 8 '11 at 16:50

2 Answers 2

Define two page styles:

\documentclass[11pt,a4paper,oneside]{report}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancypagestyle{normal}{%
  \fancyhead{} % clear all header fields
  \fancyhead[L]{\leftmark}}
\fancypagestyle{special}{%
  \fancyhead{} % clear all header fields
  \fancyhead[L]{\rightmark}}

\renewcommand{\chaptermark}[1]{%
  \markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{%
  \markright{\thesection.\ #1}{}}

\setlength{\headheight}{13.6pt}

\begin{document}

\chapter{My First Chapter}
\pagestyle{normal}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage
\section{Section 3}
\clearpage      

\chapter{My Second Chapter}
\pagestyle{special}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage

\chapter{My Third Chapter}    
\pagestyle{normal}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage    

\end{document}

Don't forget to modify \headheight as suggested during compilation:

Package Fancyhdr Warning: \headheight is too small (12.0pt): 
 Make it at least 13.59999pt.
 We now make it that large for the rest of the document.
 This may cause the page layout to be inconsistent, however.
share|improve this answer
    
Thank you @egreg! I understand now how the \fancypagestyle command works, there is only one example in the package documentation and I didn't manage to adapt it to my case. Now I also understand more the use of \leftmark and \righmark. I'll also check the \headheight as suggested during the compilation of my big file. Thanks again. –  Marie Dec 8 '11 at 16:38

You have to re-redefine \sectionmark not \chaptermark:

\documentclass[11pt,a4paper,oneside]{report}
\usepackage[english]{babel}
\usepackage{blindtext}% for demo only
\usepackage{fancyhdr}
\begin{document}

\pagestyle{fancy}
\fancyhead{} % clear all header fields
\fancyhead[LO,LE]{\leftmark}
\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter.\ #1}{}}

\blinddocument

\renewcommand{\sectionmark}[1]{%
\markboth{\thesection.\ #1}{}}
\blinddocument

\renewcommand{\sectionmark}[1]{}
\blinddocument

\end{document}

That's because the sections will be placed by \sectionmark at the headline. So this will be done as long as \sectionmark uses \markboth or \markright. And the \markboth at \sectionmark will overwrite the \markboth at \chaptermark. But after re-redefinition of \sectionmark it does not longer use any of these commands. Because of this, the already existing \markboth at \chaptermark won't be overwritten by \sectionmark any longer.

Advantage of this solution would be, that you still have a head, if you have several pages before the first section, e.g.,

\documentclass[11pt,a4paper,oneside]{report}
\usepackage[english]{babel}
\usepackage{blindtext}% for demo only
\usepackage{fancyhdr}
\begin{document}

\pagestyle{fancy}
\fancyhead{} % clear all header fields
\fancyhead[LO,LE]{\leftmark}
\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter.\ #1}{}}

\blinddocument

\renewcommand{\sectionmark}[1]{%
\markboth{\thesection.\ #1}{}}
\chapter{Test}
\blindtext[10]
\section{First Section at Test}
\blindtext
\section{Second  Section at Test}
\blindtext[5]
\section{Third Section at Test}
\blindtext[5]

\renewcommand{\sectionmark}[1]{}
\blinddocument

\end{document}

Here at page 6 you may find the chapter at the head, and with the first section at page 7 you will get the section.

share|improve this answer
    
Thanks as well for this other solution! Also works well on the small example. I'll check and run with my big file. –  Marie Dec 8 '11 at 17:00
    
@Marie: You don't need to say "thanks", just use upvote if the answer may be usefull too. –  Schweinebacke Dec 9 '11 at 7:28

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.