[Tex/LaTex] Choosing \firstmark and \botmark with \leftmark

fancyhdr

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}

gah

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:

\documentclass[twoside,openany]{book}
\usepackage{lipsum}

\usepackage{titleps}
\renewpagestyle{plain}{
  \sethead[\firsttitlemarks\bfseries\thesection][][]% even-left | even-center | even-right
  {}{}{\bottitlemarks\bfseries\thesection}% odd-left | odd-center | odd-right
  \setfoot[\thepage][][]% even-left | even-center | even-right
  {}{}{\thepage}% odd-left | odd-center | odd-right
  \setheadrule{0.4pt}
}
\pagestyle{plain}

\begin{document}
  \chapter{Header Test}
  \section{Sec1}
  \lipsum[1]
  \section{Sec2}
  \lipsum[3-4]
  \section{Sec3}
  \lipsum[5]
  \section{Sec4}
  \lipsum[7]
  \chapter{New Chapter}
  \section{Sec5}
  \lipsum[2]
  \section{Sec6}
  \lipsum[6]
  \section{Sec7}
  \lipsum[8-9]
  \section{Sec8}
  \lipsum[10]
\end{document}

fancy headers

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:

\renewpagestyle{plain}{
  \sethead[\firsttitlemarks\bfseries\ifnum\value{section}=0 \thechapter\else\thesection\fi][][]% even-left | even-center | even-right
  {}{}{\bottitlemarks\bfseries\ifnum\value{section}=0 \thechapter\else\thesection\fi}% odd-left | odd-center | odd-right
  \setfoot[\thepage][][]% even-left | even-center | even-right
  {}{}{\thepage}% odd-left | odd-center | odd-right
  \setheadrule{0.4pt}
}

chapter only

EDIT

I am not sure why issuing \tableofcontents seems to blank the headers. However, since you are using titlesec, 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.)

\documentclass[twoside,openany]{book}
\usepackage{lipsum}
\usepackage[pagestyles]{titlesec}
\renewpagestyle{plain}{
  \sethead[\firsttitlemarks\bfseries\ifthesection{\thesection}{\ifthechapter{\thechapter}{\chaptertitle}}][][]% even-left | even-center | even-right
  {}{}{\bottitlemarks\bfseries\ifthesection{\thesection}{\ifthechapter{\thechapter}{\chaptertitle}}}% odd-left | odd-center | odd-right
  \setfoot[\thepage][][]% even-left | even-center | even-right
  {}{}{\thepage}% odd-left | odd-center | odd-right
  \setheadrule{0.4pt}
}
\pagestyle{plain}

\begin{document}

  \tableofcontents

  \chapter{Header Test}
  \section{Sec1}
  \lipsum[1]
  \section{Sec2}
  \lipsum[3-4]
  \section{Sec3}
  \lipsum[5]
  \section{Sec4}
  \lipsum[7]
  \chapter{New Chapter}
  \section{Sec5}
  \lipsum[2]
  \section{Sec6}
  \lipsum[6]
  \section{Sec7}
  \lipsum[8-9]
  \section{Sec8}
  \lipsum[10]
  \chapter{GAH}
  \lipsum[1-4]
  \chapter{New Chapter AAAA}
  \section{Sec5}
  \lipsum[2]
  \section{Sec6}
  \lipsum[6]
  \section{Sec7}
  \lipsum[8-9]
  \section{Sec8}
  \lipsum[10]
  \chapter{GAH1}
  \section{Sec8}
  \lipsum[10]
  \lipsum[1-4]
  \lipsum[1-4]
  \lipsum[1-4]
  \lipsum[1-4]
  \chapter{New CHAPTER AAAAA}
  \section{Sec5}
  \lipsum[2]
  \section{Sec6}
  \lipsum[6]
  \section{Sec7}
  \lipsum[8-9]
  \section{Sec8}
  \lipsum[10]
\end{document}