1

By using fancyhdr, I want to get the section name and number in the header of pages and if relievant also the chapter name.

To be CLEARER, The question goes to : How to put on the header of the opening pages of chapters, the name and number of the first section of this chapter, in case several new sections exist in the first page of this chapter ?

MY FIRST ATTEMPT: (code below)

If there is no chapter in the part, there is no problem. (example PartA) Otherwise, there is no header on chapter page. To solve this, I redefined the chapter command to keep the page style on chapter pages (Modification 1). Then I got the Chapter name but not the section one. which is not yet defined at this stage.

So I redefined the \chaptermark command (modification 2) inspired by Header not displaying correctly on chapter page due to subsection.

It is quite good but I still had two issues:

  • If there was no section in the chapter page, the section name from the last chapter was used (See picture 1a). I found a solution by resetting the firstsectiontitle when creating a new chapter. It works fine but it is perhaps not a proper way, using \def or \gdef.

At that stage I had

 % Modification 2
    \renewcommand{\chaptermark}[1]{%
          \markboth{\MakeUppercase{\chaptername\ \thechapter. \ #1}} % Chapter name appears in the header as previously. 
              {\noexpand\firstsectiontitle}
      \gdef\firstsectiontitle{}  % To remove the firstsectiontitle from last Chapter in case there is no section on the current chapter first page
    }
    \renewcommand{\sectionmark}[1]{%
      \markright{   \thesection. \ \MakeUppercase{#1}}
      \gdef\firstsectiontitle{\thesection. \ \MakeUppercase{#1}}}
    \newcommand\firstsectiontitle{}
  • The second issue I still can't handle with : If there are two or more sections in the same chapter page, the name of the last section in the page is diplayed in the header. I would prefer the rightmark("sectionmark") to be the name of the chapter's first section. See picture 1b (8 instead of 10). Using the same trick as in the same previously mention link, with the boolean firstsectionmark, I could get the good first section name but not the good section number due to \section being unappropriate.(see picture 1c).

I well understand that {\thesection.\ #1} is not a good definition in the \sectionmark redefinition. Would it be possible to define a firstsectiontitlenumber to be set in the sectionmark definition in \gdef\firstsectiontitle{ \MakeUppercase{\thesection.\ #1}} Any suggestion?

Picture1, click to open Picture1

SECOND ATTEMPT STRATEGY (code below) inspired by Header with Chapter and section on page of chapter begin

A mark command sets three variables: \topmark (last mark of the previous page), firstmark (first mark on the current page) and \botmark (last mark on the current page). \rightmark uses the \firstmark set by \markright or the second argument of \markboth. On a chapter page the first call of \markboth is done by \chapter, normally with an empty second argument to reset the header entry.

In our case, firstmark is empty. The workaround is to use botmark instead. It gives us almost what expected but still the number and name of the last section of chapter page. See picture 2. Picture2


Could anyone help me with this, please?
There should be an easy way, since if behaves as I want if there is no chapter in the part as in Part A.


For the first attempt, here is the code with the two modifications. It is almost a minimal example

\documentclass[twoside, openright]{report}
\usepackage{remreset}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{lmodern}
%
\pagestyle{fancy}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}
\fancyhf{} 
\fancyhead[LE,RO]{\rightmark}
\fancyhead[RE,LO]{\leftmark}
\fancyfoot[LE,RO]{\thepage}
%
% Modification 1: redefinition of chapter from report.cls
\makeatletter
\renewcommand{\chapter}{\clearpage   
                    %\thispagestyle{plain} % to keep the fancy style
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}
\makeatother  
%
% Modification 2
\renewcommand{\chaptermark}[1]{%
      \markboth{\MakeUppercase{\chaptername\ \thechapter. \ #1}} % Chapter name appears in the header as previously. 
          {\noexpand\firstsectiontitle}
    \gdef\firstsectiontitle{}  % To remove the firstsectiontitle from last Chapter in case there is no section on the current chapter first page
    \global\firstsectionmarktrue  % To get the title of the first subsection of the current chapter.
}
%
\renewcommand{\sectionmark}[1]{%
      \markright{ \MakeUppercase{\thesection.\ #1}}
      \iffirstsectionmark
    \gdef\firstsectiontitle{ \MakeUppercase{\thesection.\ #1}}
      \fi
      \global\firstsectionmarkfalse
  }
\newif\iffirstsectionmark
\newcommand\firstsectiontitle{}

\renewcommand*\thesection{\arabic{section}} % for section display
\makeatletter \@removefromreset{section}{chapter}\makeatother  %to décorrelate sction and chapter counters (\usepackage{remreset})
%
\begin{document}
\part{PART A No chapter No Problem}
\section{Sectionn1 }
\section{Sectionn2 }
\part{PART B}
\chapter[ChapterBa OK]{ChapterBa It is OK,easy configuration 1 section}
\section{Sectionn3 }
\lipsum[1-4]
\section{Sectionn4 }
\lipsum[1]
\section{Sectionn5 }
\lipsum[1]
\chapter[ChaptBb no section]{ChapterBb No section in the page  }
\lipsum[1-4]
\section{Sectionn6 }
\lipsum[1]
\section{Sectionn7 }
\lipsum[1-3]
\chapter{ChapterBc Several sections}
\section{Sectionn8 }
\lipsum[1]
\section{Sectionn9 }
\lipsum[1]
\section{Sectionn10 }
\lipsum[1]
\section{Sectionn11 }
\lipsum[1]
\part{PART C}
\lipsum[1-8]
\section{Sectionn12 }
\section{Sectionn13 }
\end{document}

Here is the code with second strategy:

\documentclass[twoside, openright]{report}
\usepackage{remreset}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{lmodern}

\makeatletter
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
%\providecommand*{\rightfirstmark}{\expandafter\@rightmark\firstmark\@empty\@empty}
\makeatother

\fancypagestyle{myfancy}{
  \renewcommand{\headrulewidth}{1pt}
  \renewcommand{\footrulewidth}{1pt}
  \fancyhf{} 
  \fancyhead[LE,RO]{\rightmark}
  \fancyhead[RE,LO]{\leftmark}
  \fancyfoot[LE,RO]{MYFANCY \thepage}
}
\fancypagestyle{myfancychap}{
  \renewcommand{\headrulewidth}{1pt}
  \renewcommand{\footrulewidth}{1pt}
  \fancyhf{} 
  \fancyhead[LE,RO]{\rightbotmark} %HERE IS THE DIFFERENCE
  %\fancyhead[LE,RO]{\rightfirstmark}
  \fancyhead[RE,LO]{\leftmark}
  \fancyfoot[LE,RO]{MYFANCYCHAP \thepage}
}
\pagestyle{myfancy}

\makeatletter
\renewcommand{\chapter}{\clearpage   
                    \thispagestyle{myfancychap} % to keep the fancy style. 
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}
\makeatother  

\renewcommand*\thesection{\arabic{section}} % for section display
\makeatletter \@removefromreset{section}{chapter}\makeatother  %to décorrelate sction and chapter counters (\usepackage{remreset})
%
\begin{document}
...


EDIT: ADDITIONNAL REMARK : My question is related to the twoside option and

  • whether the chapter is opening on a left or right page (Indeed, if we want all the new chapters to open on a left page we can use the titlesec package with \documentclass[twoside, openleft]{report} and the little code below which work whatever the size if the first section)
  • and whether there can be several "little" sections at the begining of a chapter. (if not, one can use the code below with \usepackage[pagestyles, botmarks]{titlesec} , whatever the option openleft, openright, openany of the document, or \usepackage[pagestyles, outermarks] {titlesec} with option openright or any of the solutions given in the links mentioned above )

\documentclass[twoside]{report}
    \usepackage[pagestyles, extramarks]{titlesec}
    \newpagestyle{mystyle}{
    \headrule% 
    \sethead[\textsl{\ifthesection{\thesection. }{} \sectiontitle}][][\textsl{\ifthechapter{\thechapter.}{} \chaptertitle}]
    {\textsl{\ifthechapter{\thechapter.}{} \chaptertitle}}{}{\textsl{\ifthesection{\thesection. }{} \sectiontitle}}
    \footrule
    \setfoot[\thepage][][]{}{}{\thepage}
    }
    \pagestyle{mystyle}
    \assignpagestyle{\chapter}{mystyle}

Solution given by esdd is great and does not depend on the previous 2 conditions.

  • 2
    \chapter calls \thispagestyle{plain}, so you'll need to use \thispagestyle{fancy} each time. – John Kormylo Apr 11 '16 at 20:28
  • to John Kormylo: I know that I have to use \thispagestyle{fancy} at each time. That is why I already redefined the \chapter command in both strategies. The issue doesn't come from here. – LatexUser Apr 11 '16 at 21:18
  • @LatexUser: Alright.. Please use a @... in front of the user - name to address somebody. – user31729 Apr 11 '16 at 22:06
0

I would really recommend to use the default pagestyle plain for chapter pages. Nevertheless here is a suggestion defining an additional mark.

\documentclass[twoside, openright]{report}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{lmodern}

\usepackage{chngcntr}% defines \counterwithout
\usepackage{xpatch}
\usepackage{scrextend}% defines \ifstr

\makeatletter
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
\makeatother

\fancypagestyle{myfancy}{
  \renewcommand{\headrulewidth}{1pt}
  \renewcommand{\footrulewidth}{1pt}
  \fancyhf{} 
  \fancyhead[LE,RO]{\rightmark}
  \fancyhead[RE,LO]{\leftmark}
  \fancyfoot[LE,RO]{MYFANCY \thepage}
}
\fancypagestyle{myfancychap}{
  \renewcommand{\headrulewidth}{1pt}
  \renewcommand{\footrulewidth}{1pt}
  \fancyhf{} 
  \fancyhead[LE,RO]{%
    \ifstr{\rightmark}{}{%
      \ifstr{\rightbotmark}{}{}{\MakeUppercase{\firstmarks\mysection}}%
    }{%
      \ifstr{\rightmark}{\leftmark}{}{\rightmark}%
    }%
  }
  \fancyhead[RE,LO]{\leftmark}
  \fancyfoot[LE,RO]{MYFANCYCHAP \thepage}
}
\pagestyle{myfancy}

\newmarks\mysection
\xapptocmd\sectionmark{\marks\mysection{\thesection. \ #1}}{}{}
\xpatchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{myfancychap}}{}{}
\counterwithout{section}{chapter}

\begin{document}
\part{PART A No chapter No Problem}
\section{Sectionn1 }
\section{Sectionn2 }
\part{PART B}
\chapter[ChapterBa OK]{ChapterBa It is OK,easy configuration 1 section}
\section{Sectionn3 }
\lipsum[1-4]
\section{Sectionn4 }
\lipsum[1]
\section{Sectionn5 }
\lipsum[1]
\chapter[ChaptBb no section]{ChapterBb No section in the page  }
\lipsum[1-4]
\section{Sectionn6 }
\lipsum[1]
\section{Sectionn7 }
\lipsum[1-3]
\chapter{ChapterBc Several sections}
\section{Sectionn8 }
\lipsum[1]
\section{Sectionn9 }
\lipsum[1]
\section{Sectionn10 }
\lipsum[1]
\section{Sectionn11 }
\lipsum[1]
\part{PART C}
\lipsum[1-8]
\section{Sectionn12 }
\section{Sectionn13 }
\end{document}

enter image description here

  • @ esdd. Thanks. It can even work for \documentclass[twoside]{report}, ie with the default openany option of a report document. – LatexUser Apr 12 '16 at 17:03

Your Answer

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

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