5

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?

3

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}
  • Gah, I was able to get this to work in the test document, but I'm still not able to get it to work in the actual document. Not sure why. EDIT: Aha! The problem definitely has something to do with \tableofcontents. I reproduced the problem by using it: pastebin.com/yqfb0fkz – Nickolas Peter O'Malley Oct 22 '14 at 3:00
  • @NickolasPeterO'Malley See edited answer. I don't know why it doesn't work but this seems to fix it. – cfr Oct 22 '14 at 11:54
3

Instead of answering your direct question, I'd answer it with an alternative suggestion. That is, using titleps.

Using switches like \toptitlemarks, \firsttitlemarks or \bottitlemarks you can extract the information for the specific section in a dictionary-style format. Below I've redefined the plain page style (just like you did with fancyhdr) setting the header and footer accordingly.

enter image description here

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

\usepackage{titleps}
\renewpagestyle{plain}{
  \sethead[\toptitlemarks\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]
\end{document}

The use of titleps is eased when reading titleps for fancyhdr users. The discussion on dictionary-style headings is mentioned in the titleps documentation (section 4 Marks).

  • I tried this, but I also use titlesec in the document. I'm getting an 'Incompatible package' error. Am I unable to use both? – Nickolas Peter O'Malley Oct 21 '14 at 8:19
  • 1
    @NickolasPeterO'Malley: titlesec should be fully-compatible with titleps as its written by the same author. However, don't use fancyhdr as well. – Werner Oct 21 '14 at 8:32
  • I figured out that I can use them both if I don't use the titleps package, but just do \usepackage[pagestyles]{titlesec}. This works, and it looks like titleps will work best. But, now I'm running in to two other problems. First, the header doesn't even show up on some of the first pages of a new chapter. Second, on some pages that have sections from the last page, that last section is displayed in the header instead of the first section of that page. Are these things that can even be fixed? – Nickolas Peter O'Malley Oct 21 '14 at 8:35
  • 1
    @NickolasPeterO'Malley I'm a big fan of fancyhdr and use it almost all the time. However, if you are using titlesec, you really would be best advised to use titleps and titlesec without fancyhdr as you are much more likely to find things work cooperatively rather than antagonistically. – cfr Oct 21 '14 at 12:58
  • @cfr I'm using it without fancyhdr. So far, it's a bit easier to use and I'm liking it. I'm just running in to the problem now that the header doesn't show up on the first page of some new chapters, and also that the mark isn't the first new section on a page, but the first section on the page from the last page. If I could fix these things, I'd be a happy camper! – Nickolas Peter O'Malley Oct 21 '14 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.