TransWikia.com

Opacity does not work on first page

TeX - LaTeX Asked on March 28, 2021

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.

2 Answers

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}

Correct answer by Ulrike Fischer on March 28, 2021

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){useboxmybackground}}

blindtext[20]

end{document}

Answered by John Kormylo on March 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP