I am trying to make an environment where every page that contains content from that environment has a red border. I have tried using \EveryShipout from the everyshi package and setting a macro when I start and stop the environment, but this has proven unreliable (It doesn't work on the first page, and sometimes, the border of random pages isn't shown.)
An example of the code I've used is below.
\documentclass{article}
\usepackage{lipsum}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{everyshi}
\makeatletter
\EveryShipout{%
\begingroup\let\protect\@typeset@protect
\IfStrEq{\inlesson}{1}{%
\begin{tikzpicture}[remember picture, overlay]
\draw[line width=25mm, red] ([shift={(-0.55\pgflinewidth,-0.05\pgflinewidth)}]current page.north east)
--
([shift={(-0.55\pgflinewidth,0.05\pgflinewidth)}]current page.south east);
\end{tikzpicture}
}{}
\endgroup
}
\makeatother
\def\inlesson{0}
\DeclareDocumentEnvironment{lesson}{}{%
\def\inlesson{1}
\color{blue}
}{%
\newpage
}
\begin{document}
\begin{lesson}
\lipsum[2-10]
\end{lesson}
\lipsum[2-10]
\end{document}
which produces
Is there a better way to get a border on every page of an environment?

