最近我发现这个background
包已经被弃用了,应该使用\AddToHook{shipout/background}
(在 XeLaTeX 中使用包背景和“嵌套”tikzpictures 进行相对于页面的定位,尺寸太大)我在这篇文章中看到了一个例子:迁移后台包 > \AddToHook 宏
但是,对于我的用例,此转换并未提供与background
使用包编写的代码完全相同的结果。我想继续使用相对坐标作为(0,0)
页面的左上角和(1,1)
页面的右下角,如下所示:在 TikZ 中相对于页面的定位作者:romain.bqt4。
代码
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{background}
\usepackage{lipsum}
\usepackage[centering, margin = 2cm, bottom = 2.5cm, top = 1.5cm, showframe]{geometry}
\usetikzlibrary{shapes.misc, positioning, calc}
\AddToHook{shipout/background}
{%
\put(1cm,-\paperheight)
{\begin{tikzpicture}[remember picture,shift=(current page.north west)] % for XeLaTeX
\begin{scope}[x={(current page.north east)},y={(current page.south west)}]
\node (logo) at (0.5, 0.95) {\piktjur};
\draw[rounded corners=10pt, line width = 2pt, red] (logo) -| (0.05, 0.025) -- (0.95, 0.025) |- (logo);
\end{scope}
\end{tikzpicture}}%
}
\SetBgAngle{0}
\SetBgScale{1}
\SetBgOpacity{1}
\SetBgContents{
\begin{tikzpicture}[remember picture,shift=(current page.north west)] % for XeLaTeX
\begin{scope}[x={(current page.north east)},y={(current page.south west)}]
\node (logo) at (0.5, 0.95) {\piktjur};
\draw[rounded corners=10pt, line width = 2pt, blue] (logo) -| (0.05, 0.025) -- (0.95, 0.025) |- (logo);
\end{scope}
\end{tikzpicture}
}
\newcommand*{\piktjur}{\includegraphics[height = 2cm]{example-image.pdf}}
\pagestyle{empty}
\begin{document}
hello
\end{document}
现在,理想情况下我希望他们能修补,我相信这与\put
命令有关。但是,如果这不可能,也会导致相对坐标系无法正常工作的问题。具体来说,将徽标节点更改为例如(0.5, 0.925)
会更改边框顶部水平部分的高度。它甚至降低酒吧。
\AddToHook{shipout/background}
{%
\put(1cm,-\paperheight)
{\begin{tikzpicture}[remember picture,shift=(current page.north west)] % for XeLaTeX
\begin{scope}[x={(current page.north east)},y={(current page.south west)}]
\node (logo) at (0.5, 0.925) {\piktjur};
\draw[rounded corners=10pt, line width = 2pt, red] (logo) -| (0.05, 0.025) -- (0.95, 0.025) |- (logo);
\end{scope}
\end{tikzpicture}}%
}
问题
我需要如何更改代码才能:
- 使边界匹配,
- 相对坐标系是否按预期工作?
答案1
虽然背景包并未弃用,但它内部使用了 tikz,因此如果您想添加 tikz 图片,它就无法很好地工作。
除此之外,我认为你对所有范围和记忆图片都过于复杂了。
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage[centering, margin = 2cm, bottom = 2.5cm, top = 1.5cm, showframe]{geometry}
\usetikzlibrary{shapes.misc, positioning, calc}
\AddToHook{shipout/background}
{%
\put(0,-\paperheight)
{\begin{tikzpicture}[x={(\paperwidth,0)},y={(0,\paperheight)}] %
\path[use as bounding box](0,0) rectangle (1,1);
\node (logo) at (0.5, 0.95) {\piktjur};
\draw[rounded corners=10pt, line width = 2pt, red] (logo) -| (0.05, 0.025) -- (0.95, 0.025) |- (logo);
\end{tikzpicture}}%
}
\newcommand*{\piktjur}{\includegraphics[height = 2cm]{example-image.pdf}}
\pagestyle{empty}
\begin{document}
hello
\end{document}
答案2
如果您想保留原始策略,这应该可行。删除\put(0,-\paperheight)
并overlay
添加\begin{tikzpicture}[remember picture,overlay,shift=(current page.north west)]
:
\AddToHook{shipout/background}
{%
{\begin{tikzpicture}[remember picture,overlay,shift=(current page.north west)] % for XeLaTeX
\begin{scope}[x={(current page.north east)},y={(current page.south west)}]
\node (logo) at (0.5, 0.925) {\piktjur};
\draw[rounded corners=10pt, line width = 2pt, red] (logo) -| (0.05, 0.025) -- (0.95, 0.025) |- (logo);
\end{scope}
\end{tikzpicture}}%
}