I'm very new to LaTeX, and I'm having a terribly difficult time understanding the marking system. I'm not really grasping what The LaTeX Companion and the fancyhdr manual are saying.
I'm creating a book type document that contains chapters and sections, and I want \thechapter
.\thesection
on the corner of each header.
Because it's a book format, each chapter begins on its own page, and that's how I want it. However, there are multiple sections on a page for certain pages.
I want the first section on the even pages to be displayed for \thesection
, and I want the last section on the odd pages to be displayed for \thesection
.
As of right now, I can only get LaTeX to display the last section on any page, whether it's even or odd. Here's the current code and a picture showing what I mean.
\documentclass[a4paper,12pt,twoside,openany]{book}
\usepackage{lipsum}
\usepackage{fancyhdr}
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[RO]{\bfseries\nouppercase\leftmark}
\fancyhead[LE]{\bfseries\nouppercase\leftmark}
\fancyfoot[RO]{\thepage}
\fancyfoot[LE]{\thepage}}
\pagestyle{plain}
\renewcommand{\chaptermark}{\markboth{\thechapter}}
\renewcommand{\sectionmark}{\markboth{\thesection}}
\begin{document}
\chapter{Header Test}
\section{Sec1}
\lipsum[1]
\section{Sec2}
\lipsum[3-4]
\section{Sec3}
\lipsum[5]
\section{Sec4}
\lipsum[7]
\end{document}
What am I doing wrong?
Best Answer
To get the first new section rather than the section continued from the previous page, I think you just need to use first marks rather than top marks. This code is adapted (slightly) from Werner's answer:
The only case in which I can reproduce chapters without headers is one in which there is no
\section
command on the initial page of the chapter. In this case, I do get a header, but it is just a line with no mark for the chapter or section. Since there is no section, this makes sense. However, you might want to have the chapter marked in this case.If so, you can try this:
EDIT
I am not sure why issuing
\tableofcontents
seems to blank the headers. However, since you are usingtitlesec
, you could use the following method which utilises the conditionals from that package. I'm not sure what should happen on the contents page itself. This puts the title of the contents into the header since there is no chapter number here. (Or, rather, the number is 0.)