9

Premise: I'm new with fancyhdr. I defined the following fancy style:

\lhead{}
\chead{\slshape\leftmark}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

which correctly draw the chapter number and name, top line, the bottom line, and the page number, in all the page except the first of each chapter.

So, I redefined the plain style in the following way:

\fancypagestyle{plain}{
\lhead{}
\chead{}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}}

which correctly draw the top line, the bottom line, and the page number in the first page of each chapter (note: the plain style miss the chapter number and name of the fancy style)

Now, I have a problem with the table of contents. The first page of the toc has the plain style (without the chapter number and name), but the following pages have the fancy style (with chapter number and name). How can I apply the plain style to ALL the page of the toc?

  • 1
    Welcome to TeX.SX! What document class are you using? – egreg Mar 26 '14 at 16:42
7

Since you're using the book class, I presume that everything in the front matter is intended to be under the plain pagestyle:

\documentclass[a4paper,oneside]{book}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{lipsum} % just for the example

\pagestyle{fancy}
\fancypagestyle{main}{%
  \fancyhf{}%
  \fancyhead[C]{\slshape\leftmark}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{0.4pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
}
\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{0pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
}

\appto\frontmatter{\pagestyle{plain}}
\appto\mainmatter{\pagestyle{main}}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

%%% fill with some mock text
\def\testsec{\section{B}\lipsum}
\def\test{\chapter{A}
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
}
\test
\test
\test
\test
\test
\test
\test
\test
\end{document}

However, I'd recommend against the footrule.

3

Try this in the preamble of your real document

\usepackage{etoc}
\renewcommand{\etocaftertitlehook}{\pagestyle{plain}}
\renewcommand{\etocaftertochook}{\thispagestyle{plain}}

(edit replaces \pagestyle with \thispagestyle in the line above)

MWE:

\documentclass{book}

\usepackage{fancyhdr}
\lhead{}
\chead{\slshape\leftmark}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\fancypagestyle{plain}{
\lhead{}
\chead{}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}}


\usepackage{etoc}
\renewcommand{\etocaftertitlehook}{\pagestyle{plain}}
\renewcommand{\etocaftertochook}{\thispagestyle{plain}}

\begin{document}

\tableofcontents

\chapter{ONE}

\section {A}

\chapter{TW0}

\section {B}

\newcount\cnta
\loop
\chapter{\the\cnta}

\ifnum\cnta<50
\advance\cnta 1
\repeat
\end{document}
  • Is it on purpose that even pages numbers appear in the left side of the header, rather than in the centre of the footer? – Bernard Mar 26 '14 at 18:46
  • @Bernard this is an artefact due to the succession of almost empty chapters. Each \chapter modifies the page style. By the way, I should have intialized the \ctna counter at 3. – user4686 Mar 26 '14 at 21:52
  • I have this error: \etocaftertochook undefined on this line: \renewcommand{\etocaftertochook}{\thispagestyle{plain}} – Andrea Mar 27 '14 at 8:39
  • @Andrea \etocaftertochook is available since release 1.07g of 2013/10/13. Perhaps you have TexLive2013 from a year ago, without subsequent updates. In that case just insert \thispagestyle{plain} immediately after \tableofcontents, or update your installation. – user4686 Mar 27 '14 at 11:16

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.