We must give more structure to the right mark emitted by the \chapter
command; so we do
\renewcommand{\chaptermark}[1]{%
\markboth{\chaptername\ \thechapter}
{\noexpand\firstsubsectiontitle}}
The \subsectiontitle
macro will be defined by the \subsection
command; of course it won't work if no subsection starts in the first page:
\renewcommand{\subsectionmark}[1]{%
\markright{#1}\gdef\firstsubsectiontitle{#1}}
Here's an example; notice some changes I've made to your code. For instance I've redefined the \sectionmark
command so it doesn't emit any mark.
The solution to the numbering problem of subsection is to \setcounter{secnumdepth}{1}
.
\documentclass{report}
% PACKAGES
\usepackage{fancyhdr}
\usepackage{lastpage} % Allows referencing of the last page to allow footer to read: "Page [Current page] of [Total number of pages]."
\usepackage{comment} % Allows comments of the type: \begin{comment}This text is commented out.\end{comment}
%\usepackage[colorlinks=true]{hyperref} % Turns table of contents and labels into clickable links. Commented out to speed up compiling.
\usepackage[T1]{fontenc} % Allows use of ">" symbol.
\usepackage{lmodern} %Not really sure what this is for.
% FORMATTING
% Header and Footer
\pagestyle{fancy}
% Allows calling chapter and section names in headers and footers.
\renewcommand{\chaptermark}[1]{%
\markboth{\chaptername\ \thechapter}
{\noexpand\firstsubsectiontitle}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{%
\markright{#1}\gdef\firstsubsectiontitle{#1}}
\newcommand\firstsubsectiontitle{}
% General Header and Footer
\fancyhf{}
\fancyhead[L]{\DocumentAuthor}
\fancyhead[R]{\BookAuthor>\leftmark>\rightmark}
\fancyfoot[C]{Page \thepage\ of \pageref{LastPage}}
% Chapter Header and Footer
\fancypagestyle{plain}{%
\fancyhf{} \fancyhead[L]{\DocumentAuthor}%
\fancyhead[R]{\BookAuthor>\leftmark>\rightmark}%
\fancyfoot[C]{Page \thepage\ of \pageref{LastPage}}%
}
% Header/Footer Separation-line Width
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}
\setlength\parindent{0pt} % Remove paragraph indentation.
% NAME AND CLASS SECTION
\newcommand{\DocumentAuthor}{Author Name}
\newcommand{\BookAuthor}{Book Author}
\setcounter{secnumdepth}{1}
%DOCUMENT
\begin{document}
\tableofcontents
\thispagestyle{empty}
\chapter{Title of Chapter 1}
\section{Sample Problems}
\subsection{Title of Subsection 1}
\pagebreak{}
\subsection{Title of Subsection 2}
\subsection{Title of Subsection 3}
\pagebreak{}
\subsection{Title of Subsection 4}
\end{document}
With this code the first subsection in the page will be put in the header.
What's \noexpand\firstsubsectiontitle
doing? The \markboth
and \markright
commands must record the current values of the counters and titles, so their arguments are passed through an \edef
; this, in the case of the first chapter and English language, would set a left mark with value Chapter\ 1
. If we said only \firstsubsectiontitle
in the second argument, it would be expanded leaving a wrong right mark because it would be empty for the first chapter and would contain the last subsection in the preceding chapter!
So we say "dear TeX, don't expand \firstsubsectiontitle
right now, but only when you'll set the header."
Actually, if there can be more than one subsection in the chapter pages, a different solution should be used:
% Allows calling chapter and section names in headers and footers.
\renewcommand{\chaptermark}[1]{%
\markboth{\chaptername\ \thechapter}
{\noexpand\firstsubsectiontitle}%
\global\firstsubsectionmarktrue}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{%
\markright{#1}%
\iffirstsubsectionmark
\gdef\firstsubsectiontitle{#1}%
\fi
\global\firstsubsectionmarkfalse}
\newif\iffirstsubsectionmark
\def\firstsubsectiontitle{}
so that the first subsection would set the mark.
Simply add
\fancyfoot[C]{\Roman{chapter}\,--\,\thepage}
to your current settings. Since, for consistency's sake this should also apply to the first page of each chapter (in ehich the plain
style is used), a redefinition of plain
will also be needed:
\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[C]{\Roman{chapter}\,--\,\thepage}
\renewcommand{\headrulewidth}{0pt}
}
A complete example:
\documentclass[a4paper]{book}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\Roman{chapter}\,--\,\thepage}
\renewcommand{\headrulewidth}{0pt}
\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[C]{\Roman{chapter}\,--\,\thepage}
\renewcommand{\headrulewidth}{0pt}
}
\begin{document}
\chapter{Test chapter one}
\lipsum[1-40]
\chapter{Test chapter two}
\lipsum[1-40]
\chapter{Test chapter three}
\lipsum[1-40]
\end{document}
Some images of some of the footers in different chapters:
Of course, activate these settings from the point in which chaters will be numbered on. Since this applies particularly for the redefinition of plain, you could conditionally make the redefinition of the footer depending on wheter you are on the \mainmatter
or not:
\makeatletter
\fancypagestyle{plain}{%
\fancyhf{}
\if@mainmatter
\fancyfoot[C]{\Roman{chapter}\,--\,\thepage}
\else
\fancyfoot[C]{\thepage}
\fi
\renewcommand{\headrulewidth}{0pt}
}
\makeatother
The code:
\documentclass[a4paper]{book}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\Roman{chapter}\,--\,\thepage}
\renewcommand{\headrulewidth}{0pt}
\makeatletter
\fancypagestyle{plain}{%
\fancyhf{}
\if@mainmatter
\fancyfoot[C]{\Roman{chapter}\,--\,\thepage}
\else
\fancyfoot[C]{\thepage}
\fi
\renewcommand{\headrulewidth}{0pt}
}
\makeatother
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{Test chapter one}
\lipsum[1-40]
\chapter{Test chapter two}
\lipsum[1-40]
\chapter{Test chapter three}
\lipsum[1-40]
\end{document}
Best Answer
Using
you'll get the chapter title in the leftmark, and using
you'll get the section number (without the chapter number prefix) and title for the rightmark.
A complete example:
The result:
Your \lhead, \rhead commands can be replaced with the "more modern"