Background: For each class I usually have a TeX file containing both stuff for me and stuff I distribute to the students.
Sometimes I give out only a short text or a couple of exercises, so to save paper I save the content in a box, print the same box three or four times on a page, and then I cut the printed page. However, as soon as the environment contains some counters and labels I'll get warnings.
A minimal code showing the issue is
\documentclass{article}
\usepackage{hyperref}
\newcounter{foo}
\newcommand*{\printmethreetimes}[1]{%
\setbox0=\vbox{#1}%
\copy0 \copy0 \copy0
}
\begin{document}
\printmethreetimes{\refstepcounter{foo}\label{baz}\arabic{foo}) something something dark side}
\clearpage
As we see in \ref{baz}
\end{document}
which clearly complains with LaTeX Warning: Label `baz' multiply defined and pdfTeX warning (ext4): destination with the same identifier (name{foo.1}) has been already used.
My current workaround is therefore to print the box once, then deactivate hyperref and \label, save the box again and print it twice more, thereby using the \savecounters@ and \restoreconters@ macros from amsmath.
\documentclass{article}
\usepackage{amsmath,hyperref}
\newcounter{foo}
\makeatletter
\newcommand*{\printmethreetimes}[1]{%
\savecounters@
\vbox{#1}%
\restorecounters@
\setbox0=\vbox{\let\label@gobble\begin{NoHyper}#1\end{NoHyper}}%
\copy0 \copy0
}
\makeatother
\begin{document}
\printmethreetimes{\refstepcounter{foo}\label{baz}\arabic{foo}) something something dark side}
\clearpage
As we see in \ref{baz}
\end{document}
This seems to work, until I have a numbered equation in it, which reminds me that I need also to add \let\label@in@display\@gobble... and someday something else will break.
Maybe I'm missing the forest for the trees, but is there another way to print a saved box without all these problems? Some \protected@copy would be nice :-)
pgfmorepagespackage has layouts to repeat the same page multiple times – samcarter_is_at_topanswers.xyz Nov 20 '23 at 17:32hyperreftomorrow, but when next year I'll look into it and ask myself "can I use the same stuff of last year?" it won't be a printed sheet I'll be lloking into but the pdf. – campa Nov 20 '23 at 18:25