##  Class files

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 ShareLaTeX, there are some extra steps required which are outlined below.

## Contents

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.

We now need to add some additional helper' code to the preamble of File1.tex as shown below:

% In your preamble

\makeatletter
\typeout{(#1)}
\IfFileExists{#1}{}{\typeout{No file #1.}}
}
\makeatother

\newcommand*{\myexternaldocument}[1]{%
\externaldocument{#1}%
}

This simply creates a new command of \myexternaldocument{...} which allows you to specify the document whose labels you would like to reference.

# Specifying the external document

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.

# Creating the latexmkrc file

The next step is to create a latexmkrc file as shown below:

• In your project editor window, click on Add file' on the top of the project sidebar.
• Select Blank file', and name the file latexmkrc.
• Add the following line to the file latexmkrc:

add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );

sub makeexternaldocument {
if (!($root_filename eq$_[0]))
{
system( "latexmk -pdf \"\$_[0]\"" );
}
}

# Final Project

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}`

Source: [1].