# Defining a global variable inside environment for page-specific header

Here is what I would like to achieve: I would like to be able to set the header content for individual pages at the start of the respective pages in the source code.

My thinking was to place the content in a variable \@thisheader that is put into the header and switch \thispagestyle to display it on that page only.

In the MWE, this works with the defined macro \thisheadermacro. However, since I wish to put a list into the header, I would prefer defining a new environment. And this is where I am stuck.

Using the environ package, I can get the code to compile, but the \@thisheader is not changed globally, and hence the previous header content is shown. Replacing \def with \gdef or \xdef did not help. And entirely replacing the variable definition by calling \chead{} directly did not do it either.

Defining a \newenvironment (see below) throws errors, probably because of grouping issues, which I cannot fix.

Is there a way to use my \NewEnviron approach? Or is there maybe a different, more elegant solution?

\documentclass{scrbook}

\usepackage{showframe, blindtext}

\usepackage{scrlayer-scrpage}
\pagestyle{plain}
\clearpairofpagestyles

\end{itemize}\endgroup

\usepackage{environ}

\begin{document}

\blindtext

%% The errors for this block, I cannot fix at all.
%\clearpage
%\blindtext

%% This block compiles, but does not yield the right header.
\clearpage
\blindtext

\end{document}


PS. I have also looked into using the background package. But here I run into the same problem: Using \backgroundsetup inside a group has no global effect.

It's not clear why you want to use itemize, but here it is:

\documentclass{scrbook}
\usepackage{environ}

\usepackage{showframe, blindtext}

\usepackage{scrlayer-scrpage}
\pagestyle{plain}
\clearpairofpagestyles
\makeatletter

\noexpand\begin{itemize}
\unexpanded\expandafter{\BODY}
\noexpand\end{itemize}}%
}
\makeatother

\begin{document}