1

Recently I found out that the background package is deprecated and one should use \AddToHook{shipout/background} (comments in Positioning relative to page in XeLaTeX with package background and 'nested' tikzpictures 'dimension too large') and I have seen an example in this post: Migration background package > \AddToHook macro

However, for my use-case this conversion did not give the exact same results as the code written with background package. I want to keep using relative coordinates with (0,0) as the top-left of the page and (1,1) the bottom-right of the page, as given in: Positioning relative to page in TikZ by romain.bqt4.

Code

\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}

mismatching borders Now, ideally I would like them to patch and I believe it has something do with the \put command. However, if that is not possible, it also causes the problem of the relative coordinate system not working as it is supposed to work. Specifically, changing the logo node to for example (0.5, 0.925) changes the height of the top horizontal part of the border. It even lowers the bar.

\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}}%
}

weird relative coordinates

Question

How do I need to change the code in order to:

  1. have the borders match,
  2. have the relative coordinate system working as expected?
1
  • I have answer your previous question. The method I used will show the boarder as your wanted.
    – Tom
    Apr 25, 2022 at 17:37

2 Answers 2

2

well the background package is not deprecated, but it uses internally tikz and so doesn't work well if you want to add a tikz picture.

Beside this I think you are overcomplicating with all the scope and remember picture.

\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}

enter image description here

5
  • your solution doesn't match, the figure should be at the bottom of the page.
    – Tom
    Apr 25, 2022 at 17:53
  • @Tom the code in the question used (0.5, 0.95) as coordinate and that is at the top. (assuming the 0,0 should be at the bottom left. Apr 25, 2022 at 17:55
  • He wanna a relative positioning as a ratio of the page. Because he use the shaft and scope in tikz, So don't need \put command. The example picture he showing is also have the figure at the bottom
    – Tom
    Apr 25, 2022 at 17:58
  • After the shift=(current page.north west) , (0,0) is not at bottom left anymore. It at 'top left' right now.
    – Tom
    Apr 25, 2022 at 18:04
  • @UlrikeFischer Indeed I orignally wanted (0,0) to be the top-left and (1,1) the bottom-right as that's how I wrote the rest of the code, even though it was contrary to normal cartesian coordinates. If you can change the code such that it does that, I will gladly accept your answer.
    – PLY
    Apr 28, 2022 at 21:33
2

This should be work, if you wanna keep your original strategy. Delete \put(0,-\paperheight) and add overlay in \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}}%
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .