I need to cross reference from the file new.tex to the file master.tex.
I have read the question Cross-referencing between different files and tried the xr package.
It works, but the way it handles identical label names doesn't suit my need.
My problem is that new.tex is a correction of a chapter of master.tex and, at some point, it will be merged into it so in new.tex I need to use both existing labels from master.tex and new ones and the existing ones should point to the value in master.tex.
I tried the following, of which I give a MWE: I copied master.aux into master_original.aux (I will need to use always these values for the labels, even if master.tex changes) which contains just the line
\newlabel{oldlabel}{{99}{99}}
Then, my new.tex is the file
\documentclass{article}
\global\let\oldlabel\label
\gdef\label#1{\labelxx{#1}}
\gdef\labelxx#1{\@ifundefined{\csname #1 \endcsname}{\oldlabel{#1}}{}}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\input{master_original.aux}
\begin{document}
\begin{theorem}
This is the definition of the a new label
\label{newlabel}
\end{theorem}
\begin{theorem}
This is the definition of the old label
\label{oldlabel}
\end{theorem}
The new label points to \ref{newlabel}
The old label to \ref{oldlabel}
\end{document}
My intention is to modify the definition of \label in such a way that when creating a new label, say \label{formula} if \formula is already defined in the master_original.aux then it does nothing, otherwise, it defines it.
\AtBeginDocument{\newlabel{oldlabel}{{99}{99}}}. The main problem is that this confuses the multiply label and rerun tests. – Ulrike Fischer May 09 '23 at 12:59