7

The following code shows only section names in the headers.

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\rightmark}

\begin{document}

\chapter{Uno}
\section{Ducks}
\lipsum

\chapter{Dos}
\lipsum

\chapter{Tres}
\section{Frogs}
\lipsum

\end{document}

However some of my short chapters (like Chapter 2 in the example) don't have sections in them. In this case, the header on the page is empty. If there is no section, I would like the chapter name to appear at the same place.

How could I achieve this?

Please note that I don't want both chapters and sections to show. I want section title to show as a priority, and if they're not available, I want the chapter title.

  • \markleft? \markboth? – Sigur Nov 28 '13 at 21:48
5

Usually \chaptermark (called by \chapter) only sets the \leftmark. You can make it set the \rightmark; a section will take precedence over it.

\documentclass[12pt,a4paper,oneside]{report}

\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\rightmark}
\setlength{\headheight}{14.5pt}

\makeatletter
\renewcommand{\chaptermark}[1]{%
  \markboth{}{\MakeUppercase{%
    \ifnum\c@secnumdepth>\m@ne \@chapapp \ \thechapter. \ \fi #1}}%
}
% if you don't want the word `Chapter', use the following
%\renewcommand{\chaptermark}[1]{%
%  \markboth{}{\MakeUppercase{%
%    \ifnum\c@secnumdepth>\m@ne \thechapter. \ \fi #1}}%
%}
\makeatother


\begin{document}

\chapter{Uno}
\section{Ducks}
\lipsum

\chapter{Dos}
\lipsum

\chapter{Tres}
\section{Frogs}
\lipsum

\end{document}

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.