7

Using afterpage to set a page style after some page only works for \thispagestyle, but not for \pagestyle. Why is that? How can I make it work?

\lipsum[1-20] produces 4 pages of text, Lorem Ipsum style. So, the following document consists of 8 pages.

\documentclass{article}
\usepackage{fancyhdr,afterpage,lipsum}% http://ctan.org/pkg/{fancyhdr,afterpage,lipsum}
\fancypagestyle{pagestyleA}{
  \fancyhf{}% Clear header/footer
  \fancyhead[C]{pagestyleA}% Header
  \fancyfoot[C]{A--\thepage}% Footer
}
\fancypagestyle{pagestyleB}{
  \fancyhf{}% Clear header/footer
  \fancyhead[C]{pagestyleB}% Header
  \fancyfoot[C]{B--\thepage}% Footer
}
\pagestyle{empty}
\begin{document}
\afterpage{\thispagestyle{pagestyleA}}\lipsum[1-20]
\clearpage
\afterpage{\pagestyle{pagestyleB}}\lipsum[1-20]
\end{document}

Using \afterpage{\thispagestyle{pagestyleA}} at the start prints page 2 with page style pagestyleA.

enter image description here

It is expected that the second \afterpage{\pagestyle{pagestyleB}} should deliver pages 6-8 with page style pagestyleB. However, it does nothing...

enter image description here

  • 3
    The token list in the argument of \afterpage is processed in a group. The difference is that \thispagestyle acts globally, while \pagestyle doesn't. – egreg Dec 6 '12 at 7:54
  • egrep's analysis is correct. There is discussion of this on page 17 of the fancyhdr documentation. – Andrew Swann Dec 6 '12 at 7:58
11

The afterpage package documentation is not explicit on the subject, but it turns out that the token list given as argument to \afterpage is processed inside a group, so declarations inside it are undone at the end of the group, unless expressly made \global.

The effect of \thispagestyle is indeed global: its definition is

\def\thispagestyle#1{%
  \@ifundefined{ps@#1}%
    \undefinedpagestyle
    {\global\@specialpagetrue\gdef\@specialstyle{#1}}}

On the other hand, the definition of \pagestyle is

\def\pagestyle#1{%
  \@ifundefined{ps@#1}%
    \undefinedpagestyle
    {\@nameuse{ps@#1}}}

and therefore one can't say \global\pagestyle{pagestyleB}: the actual assignments are performed when executing the instructions contained in \ps@pagestyleB.

As Andrew Swann remarks in his comment, the problem is discussed on page 17 of fancyhdr's documentation.

Depending on the intended application, the problem could be solved by using a "conditionally defined" page style and using \afterpage to globally change the conditional's truth value. Say

\documentclass{article}
\usepackage{fancyhdr,afterpage,lipsum}

\newif\ifpsB
\fancypagestyle{pagestyleAB}{
  \fancyhf{}% Clear header/footer
  \fancyhead[C]{pagestyleA}% Header
  \fancyfoot[C]{\ifpsB B\else A\fi--\thepage}% Footer
}
\pagestyle{pagestyleAB}

\begin{document}
\afterpage{\thispagestyle{empty}}\lipsum[1-20]
\clearpage
\afterpage{\global\psBtrue}\lipsum[1-20]
\end{document}

Here pages 1 and 3–5 will have “A—#” at their footer, pages 6–8 will have “B—#” and page 2 will have the empty page style.

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.