# defining the same \label twice with multiple levels of nested files

This question is an extension of the already answered defining the same \label twice. I am trying to find a solution which solves the problem described in defining the same \label twice, while also still allowing cross references between documents called into the main document. For example, see the following three nested files and their output:

First File

\documentclass{article}
\usepackage{catchfilebetweentags}

\begin{document}

\section{FIRST FILE}

\subsection{FIRST FILE}\label{arrow}

\subsubsection{FIRST FILE \ref{hey}}

Call the nested files again.

\end{document}


Second File

\documentclass{article}
\usepackage{catchfilebetweentags}

\begin{document}

%<*tag>
\section{SECOND FILE}

\subsection{SECOND FILE}

\subsubsection{SECOND FILE \ref{hey}}

%</tag>

\end{document}


Third File

\documentclass{article}
\usepackage{catchfilebetweentags}

\begin{document}

%<*tag>
\section{THIRD FILE}

\subsection{THIRD FILE \ref{arrow}}

\subsubsection{THIRD FILE}\label{hey}
%</tag>

\end{document}


Which gives the following:

What I am looking for is to maintain the ability to cross reference between files 1 and 2 and 3, which works perfectly fine until I call file 2 a second time, resulting in the references in 1.1.1 and 2.1.1 to be 5.1.1 instead of 3.1.1. As mentioned before the solution in defining the same \label twice solves the issue but not when the files \ref a \label in other files.

• True, my solution assumes \labels and \references are local to the file it's called from. You'd need to be able to provide a means to distinguish between a (say) local and global/absolute \ref. Without that there's no way for (La)TeX to know whether \ref{hey} could refer to something internal, or possibly external to the current file. Don't know whether you're able to edit the different files and use an alternative to \ref, say, \absref...? – Werner Apr 21 '17 at 18:17
• Yeah that's what I was thinking as well. What I had done earlier today after posting this is define a \newcounter{counter}, then define \newcommand{\callcount}{\arabic{counter}}. So in each \ref which I wanted to be "local" I put \ref{\callcount something}, and global \ref s are just written as normal. So as long as I write something along the lines of \addtocounter{counter}{1} before inputting a redundant file the referencing seems to work. As you mentioned there really is no way around having to distinguish between the two label types somehow. – Elijah Apr 21 '17 at 19:06
• Using \callout is exactly what my other answer does automatically, but for all \refs. (La)TeX cannot do all the thinking for you, so you either have to insert some global \refs manually, or insert the local \refs manually, or define some mechanism by which you can discern between local/global \references. – Werner Apr 21 '17 at 19:09