# Header not displaying correctly on chapter page due to subsection

(Complete source code below.)

(Note: Not sure if I am using LaTeX or just TeX. I am editing with TeXworks, as TeXStudio was not rendering what I typed without +20 seconds of lag.)

I apologize if this question has already been asked. I searched tex.stackexchange.com, the internet, and the fancyhdr documentation, but could not find the solution.

Simply put, I used fancyhdr to setup headers, however the chapter pages are not displaying correctly, despite using fancyhdr to redefine the plain style.

The best clue I could get from the fancyhdr documentation was the following:

Another problem with the marks in the standard LATEX classes is that the higher level sectioning commands (e.g. \chapter) call \markboth with an empty right argument. This means that on the ﬁrst page of a chapter (or a section in article style) the \rightmark will be empty. If this is a problem you must manually insert extra \markright commands or redeﬁne the \chaptermark (\sectionmark) commands to issue a \markboth command with two decent parameters.

I am not clear how to implement this solution, despite many attempts.

In detail, this is what I am trying to do (the bold items are causing problems):

1. Left Header: [Author's Name]
2. Right Header: [Book Author's Name]>["Chapter"+Chapter#]>[Subsection Name]
3. The same Header must show on all pages, including the chapter pages, and excluding the Table of Contents. [For solution, see egreg's response.]
4. Subsections must not be automatically numbered, i.e., they must be "* commands".
5. Subsections must appear in Table of Contents, despite being "* commands".

P.S. [Solved, see solution below:] Is there a way (1) to not have the subsections automatically numbered and (2) included in the Table of Contents, yet not have to input each subsection title three times? Just curious. See below:

\subsection*{Title of Subsection 3\markright{Title of Subsection 3}{}}
\addcontentsline{toc}{subsection}{Title of Subsection 3}


Solution #1 (Simplest): In preamble, write \setcounter{secnumdepth}{1}.

Solution#2: In preamble, create a new command by writing:

\newcommand{\newsubsection}[1]{\subsection*{#1\markright{#1}}%


To save everyone some time compiling, on the left is the chapter page, and on the right shows what the chapter page header should look like this:

\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}{}}
\renewcommand{\subsectionmark}[1]{\markright{#1}{}}

% General Header and Footer
\lfoot{}
\cfoot{}
\rfoot{Page \thepage\ of \pageref{LastPage}}

% Chapter Header and Footer
\fancypagestyle{plain}{%
\lfoot{}
\cfoot{}
\rfoot{Page \thepage\ of \pageref{LastPage}}}

% Header/Footer Separation-line Width
\renewcommand\footrulewidth{0.5pt}

\setlength\parindent{0pt} % Remove paragraph indentation.

% NAME AND CLASS SECTION
\newcommand{\DocumentAuthor}{Author Name}
\newcommand{\BookAuthor}{Book Author}

%DOCUMENT
\begin{document}

\tableofcontents{}
\thispagestyle{empty}

\chapter{Title of Chapter 1}
\section{Sample Problems}
\subsection*{Title of Subsection 1\markright{Title of Subsection 1}{}}
\addcontentsline{toc}{subsection}{Title of Subsection 1}
\pagebreak{}
\subsection*{Title of Subsection 2\markright{Title of Subsection 2}{}}
\addcontentsline{toc}{subsection}{Title of Subsection 2}
\subsection*{Title of Subsection 3\markright{Title of Subsection 3}{}}
\addcontentsline{toc}{subsection}{Title of Subsection 3}
\pagebreak{}
\subsection*{Title of Subsection 4\markright{Title of Subsection 4}{}}
\addcontentsline{toc}{subsection}{Title of Subsection 4}

\end{document}

-
Excellently asked question, great MWE, you really did your homework first, thanks for that! The only flaw of your question is that it technically contains two distinct questions, which is frowned upon on here. However, since your question 6 refers to a pretty basic technique and question 3 is really the tricky one (It can't be solved as described in tex.stackexchange.com/q/17062/4012), I recommend you edit your question and turn question six into a kind of "P.S." sneaky little extra question, that'll pass. – doncherry Oct 30 '12 at 5:42
Alright, I didn't get my solution for the main problem to work, but for the smaller one: Just add \newcommand{\mysubsection}[1]{\subsection*{#1\markright{#1}{}}% \addcontentsline{toc}{subsection}{#1}} to your preamble and use \mysubsection{foo} as your only subsection command. For more information on this technique, you could start with section 6.1.1 of The Not So Short Introduction to LaTeX2ε, or look at the synopsis at section 13.1 of the unofficial LaTeX reference manual. – doncherry Oct 30 '12 at 6:05
And finally: lmodern is a good choice for a font package. It loads the Latin Modern fonts, which are extremely similar to the Original Computer Modern fonts, but more extensive and technically superior. See tex.stackexchange.com/questions/1390/latin-modern-vs-cm-super (even though cm-super isn't the original CM fonts). – doncherry Oct 30 '12 at 6:07
Wow, thanks for the great reply. I did not realize that only one question at a time was allowed, so thank you for pointing that out. I turned Item 6 into a P.S. Perhaps, I should make another question out of it? – Jamie Oct 30 '12 at 6:36
The solution to Item 6 worked perfectly. Thank you. – Jamie Oct 30 '12 at 6:53

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{}
\fancyfoot[C]{Page \thepage\ of \pageref{LastPage}}

% Chapter Header and Footer
\fancypagestyle{plain}{%
\fancyfoot[C]{Page \thepage\ of \pageref{LastPage}}%
}

% Header/Footer Separation-line Width
\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.

-
+1. Perhaps \newcommand*{\subsectiontitle}{} is appropriate before redefining \chaptermark & friends to make sure that \subsectiontitle isn't already defined (say, by another package). – lockstep Oct 30 '12 at 8:47
@lockstep Thanks; I've changed the macro name to something less likely to be already defined (and changed the initialization to use \newcommand). – egreg Oct 30 '12 at 8:50
@egreg Thank you! The solution works perfectly. I'm not sure why, but it works. Tomorrow, I will have to look into \noexpand and \gdef. Again, thank you. – Jamie Oct 30 '12 at 9:15
@egreg Thank you very much for the explanation, as well as the second solution, which allows only the first subsection to present in the header. I was going to post another question about this, so it is good I reread your post! As for the explanation, I will need to look at it more carefully later tonight when I have more time. I tried removing the \noexpand command to see what effect it might have, but I simply received an error message. I will do more research into this matter later today, as I need to get to the gym. :) Thank you very much, egreg and everyone who contributed! – Jamie Oct 31 '12 at 0:38

For unnumbered subsections that are nevertheless included in the ToC, add \setcounter{secnumdepth}{1} to the preamble and use \subsection instead of \subsection* in the text body. Together with the fact that you've already redefined \subsectionmark (BTW, \markright only takes one argument), this also (mostly) removes the need to use \markright in the text body.

A workaround for your main question is to add \clearpage\markright{Title of subsection 1} before the chapter start.

\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}{}}
\renewcommand{\subsectionmark}[1]{\markright{#1}}

% General Header and Footer
\lfoot{}
\cfoot{}
\rfoot{Page \thepage\ of \pageref{LastPage}}

% Chapter Header and Footer
\fancypagestyle{plain}{%
\lfoot{}
\cfoot{}
\rfoot{Page \thepage\ of \pageref{LastPage}}}

% Header/Footer Separation-line Width
\renewcommand\footrulewidth{0.5pt}

\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}

\clearpage
\markright{Title of Subsection 1}

\chapter{Title of Chapter 1}
\section{Sample Problems}
\subsection{Title of Subsection 1}
\clearpage
\subsection{Title of Subsection 2}
\subsection{Title of Subsection 3}
\clearpage
\subsection{Title of Subsection 4}

\end{document}

-
Thank you very much. The \setcounter{secnumdepth}{1} worked very well. – Jamie Oct 30 '12 at 8:03
The solution to the main problem works, however it is a solution that will have to be manually tweaked each time a new chapter comes. Is there a way that LaTeX can automatically use the appropriate title? And thanks for pointing out that \markright only takes one argument. – Jamie Oct 30 '12 at 8:11
@Jamie I have no solution right now. Note that you can unaccept my answer if a better answer comes along. – lockstep Oct 30 '12 at 8:13
@Jamie Perhaps you should unaccept my answer right now (and only keep the upvote) because potential answerers will look more closely at questions without accepted answers. – lockstep Oct 30 '12 at 8:18
Okay, thanks for the tip. – Jamie Oct 30 '12 at 8:21