×
Clique aqui e veja a página ShareLaTeX em Inglês

[edit] ShareLaTeX guides

[edit] LaTeX Basics

[edit] Mathematics

[edit] Figures and tables

[edit] References and Citations

[edit] Languages

[edit] Document structure

[edit] Formatting

[edit] Fonts

[edit] Presentations

[edit] Commands

[edit] Field specific

[edit] 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.

  Open an example in ShareLaTeX

Contents

[edit] Loading the xr package

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.

[edit] Add some additional code to 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
\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.

[edit] 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.

[edit] 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 \"$_[0]\"" );
    }
}

[edit] 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}.

  Open an example in ShareLaTeX

% 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].