4

Look at this example:

%! TEX program = xelatex
\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{blindtext}

\begin{document}

\AddToHook{shipout/background}
{%
    \begin{tikzpicture}[remember picture,overlay]
        \draw[opacity=0.2] ($(current page.north east)+(-1cm,-1cm)$) -- ($(current page.south east)+(-1cm,1cm)$);
    \end{tikzpicture}%
}

\blindtext[20]

\end{document}

It draws a vertical line with opacity 0.2 on the right side. However the opacity setting does not work on first page. It's similar to the example in this answer but the solution there does not work here.

7

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}
3
  • Thanks for this! By the way, I also noticed that \shade ... is not being drawn on the first page. How could one fix this?
    – Jinwen
    Feb 1 at 10:42
  • show an example, at best in a new question. Feb 1 at 11:00
  • I just proposed a new question.
    – Jinwen
    Feb 1 at 11:05
2

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.