# multiple LaTeX documents citing each other's internal references

Suppose someone wants to create multiple LaTeX documents capable of referencing the internal references of the other documents. Is there a way to do this that avoids hard-coding the label of the other document?

As an example, suppose an instructor has two sets of lecture notes: A.pdf and B.pdf corresponding to two classes they teach "A" and "B". Each time they teach class A they update A.pdf, possibly adding new items with labels (e.g., Lemma 1.2.3 might change to Lemma 1.2.5). Then they should be able to go over to B.pdf, which might reference Lemma 1.2.3 in A.pdf, recompile B.pdf and have the original reference to Lemma 1.2.3 now say Lemma 1.2.5. They shouldn't have to actually write "1.2.3" anywhere in the .tex source and then change it to "1.2.5" before recompiling B.pdf. This type of hard-coding is fine when you are referencing an already-published document, but is there a natural way to avoid it in this situation?

You can use the xr package to define references to an external document. So, if you have two documents A.tex and B.tex in document A you can use

\externaldocument{B}


to load the references in B (\ref and \pageref). Please notice that, then, \label{xxx} can refer to a reference in document A or document B. You can include multiple \externaldocument calls.

Here is a complete set of two MWEs:

File A.tex (the \setcounter is used just to have different labels).

\documentclass{article}

\usepackage{xr}

\newtheorem{lemma}{Lemma}
\setcounter{lemma}{100}
\externaldocument{B}

\begin{document}

\begin{lemma}
\label{a:lemma:first}
this is a lemma
\end{lemma}

This is a reference to the lemma in this document \ref{a:lemma:first}

This is a reference to the lemma in the related document \ref{b:lemma:first}
\end{document}


similarly, file B.tex

\documentclass{article}

\usepackage{xr}

\newtheorem{lemma}{Lemma}

\externaldocument{A}

\begin{document}

\begin{lemma}
\label{b:lemma:first}
this is a lemma
\end{lemma}

This is a reference to the lemma in this document \ref{b:lemma:first}

This is a reference to the lemma in the related document \ref{a:lemma:first}
\end{document}


yielding