5

I have to include page-specific content multiple times per page (DM codes to be specific) in a LaTeX document. To that end I use a combination of storebox and everypage such that content is typeset once and referenced multiple times in the resulting PDF:

\documentclass[a4paper]{article}

\usepackage[pagestyles,extramarks]{titlesec} \usepackage{everypage} \usepackage{storebox} \AtBeginDocument{{}} % due to https://tex.stackexchange.com/a/141540

\newpagestyle{mypagestyle}{% \sethead[% ][% \usestorebox{\mybox}% ][% ]{% }{% \usestorebox{\mybox}% }{% }% \setfoot[% ][% \usestorebox{\mybox}% ][% ]{% }{% \usestorebox{\mybox}% }{% }% }

\pagestyle{mypagestyle}

\AddEverypageHook{\storebox{\mybox}{some page-specific stuff repeated multiple times on that page}}

\begin{document}

Page 1

\clearpage

Page 2

\end{document}

This works fine for any version prior to 2020. However, texlive 2020 introduced native hooks, making the everypage package obsolete. Since that version, the approach above results in:

! Undefined control sequence.
<argument> \pdfrefxform \mybox

Using the new hooks directly, i.e., \AddToHook{shipout/before}{stuff}, gives the same error. Using xsavebox instead of storebox would work fine, but xsavebox is way slower such that it is not an alternative.

Does anyone has an idea how to make storebox work with texlive 2020?

moepi
  • 51
  • why do you use a page hook to store the box? Simply storing in the document looks much easier. – Ulrike Fischer Jan 17 '21 at 10:41
  • 1
    Use \usepackage{everypage-1x} instead. The used shipout/background hook to emulate everypage is too late for your code to have any effect (and is limited in a group scope). It would be the correct code for placing additional information on a page, but not for altering actual contents of the page box. – Skillmon Jan 17 '21 at 10:47
  • 1
    Also, with the new hook mechanism, you should use \AtBeginDocument[storebox]{{}} to fix the storebox bug, instead of just doing \AtBeginDocument{{}} after loading the package. – Skillmon Jan 17 '21 at 10:51
  • but if the package has a simple bug of missing {} and hasn't been fixed since 2011 you might think about asking it be declared unmaintained and fixing it on ctan, – David Carlisle Jan 17 '21 at 11:03
  • @Skillmon: that works indeed. Question is now how to make it work in the future as usage of everypage-1x is discouraged. Is there an easy way to hook into early enough with the new hook mechanism? – moepi Jan 17 '21 at 11:16
  • well you are claiming that xsavebox work, and if this is true then no hook is needed and savebox should be fixed. So show a working example with xsavebox. (And better answer the question why you actually need an everypage hook here. In your example it doesn't make sense). – Ulrike Fischer Jan 17 '21 at 12:30
  • @UlrikeFischer please read my comments regarding everypage-1x above. If you want a working example with xsavebox, replace storebox by that (savebox was never mentioned by me here, although I know about the connection between both packages). Why I need an everypage hook (or similar): it has to be done within a pagestyle. I cannot fire that macros by hand on every page. Please consider my example code in my original question for reference. – moepi Jan 17 '21 at 13:22
  • sorry I meant storebox to savebox, but why do you need to fire on every page? Why can't you store the box at the begin of the document? The main point of xform is to reuse boxes, it doesn't make much sense to use it for boxes that needs updates. Or alternatively why don't you fire it in the page style code? – Ulrike Fischer Jan 17 '21 at 13:33
  • @UlrikeFischer because it has to be redefined on every page, as you would know if you have read my initial question. The content is page-specific. I inlcude datamatrix codes (multiple per page) that are specific to each page. And for that reason I want it to be stored once and referenced multiple times. – moepi Jan 17 '21 at 13:38

1 Answers1

4

The following allows to store a new box on every page and to reuse it on various place in the footer or header. The pdf contains two xobjects, one for every page. Something similar is probably possible with titlesec but I don't know the package very well.

The patch of the internal storebox command is needed as it doesn't store the reference to the xform global. This and the \AtBeginDocument bug should be reported to the author.

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{storebox}
\makeatletter
%make command global
\def\@storebox#1{%
    \begingroup
    \@collectboxto\collectedbox{%
        \storebox@immediate\pdfxform resources {\the\pdfpageresources}\collectedbox
        \endgroup\global\mathchardef#1=\pdflastxform
    }%
}
\makeatother
\pagestyle{fancy}
\fancyhf{}
\newcommand\mybox{} %allocate the name to avoid to overwrite an existing command.
\lhead{\storebox{\mybox}{some page-specific stuff repeated multiple times on that page\thepage/\leftmark}%
       \usestorebox{\mybox}}
\rfoot{\usestorebox{\mybox}}

\begin{document} \section{abc} Page 1

\clearpage \section{cde} Page 2

\end{document}

Ulrike Fischer
  • 327,261
  • That's a very nice example. I don't know whether fancyhdr is an option for me. If we could port that to titlesec, it would be the solution. But ++ – moepi Jan 17 '21 at 14:24
  • it is quite probably somehow possible with titlesec, but I'm not using it, It is not fully compatible with hyperref and difficult to debug. – Ulrike Fischer Jan 17 '21 at 14:30
  • I considered using fancyhdr but, at the moment, I don't see the flexibility of titlesec with respect to head/foot notes and odd/even pages. fancyhdr was, for me, always a bit "on top" ... – moepi Jan 17 '21 at 14:39
  • fancyhdr can handle odd/even pages without problems. I never had a pagestyle I couldn't implement with it. – Ulrike Fischer Jan 17 '21 at 14:43
  • 2
    I've opened a ticket for the {} bug: https://sourceforge.net/p/storebox/tickets/1/ – egreg Jan 17 '21 at 15:21
  • For this, xsavebox could be used too. No patching needed ;) – AlexG Jan 17 '21 at 18:54
  • @AlexG I know (and I would use it) but the OP claimed it is too slow ... – Ulrike Fischer Jan 17 '21 at 19:15
  • I've read this too. It amazes me, since the underlying mechanism --- XForm Object generation --- is the same. – AlexG Jan 17 '21 at 19:34