3

I'm trying to add some ornaments to every page using pgfornament, in the way it's described in that package's documentation. I ran into some problems there which -- I think -- relate to eso-pic and \AddToShipoutPicture; some things being \put on the page don't show up where they should. Here's a fairly minimal example:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{eso-pic}
\usepackage{calc}
\usepackage{lipsum}
\makeatletter
\AddToShipoutPicture{%
\begingroup
\setlength{\@tempdima}{2mm}%
\setlength{\@tempdimb}{\paperwidth-\@tempdima-2cm}%
\setlength{\@tempdimc}{\paperheight-\@tempdima}%
\put(\LenToUnit{\@tempdima},\LenToUnit{\@tempdimc}){OK} % upper left
\put(\LenToUnit{\@tempdima},\LenToUnit{\@tempdima}){OK} % lower left
\put(\LenToUnit{\@tempdimb},\LenToUnit{\@tempdimc}){wrong} % upper right
\put(\LenToUnit{\@tempdimb},\LenToUnit{\@tempdima}){OK} % lower right
\endgroup
}
\makeatother
\begin{document}
\lipsum[1]
\end{document}

This produces the following:

enter image description here

So as you can see, one of the added labels is in the wrong spot, and I have absolutely no idea why. The same thing also happens with e.g. the code from Gonzalo's answer to this question (which apparently worked for him when he posted it).

Would appreciate any help, I'm not even sure how to begin debugging this. eso-pic is current, there's nothing unusual in the log file.

chsk
  • 3,667
  • 1
    Try out commenting the two first \put and see what happens. Additionally note that if you have a modern latex instalation (after october 2020), then the eso-pic functionally is build into the new hook feature, here as a background hook (which is actually how eso-pic is implemented these days) – daleif Feb 03 '22 at 09:23
  • Thanks for the pointer --- turns out that if you comment out the first two, the third works. Why's that? (As for updates, I'm probably a bit behind; I use LaTeX too much for "production work" to risk breakages most of the time.) – chsk Feb 03 '22 at 09:29
  • 1
    I just asked the chat the same. I do not understand it, other whan \@tempdimc getting overwriten. Note that you don't need calc you can just use \dimexpr length calc (sometimes ended by a \relax) – daleif Feb 03 '22 at 09:33
  • 1
    also you do not need \LenToUnityiou can use lengths directly in recent latex (presumably you have a new enough latex, as otherwise you would not have an issue with@tempdimc`) – David Carlisle Feb 03 '22 at 09:36
  • Thanks to you both for the pointers. FWIW the use of \@tempdimc (as well as pretty much everything else) is from the pgfornament documentation, so perhaps I should raise an issue there to have the example code fixed. – chsk Feb 03 '22 at 10:18
  • @chsk that might be an idea. As David mentioned relying on the value of a temp var that other macros might also use under the hood is not a good idea. – daleif Feb 04 '22 at 11:57

2 Answers2

6

It is never safe to assume scratch registers such as \@tempdimc are "safe" to use across non local scopes. It may work but (in newer latex implementations) it may get used for some other purpose so not have the value you set.

If you allocate a register then it works

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{eso-pic}
\usepackage{calc}
\newlength\zzz
\usepackage{lipsum}
\makeatletter
\AddToShipoutPicture{%
\begingroup
\setlength{\@tempdima}{2mm}%
\setlength{\@tempdimb}{\paperwidth-\@tempdima-2cm}%
\setlength{\zzz}{\paperheight-\@tempdima}%
\put(\LenToUnit{\@tempdima},\LenToUnit{\zzz}){OK} % upper left
\put(\LenToUnit{\@tempdima},\LenToUnit{\@tempdima}){OK} % lower left
\put(\LenToUnit{\@tempdimb},\LenToUnit{\zzz}){wrong} % upper right
\put(\LenToUnit{\@tempdimb},\LenToUnit{\@tempdima}){OK} % lower right
\endgroup
}
\makeatother
\begin{document}
\lipsum[1]
\end{document}

Note the change that introduced internal \@tempdimc usage was to allow lengths to be used, so you do not need \LenToUnit

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{eso-pic}
\usepackage{calc}
\newlength\zzz
\usepackage{lipsum}
\makeatletter
\AddToShipoutPicture{%
\begingroup
\setlength{\@tempdima}{2mm}%
\setlength{\@tempdimb}{\paperwidth-\@tempdima-2cm}%
\setlength{\zzz}{\paperheight-\@tempdima}%
\put(\@tempdima,\zzz){OK} % upper left
\put(\@tempdima,\@tempdima){OK} % lower left
\put(\@tempdimb,\zzz){wrong} % upper right
\put(\@tempdimb,\@tempdima){OK} % lower right
\endgroup
}
\makeatother
\begin{document}
\lipsum[1]
\end{document}

Although you do not need any register assignments (or calc package) here at all as you can use dimension expressions in all picture mode contexts.

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{eso-pic}
\usepackage{lipsum}

\AddToShipoutPicture{% \put(2mm,\paperheight-2mm){OK} % upper left \put(2mm,2mm){OK} % lower left \put(\paperwidth-2mm-2cm,\paperheight-2mm){wrong} % upper right \put(\paperwidth-2mm-2cm,2mm){OK} % lower right } \begin{document} \lipsum[1] \end{document}

David Carlisle
  • 757,742
5

Too long for a comment. Just wanted to show this without using eso-pic using the background hook in the LaTeX format after october 2020:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{lipsum}
\AddToHook{shipout/background}{%
  \put(2mm,-4mm){OK} % upper left
  \put(2mm,4mm-\paperheight){OK} % lower left
  \put(\paperwidth-12mm,-4mm){wrong} % upper right
  \put(\paperwidth-6mm,4mm-\paperheight){OK} % lower right
}
\begin{document}
\lipsum[1]
\end{document}

Though for more consistent placing on the right side I'd probably typeset those in \llap{...} such that the right side of the inserted text is at the given coordinate.

daleif
  • 54,450