3
\documentclass[10pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{fancyhdr}


\title{My Book}
\author{Me}
\begin{document}
\maketitle

\chapter{My Chapter}
\lipsum
\end{document}

How can I have centered alternating book title and chapter title at the top centered in between rule, and centered page number at the bottom centered in between rule? (For an idea see the image below.)

enter image description here

1 Answer 1

2

With \hrulefill, you get a horizontal line that spans all available space. So \hrulefill\thepage\hrulefill gives you "line page-number line" spanning the whole width of the text area. This you can set as the footer with \fancyfoot[C]. You want to tweak the appearance a bit:

  • The lines should not directly connect to the page number, so you want to add same horizontal space to the left and right of the number like \quad.
  • By default, the line is drawn on the baseline of the page number, but in your example it is vertically centered around the number. So you need to lower the page number with something like \raisebox{-3pt}.

The same is possible for the header and titles, with the same "tweaking" required. Some more things to consider:

  • You can use \markboth{\@title}{\@title} (maybe add some text formatting macros) to use the title that you set in \title also for the headers. Be sure to call it after \title but before \maketitle.
  • You need to redefine \chaptermark, \sectionmark, etc. to ensure that you don't overwrite the leftmark with the booktitle, and only insert the chapter titles (and, if applicable, of sections, etc.) to the rightmark.

As a starting point for developing your header & footer style, a minimal example below.

\documentclass{book}
\usepackage{lipsum}

\title{My Book}
\author{Me}

\makeatletter
\markboth{\textsc{\@title}}{\textsc{\@title}}
\makeatother

\usepackage{fancyhdr}
\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[C]{\hrulefill\quad\raisebox{-3pt}{\thepage}\quad\hrulefill}
\fancyhead[CO]{\hrulefill\quad\raisebox{-3pt}{\leftmark}\quad\hrulefill}
\fancyhead[CE]{\hrulefill\quad\raisebox{-3pt}{\rightmark}\quad\hrulefill}
\renewcommand{\headrulewidth}{0pt}
}
\pagestyle{plain}

\renewcommand{\chaptermark}[1]{\markright{\textsc{#1}}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{}

\begin{document}
\maketitle
\chapter{My Chapter}
\lipsum
\end{document}

enter image description here

2
  • How do I omit the header at the first page of the chapter? (And also the page number.)
    – blackened
    Commented Dec 4, 2019 at 9:34
  • Have a look at the fancyhdr documentation. In this example, I redefined the plain pagestyle because that's the one used by the first pages of the chapters (I wanted to have a short example, and also the screenshot you posted has headers and footers on the opening page). Instead, you want to use the plain and fancy styles separately, which means you have to define both separately.
    – Tiuri
    Commented Dec 4, 2019 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .