Does hyperref work between two files?

Just to give a full example. I have found xr-hyper quite tricky to get it to work. It seems that xr-hyper must be called before hyperref and both documents must be compiled twice. Below is an example that shows how to get it going.

  1. write the following two tex file, docA.tex and docB.tex
  2. put both files in the same directory, otherwise the path must be specified as part of the xr header.
  3. run pdfLaTeX twice on both filles. First on docA and then on docB and repeat the process

EDIT2: I do not think citations will work accross files (not natbib at least)

docA.tex

\documentclass{article} 
\usepackage{xr-hyper} 
\usepackage{hyperref} 
\externaldocument[B-]{docB}[docB.pdf]% <- full or relative path 
\begin{document}
    This is a test for math.
    \begin{equation}
        E=mc^2 \label{eq:1}
    \end{equation}
    This is a second test for math.
    \begin{equation}
        r = \sqrt{x^2 + y^2} \label{eq:2}
    \end{equation}
    In document B Eq.~~(\ref{B-eq:x}) 
\end{document}

docB.tex

\documentclass{article}
\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument[A-]{docA}[docA.pdf]% <- full or relative path
\begin{document}
  \setcounter{equation}{5}

  As was shown in Eq.~(\ref{A-eq:1}) is it
  ... or in Eq.~(\ref{A-eq:2}) is ...
  \begin{equation}
    \mathrm{e}^{i\pi}-1=0 \label{eq:x}
  \end{equation}
\end{document}

Answer: Yes. hyperref package is enough.

For example, I have two files in the same folder named Lesson5.tex and Supplement5.tex. I have the following link in Lesson5.tex.

The \href{run:./Supplement5.pdf}{supplement to Lesson 5} is available for that purpose.

I have the following line in supplement5.tex.

\href{run:./Lesson5.pdf}{Go Back to Lesson 5} 

With this, I can go back and forth between two documents.


I guess answers to your questions will depend in part on the related question, "what kind(s) of hyperlinks are you contemplating?"

If it's "just" hyperlinked versions of regular cross-references you're looking to create, the xr-hyper package -- a descendant of the xr (short for "cross-ref") package -- will do what you're looking for. The xr-hyper package is part of the overall hyperref suite of packages and style files.

The xr-hyper package requires you to follow some fairly simple rules while creating the labels that are to be cross-referenced. For the sake of discussion, let's assume that the two tex files are named fileA.tex and fileB.tex.

  • Load the xr-hyper package in both preambles, before loading hyperref. In the preamble of fileA.tex you'd also insert the statement \externaldocument{fileB}, and in the preamble of fileB.tex you'd insert \externaldocument{fileA}. (The .tex extension isn't needed.)

  • The labels of all items (sections, equations, figures, tables, etc) that are to be cross-referenced must be unique. (This is really a matter of simple logic rather than of software design, right?) There are two ways to meet this requirement.

    • You could literally have unique labels in both files. For instance, you could label the equations in fileA as eq:A:1, eq:A:2, eq:A:3, etc and all equations in fileB as eq:B:1, eq:B:2, eq:B:3, etc. Ditto for all figures, tables, sections, and any other items to be cross-referenced, in both files. Cross-references to the items would then look like \ref{eq:B:2} in both files.

    • You could specify a generic prefix for all labelled items in both files. To do so, modify the \externaldocument statements as follows: In fileA's preamble, you would state \externaldocument[B-]{fileB}, and in fileB's preamble you would state \externaldocument[A-]{fileA}. To create a cross-reference in fileA to sec:data located in fileB, you'd write \ref{B-sec:data}. To create a cross-reference to this item from somewhere within fileB, you would of course just write \ref{sec:data}. The two file-related prefixes do not literally have to be A- and B-; any prefixes that don't violate TeX's basic rules are OK.

    • Which one of these two methods you wish to employ is up to you. If you've already completed (more or less) one file of the two files, you probably wouldn't want to redo all of its labels; hence, the second method will likely be the better option for you. If you're starting from scratch with both files, take a moment to think which approach will be less time-consuming and/or error-prone. Having a text editor which lets you view both files will be useful.

  • Both files will need to be compiled twice to give LaTeX's cross-referencing mechanism a chance to resolve all issues. (E.g., first compile fileA, then fileB, then fileA again, then fileB again.) Depending on the complexity of your documents, a third round may be required. For instance, if you need to run BibTeX as well, do so on both files after the first round of compilations, and then rerun pdflatex (or whatever TeX format you use) on fileA and fileB twice more.

  • If you also happen to use the cleveref package, be sure to load it after hyperref. (That's one of the few exceptions to the generic recommendation of always loading hyperref last.) Then, compile both files (at least) twice. Remember that if you use cleveref, you mustn't use commas (,) as parts of the labels' names, including the file-related prefixes.