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.
If you don't set a proper head height, fancyhdr
reports the following:
Package Fancyhdr Warning: \headheight is too small (XX.Xpt):
Make it at least YY.YYYYpt.
We now make it that large for the rest of the document.
This may cause the page layout to be inconsistent, however.
As such, the newly-set \headheight
will be the same throughout the document. To avoid this, I've created the following minimal document that sets a consistent head height, adding a vertical space (gap) on the first page to simulate a larger header:
\documentclass{article}
\usepackage{fancyhdr,datetime,geometry,lipsum,graphicx}
%%%%%%%%%%%%%%%%%%date in format Sunday, October 25, 2011
\usdate
\def\theday {\dayofweekname{\day}{\month}{\year}}
\def\mydate {\theday , \today}
%%%%%%%%%%return address%%%%%%%%%%
\def\myname {Frank Thorne}
\def\mydeptname {Department of Mathematics}
\def\myaffiliation {University of South Carolina}
\def\mystreet {1523 Greene Street}
\def\mycitystatezip {Columbia,~SC \quad 29208}
\def\myphone {\textit{Phone:} (803)404-4057 (home)}
\def\office {(803)777-4224}
\def\fax {\textit{Fax:}(803)777-3783}
\def\email {thorne@math.sc.edu}
\def\url {\texttt{http://www.math.sc.edu/~thornef}} % NOTE: use $\sim$ for tilde
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%margins%%%%%%%%%%%%%%%%%%
\geometry{
margin = 1in,
headheight = 13.5pt
}
\setlength{\parindent}{0pt} %controls how the indentation of new paragraphs behaves
\setlength{\parskip}{6pt} %controls the space between paragraphs
%%%%%%%%%%%%%%%%letterhead%%%%%%%%%%%%%%%%
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\pagestyle{fancy}%
\fancypagestyle{firstpage}{
\fancyhf{}% Clear header/footer
%%%usc logo
\fancyhead[L]{\raisebox{\dimexpr-\height+\baselineskip}[0pt][0pt]{\includegraphics[width=2.5in]{example-image}}}
%%%%%%%%%%return address on right%%%%%%%
\fancyhead[R]{\leavevmode\footnotesize\itshape%
\begin{tabular}[t]{l@{}}
\myname \\ \mydeptname \\
\myaffiliation \\ \mystreet \\
\mycitystatezip \\ \email \\[\normalbaselineskip]
\mydate
\end{tabular}}
}
\begin{document}
\thispagestyle{firstpage}
\vspace*{8\baselineskip}
\lipsum[1-10]
\bigskip
\hspace{.5\textwidth}%
\begin{tabular}{@{}l}
Sincerely, \\[2\normalbaselineskip]
Frank Thorne \\
\textit{Assistant Professor of Mathematics}
\end{tabular}
\end{document}
I've used geometry
to adjust the page layout - it's just easier/more convenient.
Best Answer
You can put all the material in the left or right header as needed, with the page number in a box of fixed width using the
\makebox
macro and the other text immediately after or before corresponding to even or odd pages. The syntax for\makebox
iswith
pos
one ofl
,r
orc
, for left, right or centered material within the box. It is good to specify thewidth
in units that depend on the font, such asem
. Below I have used1.5em
, which gives you space for a three digit page number.Left page:
Right page:
Beware of spaces added in your macros - for example, your orginal code added a space before
Kapitel
, which I have removed above.