60

Can I create a link from one file into another one. So that when the resulting pdf files are in the same directory the link takes me from one to the other? Does another package do that?

Added on 1/20/2012, Reason for the question: I want to submit a document to an organization which wants it to be split into A.pdf for body of paper and B.pdf for the references, C.pdf etc. I assume they will reside in a common directory. They may be viewed individually or coalesced together into a single document.

6 Answers 6

39

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}
5
  • Why would citations need to work across files? If there's a citation in file A to "Smith (2012)", say, won't that reference have to show up in the bibliography of fileA, located (probably) near the end of fileA.pdf? The hyperlink from the citation call-out to the place in the Referencds section will work its normal course.
    – Mico
    Commented Jan 19, 2012 at 11:57
  • @Mico: What I mean by that is that (with xr) you cannot have a complete bibliography in one file and only citations in the other files. Every file will need its own standalone bib.
    – Danie Els
    Commented Jan 19, 2012 at 12:07
  • As you mentioned citation \cite won't work across files as is. Also clicking on a link in A.pdf to an item in B.pdf makes A.pdf to disappear and B.pdf will open in its place. I don't know how to "open another window" as you do by right-clicking a link in a browser. Also in Reader Alt+LeftArrow acts as browser back button, taking you back to where you started; however that function does not seem to work here.
    – Maesumi
    Commented Jan 24, 2012 at 5:09
  • It does not work for me unless I remove the "[docB.pdf]" parts Commented Jan 25, 2016 at 2:32
  • @stackovergio I encountered something similar. But, I discovered the issue was actually that my compiler wasn't actually doing a full re-compile if it detected I hadn't changed the other docs at all. Thus, if I modify the three step procedure here by adding/removing a space from each doc and then re-saving then I don't need to do any separate deleting. Commented Jul 28, 2017 at 15:49
19

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.

3
  • 2
    Is there any explanation on why that run: prefix is needed? In my tests without that prefix it works well in most readers. But the TexLive built-in PDF reader/preview e.g. does not open the file if you prepend the path with run:. Without the prefix, it seems to work in many readers, too.
    – rugk
    Commented Jun 9, 2020 at 16:53
  • Well ... that shows how to link to another file ... what about linking to a theorem or a page on the other file, The answer is rather short and incomplete. Commented Dec 19, 2021 at 23:37
  • Is there a way to open the file like Supplement5.pdf in this case in another window on Adobe Reader?
    – Bman72
    Commented Apr 11, 2023 at 8:50
14

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.

3
  • I will be submitting a proposal paper which at the point of upload is to be broken up into several files, so citations are in the Description while referenced items are in another part the Bibliography. I want to be able to go from citation to the reference as in when all parts are in one single document.
    – Maesumi
    Commented Jan 19, 2012 at 3:52
  • @Maesumi -- your comment contains vital information explaining the reason for your question. please edit your question and add this as a second paragraph. Commented Jan 19, 2012 at 14:20
  • I think that it's worth noting that there is no need to use xr-hyper in the source document at all. However, the manglings that hyperref does (and that xr-hyper expects) to the .aux file mean that you must use hyperref in the source document (if you use it in the target).
    – LSpice
    Commented Aug 18, 2013 at 4:47
8

With hyperref package only: use \hyperdef (to label) + \hyperref (to link):

% file_A.tex
... How can I refer to \hyperdef{categoryname}{labelname}{this text} from file\_B.pdf?

and

% file_B.tex
... Like \hyperref{file_A}{categoryname}{labelname}{this}!

This creates a hyperlink from file_B.pdf (after pdfLaTeX processing) to the correct location in file_A.pdf.

But it is not possible with this technique to retrieve LaTeX-generated numbers from labels in File A (such as eq. no, section no) in order to use them in File B as link text.

For example, if file_A.pdf has an equation labelled with \label{myeq} resulting in the number (1), it will not be possible to show this number in the text of the link (other than writing it manually...):

% file_B.tex
... Like \hyperref{file_A}{categoryname}{labelname}{Eq. ??? in file\_A.pdf}

Or course to be able to do that LaTeX would require to compile both files jointly so file_B knows about the counters of file_A (It seems the xr-hyper package can be used for that.) Above, only an independent compilation of the files is necessary.

1
  • What purpose does the argument categoryname serve here?
    – Janosh
    Commented Mar 12, 2016 at 15:02
6

You could also attach B.pdf, C.pdf, ... to A.pdf using embedfile and link to them using hypgotoe.

The following example is taken directly from the hypgotoe documentation:

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{filecontents}
\begin{filecontents}{hypgotoe-child.tex}
\NeedsTeXFormat{LaTeX2e}
\documentclass{article}
\usepackage{hypgotoe}[2007/10/30]
\begin{document}
\section{This is the child document.}
\href{gotoe:%
  dest={page.1},parent%
}{Go to first page of main document}\\
\href{gotoe:%
  dest={page.2},parent%
}{Go to second page of main document}
\newpage
\section{This is the second page of the child document.}
\href{gotoe:%
  dest={page.1},parent%
}{Go to first page of main document}\\
\href{gotoe:%
  dest={page.2},parent%
}{Go to second page of main document}

\hypertarget{foobar}{}
Anker foobar is here.
\end{document}
\end{filecontents}
\documentclass{article}
\usepackage{hypgotoe}[2007/10/30]
\usepackage{embedfile}
\IfFileExists{hypgotoe-child.pdf}{%
  \embedfile{hypgotoe-child.pdf}%
}{%
  \typeout{}%
  \typeout{--> Run hypgotoe-child.tex through pdflatex}%
  \typeout{}%
}
\begin{document}
\section{First page of main document}
\href{gotoe:%
  dest=page.1,embedded=hypgotoe-child.pdf%
}{Go to first page of child document}\\
\href{gotoe:%
  dest=page.2,embedded=hypgotoe-child.pdf%
}{Go to second page of child document}\\
\href{gotoe:%
  dest=foobar,embedded=hypgotoe-child.pdf%
}{Go to foobar in child document}
\newpage
\section{Second page of main document}
\href{gotoe:%
  dest=section.1,embedded=hypgotoe-child.pdf%
}{Go to first section of child document}\\
\href{gotoe:%
  dest=section.2,embedded=hypgotoe-child.pdf%
}{Go to second section of child document}\\
\href{gotoe:%
  dest=foobar,embedded=hypgotoe-child.pdf%
}{Go to foobar in child document}
\end{document}

My familiarity with this is non-existent, although it may be a way to submit a document in parts that ensure they stay in the same bundle plus allows hyperlinking from one to the other.

4

There is an alternative for xr-hyper, called zref-xr. It is an extension of the xr package. I found it better suited to my needs because xr-hyper expands the content of the label, which may contain undefined macros. Moreover zref-xr needs only to be loaded in the file which references the other file (if the references only go one way as was my case).

An example (inspired from Heiko Oberdiek's answer) is thus the following:

In fileA.tex:

\documentclass{scrartcl}

\usepackage{hyperref}

\begin{document}

\section{Hello everyone!}
\label{sec:hello}

What a fine day!
\end{document}

In fileB.tex:

\documentclass{scrartcl}

\usepackage{nameref,zref-xr} % nameref before zref-xr
\zxrsetup{toltxlabel} % allows to use the \ref command as usual
\zexternaldocument*[pref:]{fileA} % prefix is optional
\usepackage{hyperref}

\begin{document}

There is something very interesting in Section~\ref{pref:sec:hello}.
I suggest you check it out!
\end{document}

You can compile fileA as usual, then you can compile fileB as usual. No need for complex incantations.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .