看这个例子:
%! 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}
它在右侧绘制了一条不透明度为 0.2 的垂直线。但是不透明度设置在首页上不起作用。它类似于这个答案但那里的解决方案在这里不起作用。
答案1
\AddToHook
如果你在a后添加,\ShowHook{shipout/background}
你会得到
-> 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}
该pgfrcs
块来自 tikz/pgf,负责不透明度。但您的代码较晚,因此 pgf 无法捕获它。
该问题将在未来的 LaTeX 版本中得到解决,但现在您必须自己更改执行顺序,方法是为代码添加标签并使用规则来更改顺序:
\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}
答案2
您也可以使用保存箱,但您不能使用,[remember picture]
因为每个\usebox
都会将相同的内容添加pgfid
到辅助文件中。另一方面,\put(0pt,0pt)
对应于(current page.north west)
和\put(\paperwidth,-\paperheight)
等(current page.south east)
,因此您实际上不需要它。您也不会得到辅助文件延迟。
\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}