Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
2  
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
    
Thanks @AndrewSwann; I was unaware of this discussion. –  Werner Dec 6 '12 at 15:12

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
    
Thanks for the clear discussion on the issue. –  Werner Dec 6 '12 at 15:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.