The xr
package is used for cross-referencing across multiple independent document. For example, you would use the xr
package if you had two separate files in a project, File1.tex
and File2.tex
, and you would like to have a reference in File1.tex
to something labelled in File2.tex
, without including File2.tex
in File1.tex
.
To do this in Overleaf, there are some extra steps required which are outlined below.
The first thing required is to simply load the xr package in the main file you are working on. In this case, this simply means we include
\usepackage{xr}
in the preamble of File1.tex
.
If you're using the hyperref
package in your main file (i.e. File1.tex
), then load xr-hyper
instead of xr
in File1.tex
before hyperref
. You'll also need to load the hyperref
package in the external document (i.e. File2.tex
).
We now need to add some additional `helper' code to the preamble of File1.tex
as shown below:
% In your preamble
\makeatletter
\newcommand*{\addFileDependency}[1]{% argument=file name and extension
\typeout{(#1)}
\@addtofilelist{#1}
\IfFileExists{#1}{}{\typeout{No file #1.}}
}
\makeatother
\newcommand*{\myexternaldocument}[1]{%
\externaldocument{#1}%
\addFileDependency{#1.tex}%
\addFileDependency{#1.aux}%
}
This simply creates a new command of \myexternaldocument{...}
which allows you to specify the document whose labels you would like to reference.
The next step is to specify the external document whose labels you would like to reference. This is the only part of the code you will have to change yourself depending on the name of the file.
% In your preamble
\myexternaldocument{File2}
Here, File2
can be replaced by whatever file you would like to reference the labels of.
The next step is to create a latexmkrc
file as shown below:
latexmkrc
.latexmkrc
:
add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );
sub makeexternaldocument {
if (!($root_filename eq $_[0]))
{
system( "latexmk -cd -pdf \"$_[0]\"" );
}
}
This compiles the external document File2
with the PDFLaTeX engine, and the auxiliary files produced are saved in the cache to be accessed and referenced as File1
is compiled. If your external file needs to be compiled with a different engine, the appropriate command should be commented out from the following (by removing the preceeding #
; here, PDFLaTeX is used again):
add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );
sub makeexternaldocument {
if (!($root_filename eq $_[0]))
{
# FOR PDFLATEX
system( "latexmk -cd -pdf \"$_[0]\"" );
# FOR LATEX+DVIPDF
# system( "latexmk -cd \"$_[0]\"" );
# FOR XELATEX
# system( "latexmk -cd -xelatex \"$_[0]\"" );
# FOR LUALATEX
# system( "latexmk -cd -lualatex \"$_[0]\"" );
}
}
Once all the steps above have been completed, you are ready to start referencing across different documents. This is done in the exact same way as normal. If you have a something in File2.tex
labelled with \label{file2:introduction}
, then you can simply reference this in File1.tex
by writing \ref{file2:introduction}
.
% In File1.tex
We can reference the image \ref{picture} of File2.tex.
% In File2.tex
\begin{figure}
\centering
\includegraphics{image.PNG}
\caption{Caption}
\label{picture}
\end{figure}