# Multiply defined labels with \usebox

I want to duplicate some content without re-defining its labels. For example,

\newbox{\foobox}
\sbox{\foobox}{\vbox{\section{Foo}\label{sec:foo}}
\usebox{\foobox}
\usebox{\foobox}


will emit a warning, because sec:foo is defined more than once. Is there any way to prevent the second \usebox from writing to the aux file?

-
You basically can't, with this approach. When the \vbox is built, the \label command is translated into a whatsit that contains a \write instruction and it can't be disabled any more, because the vertical list is already packaged. –  egreg Feb 6 '14 at 18:55
That's quite unfortunate. Is there any way to duplicate content without affecting what is to be typeset? My original intent was to typeset a bunch of labelled theorems multiple times. With a plain macro instead of a box, the theorem numbering counters (of course) were also incremented the second time, which I do not want. –  Kristóf Marussy Feb 6 '14 at 19:57
There are two problems. (1) Avoid \label doing a \write in the later usages: this might be solved by absorbing the text as a macro replacement text; (2) “freezing” all counters to their current value. Could you be more specific about the problem and what counters you need to be frozen? –  egreg Feb 6 '14 at 20:39

I assume, you want to have the first \label intact and to disable the others. Then the following uses a trick:

1. The box is created with two commands \BeginIgnoreAux{<id>} and \EndIgnoreAux before and after the original contents. These macros write markers into the .aux file with the result, the label stuff gets in between.
2. If the .aux file is read, then the label stuff is executed as usual the first time. If \BeginIgnoreAux{<id>} is called more then once with the same <id>, then the label stuff is ignored. This avoids duplicate labels.
3. LaTeX reads the .aux file twice. At the end of the document the .toc files are written, for example. Therefore Macro \AuxResetIgnoreStuff clears the <id>s.
\documentclass{article}

\makeatletter
\global\let\AuxResetIgnoreStuff\@empty
\usepackage{auxhook}

% Macros inside the .aux' file
\newcommand*{\AuxBeginIgnore}[1]{%
\@ifundefined{ignore@#1}{%
\global\expandafter\let\csname ignore@#1\endcsname\@empty
\expandafter{%
\expandafter\global\expandafter\let\csname ignore@#1\endcsname\relax
}%
}\AuxSkip
}
\def\AuxSkip#1\AuxEndIgnore{}
\let\AuxEndIgnore\relax

% User commands
\newcommand*{\BeginIgnoreAux}[1]{%
\protected@write\@auxout{}{%
\string\AuxBeginIgnore{#1}%
}%
}
\newcommand*{\EndIgnoreAux}[1]{%
\protected@write\@auxout{}{%
\string\AuxEndIgnore
}%
}
\makeatother

\begin{document}
\tableofcontents
\newbox{\foobox}
\sbox{\foobox}{%
\BeginIgnoreAux{foo}%
\vbox{\section{Foo}\label{sec:foo}}%
\EndIgnoreAux
}
\noindent
\usebox{\foobox}
\usebox{\foobox}

\noindent
Reference to the first label: \ref{sec:foo}.
\end{document}


The .aux file contains:

\relax
\AuxResetIgnoreStuff
\AuxBeginIgnore{foo}
\@writefile{toc}{\contentsline {section}{\numberline {1}Foo}{1}}
\newlabel{sec:foo}{{1}{1}}
\AuxEndIgnore
\AuxBeginIgnore{foo}
\@writefile{toc}{\contentsline {section}{\numberline {1}Foo}{1}}
\newlabel{sec:foo}{{1}{1}}
\AuxEndIgnore


And the .toc file:

\contentsline {section}{\numberline {1}Foo}{1}
`
-
Thanks! This works like a charm. :) –  Kristóf Marussy Feb 7 '14 at 10:33