3

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

out from example code

Is there a better way to get a border on every page of an environment?

1 Answers1

2

With eso-pic it seems to be easier (for me) to make it work. eso-pic is based on atbegshi but I find it easier to dig out the relevant command.

\documentclass{article}
\usepackage{lipsum}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{eso-pic}
\newif\ifinlesson

\makeatletter
\AddToShipoutPictureBG{%
    \begingroup\let\protect\@typeset@protect
    \ifinlesson%
        \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}
    \fi
    \endgroup
}
\makeatother

%\def\inlesson{0}
\DeclareDocumentEnvironment{lesson}{}{%
    \inlessontrue
    \color{blue}
}{%
    \newpage
    \inlessonfalse
}


\begin{document}
    \begin{lesson}
        \lipsum[2-10]
    \end{lesson}

    \lipsum[2-10]
\end{document}

enter image description here