# storebox not working as expected since texlive 2020

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}{%
][%
\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?

• why do you use a page hook to store the box? Simply storing in the document looks much easier. – Ulrike Fischer Jan 17 at 10:41
• 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 at 10:47
• 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 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 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 at 11:16

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}

• 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 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 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 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 at 14:43
• I've opened a ticket for the {} bug: sourceforge.net/p/storebox/tickets/1 – egreg Jan 17 at 15:21