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 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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted
\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.

share|improve this answer
    
thank you so much for this very cool way to do it! It helps me a bit more with understanding how fancyhdr works. Thanks also for that nifty book class \frontmatter trick! I am going to try it out on my dissertation. –  Ariel Apr 12 '12 at 16:23
    
I just noticed a slight problem with this solution. I’m using the \subsectioninheadertrue and \subsectioninheaderfalse to redefine the header multiple times in my document. It works fine the first time. However, when I use it for the 2nd time, my section headers are appearing a page earlier than they should. Any ideas why this is happening? –  Ariel Apr 29 '12 at 21:56
1  
@Ariel Does it happen always? –  egreg Apr 29 '12 at 22:06
    
Thank you for asking that! I did some more checks and the problem only appears in one condition - when the section (where I want the header to show the subsection) starts at the top of a page. –  Ariel Apr 29 '12 at 22:38
1  
@Ariel Put a \pagebreak command just before that \section command. Do this check when the document is in final form, so as not to spoil it during revisions. The problem is that the \section command is read before the page break decision is made; with \pagebreak this won't happen. –  egreg Apr 29 '12 at 22:42

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.