2

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:

The wrong ref

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

5
  • 1
    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) Sep 5, 2012 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... Sep 5, 2012 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, 2012 at 14:29
  • @egreg Yep I need to reference it... I sense that you are suggesting the copy and paste methodology :D Sep 5, 2012 at 14:31
  • @scannerdarkly Not at all. I was thinking to something like David's answer, and the information was needed.
    – egreg
    Sep 5, 2012 at 14:40

2 Answers 2

5

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}
2

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .