[Tex/LaTex] fancyhdr: Section number, Section, colon and subsection in header

fancyhdrheader-footer

I am a bit confused with the fancyhdr options. I currently have this style for my headers:
MWE:

\documentclass[12pt,oneside]{book}%use oneside to ensure no blanks\
\usepackage[text={5.45in,8.5in}, left=1.5in, right=1.25in, top=1.25in, bottom=1.25in, headheight=15.91pt, centering]{geometry}
\usepackage[table,hyperref]{xcolor}
\usepackage{setspace, fancyhdr, amsmath, amssymb,amsthm, graphicx, color, lscape, longtable, booktabs, caption, wrapfig, hyperref, pdfpages, capt-of, microtype, lmodern, titlesec, tikz, lipsum}


% Ensures we put the section name/number on the left side and page number on right

\fancyhf{}                % clear all header and footer fields
\pagestyle{fancy}           %define page style
\pagenumbering{roman}       %page numbering style: before main text
\fancyhead[L]{\nouppercase{\rightmark}}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % make space for the rule
\fancypagestyle{plain}{%
\fancyhead{} % get rid of headers on plain pages
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt} % and the line
}
\doublespacing

\newpage
\pagenumbering{arabic}
\renewcommand{\headrulewidth}{0.5pt}


\begin{document}

\chapter{Some chapter}
\section{Section I}
\lipsum[1-1]
\subsection{Subsection}
\lipsum[1-2]
\section{Section II}
\lipsum[1-2]
\subsection{Subsection One}
\lipsum[1-2]
\subsection{Subsection Two}
\lipsum[1-2]
\end{document}
\section{Section III}
\lipsum[1-2]
\subsection{Subsection for this section}
\lipsum[1-2]
\subsection{Another subsection}
\lipsum[1-2]

The current header looks like this:

1.1 Section I

This is fine for most of my sections. However for some sections, say section II in the above MWE, I would like the header to read like so:

1.2 Section II: Subsection Two

I would then like the header to switch back to the current style in the next section (Section III):

1.3 Section III

I read several posts here on fancyhdr and the section on fancyhdr in wikibooks but I am having some trouble getting it to work, especially with the colon in between. Any help would be awesome!

PS: If this is a duplicate question, could you please point me to the relevant question?

Best Answer

\documentclass[12pt,oneside]{book}

\usepackage{fancyhdr,lipsum,setspace}

\pagestyle{fancy}           %define page style
\fancyhf{}                  % clear all header and footer fields

\renewcommand{\subsectionmark}[1]{%
  \ifsubsectioninheader
    \def\subsectiontitle{: #1}%
  \else
    \def\subsectiontitle{}%
  \fi}
\newif\ifsubsectioninheader
\def\subsectiontitle{}
\fancyhead[L]{\nouppercase{\rightmark\ifsubsectioninheader\subsectiontitle\fi}}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % make space for the rule
\fancypagestyle{plain}{%
\fancyhead{} % get rid of headers on plain pages
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt} % and the line
}
\doublespacing

\renewcommand{\headrulewidth}{0.5pt}


\begin{document}
% \frontmatter %%% use this for roman page numbering

\mainmatter %%% use this for switching to arabic page numbering

\chapter{Some chapter}

\section{Section I}
\lipsum[1]

\subsection{Subsection}
\lipsum[1-2]

\section{Section II}
\subsectioninheadertrue

\lipsum[1-2]

\subsection{Subsection One}
\lipsum[1-2]

\subsection{Subsection Two}
\lipsum[1-2]
\subsectioninheaderfalse


\section{Section III}
\lipsum[1-2]

\subsection{Subsection for this section}
\lipsum[1-2]

\subsection{Another subsection}
\lipsum[1-2]

\end{document}

We redefine \subsectionmark to keep the title, but only when the conditional \ifsubsectioninheader is true; in this case the header will show the information. The usual definition of \subsectionmark is to do nothing.

As an aside, notice the better method the book class has for switching from roman page numbering to arabic. With \frontmatter you have also other advantages: the chapters there are not numbered, but go automatically in the table of contents.

Related Question