5

On the page after an environment ends, I would like to switch header style. The code below works as I would like it to. However, the use of everypage feels a little like overkill and I wonder whether there is a means built into one of the loaded packages (including zref), or otherwise "better" way to accomplish the same.

\documentclass{article}
%\usepackage{zref}
\usepackage{enumitem}
\usepackage{titleps}
\usepackage{xparse}
\usepackage{everypage}

\newpagestyle{before}{
    \sethead{}{before}{}
}
\pagestyle{before}

\newpagestyle{after}{
    \sethead{}{after}{}
}

\ExplSyntaxOn

\int_new:N \g_last_item_page_int
\int_gset:Nn \g_last_item_page_int {\c_max_int}

\newlist{mylist}{enumerate}{1}
\setlist[mylist]{
 label = (\arabic*),
 after = \int_gset:Nn \g_last_item_page_int {\thepage}
}

\AddEverypageHook{
    \int_compare:nT {\thepage > \g_last_item_page_int}
      {\pagestyle{after}}
}

\ExplSyntaxOff

\begin{document}
Header should be "before".
\newpage
\begin{mylist}
    \item Header should be "before".
    \newpage
    \item Header should be "before".
\end{mylist}
Header should be "before".

\newpage

Header should be "after".

\end{document}
Scott H.
  • 11,047

1 Answers1

5

The following uses afterpage and is influenced by Page styles only work for \thispagestyle under afterpage.

Since the setting of a page style using \pagestyle is not global (in comparison to setting it using \thispagestyle), \afterpage needs some help. Therefore, setting the page style to after (via \ps@after) \aftergroup in \afterpage provides a workaround:

\usepackage{afterpage}% http://ctan.org/pkg/afterpage
...
\makeatletter
\setlist[mylist]{
  label = (\arabic*),
  after = \afterpage{\aftergroup\ps@after}
}
\makeatother

A better might be to use conditionals or control sequences as part of the header/footer composition, since these assignments can be made global inside \afterpage without any trickery. For example:

\def\headertext{before}
\newpagestyle{myheader}{
    \sethead{}{\headertext}{}
}
\pagestyle{myheader}

\newlist{mylist}{enumerate}{1}
\setlist[mylist]{
 label = (\arabic*),
 after = \afterpage{\gdef\headertext{after}}
}
Werner
  • 603,163
  • Thanks Werner, since I'll be switching to a completely different heading style (no headrule etc.) so would need to use the first option which feels cleaner than what I'm using currently. – Scott H. Apr 11 '13 at 05:38