Opacity does not work on first page
If you add after your \AddToHook
a \ShowHook{shipout/background}
you will get
-> The hook 'shipout/background':
> Code chunks:
> pgfrcs -> \put (\hoffset -1in,\voffset -1in){\pgfutil@abe \unhbox \pgfutil@abb \pgfutil@abc \global \let \pgfutil@abc \pgfutil@empty }
> Document-level (top-level) code (executed last):
> -> \begin {tikzpicture}[remember picture,overlay] \draw [opacity=0.2] ($(current page.north east)+(-1cm,-1cm)$) -- ($(current page.south east)+(-1cm,1cm)$); \end {tikzpicture}
The pgfrcs
chunk is from tikz/pgf and responsable for the opacity. But your code is later and so pgf doesn't catch it.
The problem will be resolved in future LaTeX versions, but for now you have to change the execution order yourself by giving your code a label and using a rule to change the order:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{blindtext}
\DeclareHookRule{shipout/background}{jinwen/opac}{before}{pgfrcs}
\begin{document}
\AddToHook{shipout/background}[jinwen/opac]
{%
\begin{tikzpicture}[remember picture,overlay]
\draw[opacity=0.2] ($(current page.north east)+(-1cm,-1cm)$) -- ($(current page.south east)+(-1cm,1cm)$);
\end{tikzpicture}%
}
%\ShowHook{shipout/background}
\blindtext[20]
\end{document}
You can also use a savebox, but you can't use [remember picture]
as every \usebox
will add the same pgfid
to the aux file. OTOH, \put(0pt,0pt)
corresponds to (current page.north west)
and \put(\paperwidth,-\paperheight)
to (current page.south east)
, etc. so you don't really need it. Nor do you get the aux file delay.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newsavebox{\mybackground}
\savebox{\mybackground}{\begin{tikzpicture}[baseline=(origin)]
\coordinate (origin) at (0,0);
\draw[opacity=0.2] (\paperwidth-1cm, -1cm) -- (1cm, 1cm-\paperheight);
\end{tikzpicture}}
\usepackage{blindtext}
\begin{document}
\AddToHook{shipout/background}{\put(1cm, 0pt){\usebox\mybackground}}
\blindtext[20]
\end{document}