# Wrong reference using \input twice for the same file

I have a short piece of text with an image that I need to use more than once in my document. The rational solution I came up with is to put the text in a file and call the file when needed, something like this:

\documentclass[a4paper,twoside]{report}

\usepackage{lipsum}
\usepackage[pdftex]{graphicx}

\begin{document}

\chapter{First}
\lipsum[1]
\input{test}

\chapter{Second}
\lipsum[1]
\input{test}

\end{document}


where in the file test.tex there is the reusable piece:

The image id is: \ref{image}

\begin{figure}[h!]
\centering
\includegraphics[scale=0.9]{randomimage}
\caption{A random caption.}
\label{image}
\end{figure}


Problem is: the first \ref is wrong, as can be seen in the following image:

Now, I know this could be solved copying and pasting the text any time I need it... but it has obvious shortcomings and I would like to avoid it

• Do you really want to include an identical image and caption but with different numbers each time? Copy and paste would have the same result as using \input. the point is you have used the same label for different figures and that is an error (for which you will get a warning in the log file) – David Carlisle Sep 5 '12 at 14:25
• @DavidCarlisle Yes I figured that it was a problem of labels; I need to put it twice (three times maybe) exactly in the same way, and given that I will have to modify it a LOT in the next weeks, I figured that using \input I would have save time and possible errors... – scannerdarkly Sep 5 '12 at 14:29
• You have the same label defined multiple times: you have to supply a fresh one or generate a different one for each input. So it depends whether you need to reference the pictures or not in the text. – egreg Sep 5 '12 at 14:29
• @egreg Yep I need to reference it... I sense that you are suggesting the copy and paste methodology :D – scannerdarkly Sep 5 '12 at 14:31
• @scannerdarkly Not at all. I was thinking to something like David's answer, and the information was needed. – egreg Sep 5 '12 at 14:40

You need to give a different label to each instance. The easiest way is to use a macro rather than an input file and supply a symbolic name each time:

\documentclass[a4paper,twoside]{report}

\usepackage{lipsum}
\usepackage[demo]{graphicx}

\newcommand\fig[1]{%
The image id is: \ref{#1}

\begin{figure}[h!]
\centering
\includegraphics[viewport=0 0 100 100]{randomimage}
\caption{A random caption.}
\label{#1}
\end{figure}}

\begin{document}

\chapter{First}
\lipsum[1]
\fig{test}

\chapter{Second}
\lipsum[1]
\fig{test2}

\end{document}


You can add a macro prefix before the label name that is redefined for each \input. The file test.tex with the image. It uses \prefix image as label name. If \prefix is not defined, the label remains image.

\providecommand*{\prefix}{}
The image id is: \ref{\prefix image}

\begin{figure}[h!]
\centering
\includegraphics[scale=0.9]{randomimage}
\caption{A random caption.}
\label{\prefix image}
\end{figure}


The main file that redefines the prefix. Now the two labels can be referenced as first:image and second:image:

\documentclass[a4paper,twoside]{report}

\usepackage{lipsum}
\usepackage{graphicx}

\newcommand*{\prefix}{}

\begin{document}

Images \ref{first:image} and \ref{second:image}.

\chapter{First}
\lipsum[1]
\renewcommand*{\prefix}{first:}
\input{test}

\chapter{Second}
\lipsum[1]
\renewcommand*{\prefix}{second:}
\input{test}

\end{document}