2

I'm creating a document (main) in which sections of other documents (others) are often called in. Often the same section of a document (others) is called in at different locations of (main). The problem I have is when using \label and \ref for cross referencing, the \label is rewritten each time the (others) section is called again into (main) because the same \label command occurs in a different (further along) section. Is there a way around this?

For example, the code below gives the following image, which is what I don't want:

\documentclass{article}
\usepackage{catchfilebetweentags}

\begin{document}

Hello there.

\ExecuteMetaData[example2]{tag}

\ExecuteMetaData[example2]{tag}

Hello there. 

\end{document}

Where the other file, example2 is:

\documentclass{article}
\usepackage{catchfilebetweentags}
\begin{document}

This is some text to fill space. This is some text to fill space.

%<*tag>
\section{Hello}
\subsection{Hello There}\label{1}
\subsubsection{Hello There, as seen in \ref{1}.}
%</tag>

\end{document}

enter image description here

Where this is what I do want:

enter image description here

I can't change the label each time because I want to just recall the same document. One solution I thought of would be to have have duplicates of the files being imported with different labels in each of them but I would rather not do that (updating them all would be tedious).

  • By "imported" do you mean you copy-and-paste from the file? Or do you use \input or \include? – Werner Apr 20 '17 at 22:49
  • Sorry I will add that to the question. I wasn't using \input or \include but I am using the catchfilebetweentags package to just get specific sections – Elijah Apr 20 '17 at 22:53
  • Good. Include all the relevant details so we can copy-and-paste-and-compile and replicate your current (problematic) behaviour. – Werner Apr 20 '17 at 22:54
  • @Elijah: Off-topic: labelling with numbers only is not really the best approach – user31729 Apr 20 '17 at 22:54
  • Are you using hyperref as well? – Werner Apr 20 '17 at 22:58
2

The following code provides \newdocumentimport that adds a prefix to each \label and \ref (and \pageref) that is sequential. That way you call \newdocumentimport just before you import a new document and all \labels and \references will have the added prefix. You can also force a fixed prefix using \setfixedprefix{<prefix>}.

enter image description here

\documentclass{article}

% Counter to keep track of new document imports
\newcounter{newdocumentimport}

\AtBeginDocument{
  \let\oldlabel\label% Store \label in \oldlabel
  \let\oldref\ref% Store \ref in \oldref
  \let\oldpageref\pageref% Store \pageref in \oldpageref
}

% Establish a new document import
\newcommand{\newdocumentimport}{%
  \stepcounter{newdocumentimport}%
  \renewcommand{\label}[1]{\oldlabel{\thenewdocumentimport-##1}}%
  \renewcommand{\ref}[1]{\oldref{\thenewdocumentimport-##1}}%
  \renewcommand{\pageref}[1]{\oldpageref{\thenewdocumentimport-##1}}%
}
\newcommand{\setfixedprefix}[1]{%
  \renewcommand{\label}[1]{\oldlabel{#1-##1}}%
  \renewcommand{\ref}[1]{\oldref{#1-##1}}%
  \renewcommand{\pageref}[1]{\oldpageref{#1-##1}}%
}

\begin{document}

\section{Hello}
\subsection{Hello There}\label{1}
\subsubsection{Hey There, as seen in \ref{1}.}

%same document imported again
\newdocumentimport

\section{Hello}
\subsection{Hello There}\label{1}
\subsubsection{Hey There, as seen in \ref{1}.}

\end{document}

The above option should also work with hyperref, since the storage of \label, \ref and \pageref is delayed until \AtBeginDocument.


For an automated way of handling all files imported using catchfilebetweentags, you can use the following in your preamble:

\usepackage{catchfilebetweentags}
\let\oldExecuteMetaData\ExecuteMetaData
\renewcommand{\ExecuteMetaData}{\newdocumentimport\oldExecuteMetaData}

This will prepend \newdocumentimport before every \ExecuteMetaData, allowing your code to remain as-is without having to change anything.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.