4

I am using package fancyhdr to help me construct the headers of my book. The Editor want to me to add the chapter title to the header if the new chapter begins on a page whose number is even, and to add the section title to the header if the new chapter begins on a page whose number is odd. Then I modified the page-style "plain" as follows (just a test file):

 \documentclass[openany]{book}
 \usepackage{fancyhdr}
 \renewcommand{\headwidth}{\textwidth} 

 \pagestyle{fancy}                        
 \renewcommand{\headrulewidth}{0.5pt}
 \fancyhf{}                                                 
 \fancyhead[LE,RO]{\thepage}                       
 \fancyhead[RE]{\leftmark}                                
 \fancyhead[LO]{\rightmark} 

 \fancypagestyle{plain}{
\fancyhf{}                                
\renewcommand{\headrulewidth}{0.5pt}                               
\fancyhead[LE,RO]{\thepage}                          
\fancyhead[RE]{\leftmark}                                
\fancyhead[LO]{\rightmark}  
}

 \begin{document}
 \chapter{Test-1}     
 \section{test-section-1}
 \newpage
 \section{test-section-2}
 \newpage
 \section{test-section-3}
 \newpage
 \chapter{Test-2}
 \section{test-section-4}
 \end{document}

But I can only realize partially the specifications. If the new chapter begins on an even page, all the codes are OK, but if the new chapter begins on odd page, then it could not display the section title, but only the page number. Here is the screen-shoots:

  1. New chapter page with ODD page number, need section title odd page-without section title-new chapter

  2. even page, all things are OK even page-with chapter title

  3. odd page, all things are OK odd page-with section title

  4. new chapter page with Even page number, all things are OK even page-with chapter title-new chapter

Then I tried many methods. But at last I figured out a method, which is very complex, but it did work. I just only defined a new command, which is as follows:

  \newcommand{\headodd}[2]{\fancypagestyle{myplain}{%  
    \fancyhf{} %   
    \pagestyle{fancy}                               
    \renewcommand{\headrulewidth}{0.5pt}                                     
    \fancyhead[LE,RO]{\thepage}                         
    \fancyhead[RE]{\leftmark}  
    %\hypersetup{   
      %  colorlinks=false,
     %   pdfborder={0 0 0},
    %}                             
    \fancyhead[LO]{ \ref{#1} #2}  
    }
   \thispagestyle{myplain}
}

Then I modified the main body into :

     \chapter{Test-1}
     \headodd{s-1}{test-section-1}
     \section{test-section-1}\label{s-1}

Thus I get the following result: What I want to get

My question is: (1) Is there any other method to satisfy all the editor's specifications? (2) How to simplify my codes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.