# Reuse an image resp. a box in TikZ but have side-effects only once

I have a huge document (a pool of exercise problems) with lots of \input statements (one file per subexercise). In order to not get lost in this mess and easily open files from the PDF, I include a margin note with a link to the included file at every \input and \includegraphics.

Now I started seeing warnings like

LaTeX Warning: Label note.1.1' multiply defined.


I narrowed the issue down to this MFE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{external/mode=list and make,%
external/export=true}

\usepackage{marginnote}
\newcommand{\inclimage}[2][]{%
\marginnote{\texttt{#2}}\includegraphics[#1]{#2}%
}

\begin{document}
\def\icon{\inclimage[width=100pt]{example-image.png}}
\begin{tikzpicture}
\node {\icon};
\end{tikzpicture}
\begin{tikzpicture}
\node {\icon};
\end{tikzpicture}
\end{document}


The document needs two runs to translate, and compilation of the externalized pictures in between. I'm reasonably certain that list and make is not relevant to the problem, but that's how I use it.

Now, in the aux file I find:

\def \dpthimport {\newmarginnote {note.1.1}{{\themn@abspage }{0sp}}}\dpthimport


Remove either TikZ externalization or the margin note business, the warnings vanish. In fact, without externalization the aux file contains only

\newmarginnote{note.1.1}{{2}{10000988sp}}


The use of \dpthimport seems to be related to externalization.

So, the problem seems to be that when translating each picture for itself (due to externalisation), marginnote picks the same label name for each instance of \icon which then conflict when the whole document is compiled (again).

I tried to define the image once and reuse it inside the pictures.

• With save boxes:

\begin{document}
\newsavebox{\IconBox}
\sbox{\IconBox}{\tikz\node{\inclimage[width=100pt]{example-image.png}};}

\begin{tikzpicture}
\node {\usebox{\IconBox}};
\end{tikzpicture}
\begin{tikzpicture}
\node {\usebox{\IconBox}};
\end{tikzpicture}
\end{document}


Same warning, two margin notes (with weird positions).

• With pic from TikZ 3.0:

\begin{document}
\tikzset{%
icon/.pic={\node {\inclimage[width=100pt]{example-image.png}};}
}
\begin{tikzpicture}
\pic {icon};
\end{tikzpicture}
\begin{tikzpicture}
\pic {icon};
\end{tikzpicture}
\end{document}


Same warning, no margin note.

How can I define an image resp. a box in such a way that

• it is expanded only once, i.e. side-effects (such as margin notes) apply only once, and
• it can be reused in a TikZ image?
-

\documentclass{article}

\usepackage{tikz}
\usepackage{marginnote}
\newcommand{\inclimage}[2][]{%
\marginnote{\texttt{#2}}\includegraphics[#1]{#2}%
}
\begin{document}
\newsavebox{\IconBox}
\sbox{\IconBox}{\tikz\node{\inclimage[width=100pt]{example-image.png}};}

\begin{tikzpicture}
\node {\usebox{\IconBox}};
\end{tikzpicture}
\begin{tikzpicture}
\node {\usebox{\IconBox}};
\end{tikzpicture}
\end{document}


produces

LaTeX Warning: There were multiply-defined labels.


As the box has a \write note that gets used twice. \leaders is a TeX promitive intended to repeat a box multiple times, usually for ...... in tables of contents etc, but you can arrange the leaders to only make one copy, as a useful side effect of wrapping a box in leaders is that all writes are disabled.

so

\documentclass{article}

\usepackage{tikz}
\usepackage{marginnote}
\newcommand{\inclimage}[2][]{%
\marginnote{\texttt{#2}}\includegraphics[#1]{#2}%
}

\begin{document}
\newsavebox{\IconBox}
\sbox{\IconBox}{\tikz\node{\inclimage[width=100pt]{example-image.png}};}

\begin{tikzpicture}
\node {\xusebox{\IconBox}};
\end{tikzpicture}
\begin{tikzpicture}
\node {\xusebox{\IconBox}};
\end{tikzpicture}
\end{document}


produces no warning (it takes two runs to get rid of the effects of the aux file if you edit the document).

Using \leaders in this way is generally applicable although in this case the layout is rather strange as marginal notes can not do anything inside a \sbox` (which is a horizontal construct with no linebreaking or margins).

-