7

I have a code for printing page numbers in filled circles. But it only appears in even numbers.

\documentclass[12pt,a4paper]{book}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{fancyhdr}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{blindtext}


\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,fill=gray!50,inner sep=2pt] (char) {#1};}}

% header style
\pagestyle{fancy}
\fancyhf{}
\fancyhead[EL]{\nouppercase\leftmark}
\fancyhead[OR]{\nouppercase\rightmark}
\fancyfoot[C]{\circled{\thepage}}

\begin{document}
\tableofcontents
\newpage
\chapter{Chapter}
\blindtext

\chapter{Chapter}
\blindtext

\section{Chapter}
\blindtext

\end{document}

Example

Werner
  • 603,163
Xeo
  • 305

2 Answers2

9

The problem is not with all odd pages; the problem is with pages that internally have assigned the plain style (which, with the default settings, happen to be odd), such as the first page of each chapter. You can redefine plain to give you the desired formatting:

\fancypagestyle{plain}{%
  \fancyhf{}
  \fancyfoot[C]{\circled{\thepage}}
  \renewcommand{\headrulewidth}{0pt}
}

A complete example:

\documentclass[12pt,a4paper]{book}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{fancyhdr}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{blindtext}


\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,fill=gray!50,inner sep=2pt] (char) {#1};}}

% header style
\pagestyle{fancy}
\fancyhf{}
\fancyhead[EL]{\nouppercase\leftmark}
\fancyhead[OR]{\nouppercase\rightmark}
\fancyfoot[C]{\circled{\thepage}}

\fancypagestyle{plain}{%
  \fancyhf{}
  \fancyfoot[C]{\circled{\thepage}}
  \renewcommand{\headrulewidth}{0pt}
}

\begin{document}
\tableofcontents
\newpage
\chapter{Chapter}
\blindtext

\chapter{Chapter}
\blindtext

\section{Chapter}
\blindtext

\end{document}

The result:

enter image description here

Gonzalo Medina
  • 505,128
  • \renewcommand{\headrulewidth}{0pt} is missing in \fancypagestyle{plain}{...}: the rule over the chapter title must be removed. The page numbers in a filled circle are not the best typographical device I would think to, to be honest. – egreg Nov 14 '14 at 18:31
  • @egreg I'll remove it right away :) – Gonzalo Medina Nov 14 '14 at 18:36
3

This is very much the same problem as in Why doesn't \pagestyle{empty} work on the first page of a chapter? The first page of every chapter is issued with the plain page style by default. You'll either have to redefine plain, or add some adjustments to the \chapter command to not issue plain.

The minimal example below patches \chapter to insert a different page style - newly-defined using fancyhdr - called chapter. The chapter page style only inserts a footer with circled page numbers, but no headers:

enter image description here

\documentclass{book}

\usepackage{fancyhdr,xcolor,tikz,etoolbox}
\usepackage{blindtext}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,fill=gray!50,inner sep=2pt] (char) {#1};}}

% header style
\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\fancyhead[EL]{\nouppercase\leftmark}
\fancyhead[OR]{\nouppercase\rightmark}
\fancyfoot[C]{\circled{\thepage}}
\fancypagestyle{chapter}{% New chapter page style/used with chapter first-pages only
  \fancyhf{}% Clear header/footer
  \renewcommand{\headrulewidth}{0pt}% No header rule
  \fancyfoot[C]{\circled{\thepage}}
}

% Replace chapter first-page page style from 'plain' to 'chapter'
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\chapter}{plain}{chapter}{}{}

\begin{document}
\tableofcontents

\clearpage

\chapter{Chapter}
\blindtext

\chapter{Chapter}
\blindtext

\section{Chapter}
\blindtext

\end{document}
Werner
  • 603,163