4

I have a report where it's required to have sections from another report both in the main body as well as in an appendix, and I'm including them by using the \input{some_section} command.

I'm referring to figures in text by using:

Figure~\ref{fig:something}

Which works fine, but I'm getting warnings because the labels are multiply defined, since they occur in two parts of the report.

Is it possible for me to still reference the same image (essentially) without hard-labeling (i.e. typing Figure 1 in stead of Figure~\ref{fig:something}) ?

2
  • 1
    do you mean you are inputting the files twice, or that labels in the input file clash with labels in the main one? Commented Oct 17, 2014 at 20:18
  • I'm inputting the files twice. Commented Oct 17, 2014 at 20:30

2 Answers 2

2

You can add a macro to all the labels in the inputted file and either reset it manually or use \thechapter (for example).

\documentclass{report}
\usepackage{mathtools}

\begin{document}

Equation \eqref{first.1} and equation \eqref{first.2} are copies.

\chapter{Main}
\begin{equation}\label{first.\thechapter} a=b \end{equation}
This references the local equation \eqref{first.\thechapter}.

\chapter{Appendix}
\begin{equation}\label{first.\thechapter} a=b \end{equation}
This references the local equation \eqref{first.\thechapter}.

\end{document}

Come to think of it, instead of making many changes to the inputted file, you could use

\let\oldlabel=\label
\renewcommand{\label}[1]{\oldlabel{#1.\thechapter}}

at the beginning and

\let\label=\oldlabel

at the end.

2

You probably mean that the \label commands should be processed only the first time the file is input.

Start the file to input with

\ifcsname thisfilehasbeeninput\endcsname
   \global\expandafter\let\csname thisfilehasbeeninput\endcsname\relax
\else
   \makeatletter
   \let\latexlabel\label
   \def\label#1{\@bsphack\@esphack}
   \makeatother
\fi

and end it with

\ifcsname thisfilehasbeeninput\endcsname
\else
  \let\label\latexlabel
\fi

Instead of thisfilehasbeeninput you might use the file name; the string should be unique for every file you need to input twice.

In case the file hasn't yet been input, a command will be defined and nothing else will be done. If the file has already been input, that command will be defined and the \label command will be turned into a no-op. At the end, the original meaning of \label is restored.

You must log in to answer this question.

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