TransWikia.com

How to display current section title in the header when a new chapter begins just on an odd page

TeX - LaTeX Asked by azhi on August 26, 2020

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?

One Answer

enter image description here
enter image description here enter image description here

Though not very familiar with the titleps package but after some experimentation and Google search on this website at https://tex.stackexchange.com/questions/208370/title-marking-with-titleps-breaks-when-tableofcontents-is-used and thanks to @cfr

documentclass[twoside,openany]{book}
usepackage{lipsum}
usepackage[pagestyles]{titlesec}
renewpagestyle{plain}{
  sethead[firsttitlemarksbfseriesifthesection{thesection}{ifthechapter{chaptertitle}{chaptertitle}}][][]% even-left | even-center | even-right
  {}{}{bottitlemarksbfseriesifthesection{thesection}{ifthechapter{thechapter}{chaptertitle}}}% odd-left | odd-center | odd-right
  setfoot[thepage][][]% even-left | even-center | even-right
  {}{}{thepage}% odd-left | odd-center | odd-right
  setheadrule{0.4pt}
}
pagestyle{plain}

begin{document}

  tableofcontents

  chapter{Header Test}
  section{Sec1}
  lipsum[1]
  section{Sec2}
  lipsum[3-4]
  section{Sec3}
  lipsum[5]
  section{Sec4}
  lipsum[7]
  chapter{New Chapter}
  section{Sec5}
  lipsum[2]
  section{Sec6}
  lipsum[6]
  section{Sec7}
  lipsum[8-9]
  section{Sec8}
  lipsum[10]
  chapter{GAH}
  lipsum[1-4]
  chapter{New Chapter AAAA}
  section{Sec5}
  lipsum[2]
  section{Sec6}
  lipsum[6]
  section{Sec7}
  lipsum[8-9]
  section{Sec8}
  lipsum[10]
  chapter{GAH1}
  section{Sec8}
  lipsum[10]
  lipsum[1-4]
  lipsum[1-4]
  lipsum[1-4]
  lipsum[1-4]
  chapter{New CHAPTER AAAAA}
  section{Sec5}
  lipsum[2]
  section{Sec6}
  lipsum[6]
  section{Sec7}
  lipsum[8-9]
  section{Sec8}
  lipsum[10]
end{document}

Answered by js bibra on August 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP