0

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.

brad
  • 517
  • Why don't you use unique labels? Could be easily done by adopting parts of its filename to the label. // Does using package subfiles solve your reference problems? https://ctan.org/pkg/subfiles (This package is a pretty smart approach, in my view.) – MS-SPO May 09 '23 at 08:58
  • @MS-SPO: the master file is a printed book. A new file is a part of the book (a paragraph, a section) which needs to be modified. The modified part needs to stand alone with references which are to be both consistent with existing ones and newly defined if not existing in the original book, because it is distributed as an integration of the book. Moreover, at some time the new, modified text will take the place of the original text in the book, so labels in the new text need that are already defined in the master need to have the same name. I don't know if I am been able to explain myself. – brad May 09 '23 at 10:07
  • @MS-SPO: I don't think the package subfiles suits my needs. To my problem, package xr would have been fine if it had an option to allow the user to use external labels (without issuing a warning) in case of identical label names. But it doesn't. – brad May 09 '23 at 10:11
  • getting the correct values is rather trivial, you only need to to \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

0 Answers0