230

I want to cross-reference between different .tex files, just because I want to put every chapters separately.

Let's say in file chapterI.tex I have \label{a} and I want to access this in file chapterII.tex using something like \ref{a}. How to do this exactly in LaTeX, moreover if LEd have an easy feature to cover this, it will be better (I have put those both files in the same project).

I found another question with similar title, but I just didn't really understand. It's here: Cross-reference between LaTeX files.

zfm
  • 8,987
  • 9
  • 30
  • 21
  • 1
    You can certainly make a cross-reference across files like you want. \label{a} in one file, and \ref{a} in another. What's not working? Is there a minimal working example that shows the problem? – Mike Renfro Mar 27 '11 at 16:20
  • 1
    @Mike: and how to "compile" it into pdf or dvi exactly? I tried to do that, and it's still ?? after several tries. – zfm Mar 27 '11 at 16:27
  • 4
    Sorry, just to clarify it: Are the chapters all part of one document or are they all separated documents? In the first case it should work out of the box like @Mike said. The second case is described in my answer. – Martin Scharrer Mar 27 '11 at 16:29
  • 1
    @zfm: Since your accepted answer has nothing to do with LEd, perhaps you might change your title to "Cross reference between files in LaTeX" so that the question has broader appeal. – mforbes Sep 14 '11 at 23:39
  • @mforbes: will do it! – zfm Nov 08 '11 at 19:04
  • 2
    @zfm I think "Cross-referencing between different documents" would be even more appropriate as a title. – Jonathan Scholbach Apr 15 '18 at 08:00

8 Answers8

246

You can use the xr package to reference to other LaTeX documents.

So, in chapterII.tex, you say:

\usepackage{xr}
\externaldocument{chapterI}

And then can use \ref{a} like normal. You can also add a prefix:

\externaldocument[I-]{chapterI}

Then, you can reference it as \ref{I-a} which is useful if you have identical label names in different chapters.

Note that the normal xr doesn't add hyperlinks. It only adds the label text. The zref package can be used for this. It provides the zref-xr subpackage which gives you the same functionality and should work fine with hyperref (after all both packages are from the same author). You only need to replace \externaldocument with \zexternaldocument and \ref with \zref.

Note that the xr package reads the aux file, e.g. in this case, it checks chapterI.aux for references.

Paul Wintz
  • 402
  • 2
  • 14
Martin Scharrer
  • 262,582
  • 13
    @Martin: It might be useful to mention that you need to replace \usepackage{xr} with \usepackage{zref-xr}`. (I was not sure what a "subpackage" was when I first read your answer.) – mforbes Sep 14 '11 at 23:34
  • 3
    @Martin: I am stumped: how do you get hyperref to work with zref-xr? Using \zref and \zlabel (with \usepackage{zref-user}) generates the labels properly, but I cannot get hyper refs working. Could you please post a MWE or should I ask a related question emphasizing the hyperref aspect? – mforbes Sep 15 '11 at 06:05
  • @mforbes: Did you solve this? I posted it as a new question some days ago. – Carsten Thiel Aug 13 '13 at 07:35
  • @CarstenThiel Nope. – mforbes Aug 14 '13 at 01:24
  • 1
    But what I wanna cross-ref a figure in B.tex from A.tex, which is an unrunnable snippet? I have a structure file that has all the preambles. Both A and B are simply two snippets that cannot use any packages. How may I do it in this case? – Sibbs Gambling Dec 06 '13 at 16:35
  • 9
    One extra thing: if you use hyperref in one document you must use it in the other document otherwise the reference text will be something like 1[section name]section.1 instead of just 1 (see http://www.latex-community.org/forum/viewtopic.php?f=5&t=6531). – dshepherd Mar 02 '15 at 10:43
  • 1
    Very helpful. But why oh why isn't this automatic for documents included via \input or \include?... – beldaz Sep 30 '15 at 00:00
  • @beldaz: I don't know what you mean. Files included via \input and \include will be crossreferenced like normal. Are you talking about including a complete other document this way? – Martin Scharrer Sep 30 '15 at 18:43
  • @Martin: cross referencing didn't seem to work for input files without declaring them as external documents with xr. Could be I'm wrong, so will check later. – beldaz Sep 30 '15 at 22:19
  • 1
    @beldaz: No, that is not the case. It is very normal to have your chapters in \include files and then internal cross referencing is working like a charm. You just have to compile twice of course, or maybe more often in some cases (TOC page count changed since last run, etc.) – Martin Scharrer Oct 01 '15 at 14:05
  • @MartinScharrer: I have documents that \input content (including whole \begin{document} ... \end{ducoment} for which I have to explicitly declare \externaldocument to get references to work. Perhaps it works for you when you're dealing with smaller parts of a document, but I think my case is different. – beldaz Oct 05 '15 at 01:00
  • I cannot get working example with hyperref and zref-xr although with xr-hyper it works. My file B: \documentclass{book} %\usepackage[user,xr]{zref} %\usepackage{zref-xr} \usepackage{xr-hyper} \usepackage{hyperref} \externaldocument{A} \begin{document} \section{Shafranov}\label{Shafranov} Shafranov \newpage \section{Kotelnikov}\label{Kotelnikov} Kotelnikov \newpage \section{Refs} \hyperref[Dittmar]{hyperref to Dittmar} \hyperref[Langmuir]{hyperref to Langmuir} \hyperref[Shafranov]{hyperref to Shafranov} \hyperref[Kotelnikov]{hyperref to Kotelnikov} \end{document} Can someome modify it pf zref? – Igor Kotelnikov Nov 28 '15 at 16:12
  • 2
    zref-xr does not create hyperlinks. hyperref module is not ninished in zref. – Igor Kotelnikov Nov 29 '15 at 06:17
  • One gotcha here: when in doubt, eliminate spaces in your filenames. – Adrian Keister Mar 11 '16 at 21:35
  • @dshepherd: thanks so much, your comment save me a lot of pain. – Martin Argerami Nov 27 '16 at 05:06
  • 6
    It seems that including .tex extension to the file path in \externaldocument{...} ruins the whole thing, just a warning. – Weather Report Jan 23 '17 at 15:49
  • In my case, I cannot get the zref-xr working. xr however, works out of the box ... provided that *.synctex.gz file of the external document is present. If not, some kind of an infinite loop occurs. – Vid Merljak Jul 21 '17 at 07:13
  • 1
    @MartinScharrer: It is not clear whether with the zref-xt package, the cross-references and labels must all be given with the z prefix (e.g. \zref and \zlabel) also in the external document, or not. Could you clarify this? Also, does it matters in what order are the zref-xr and hyperref packages loaded? Usually, hyperref is preferred to be loaded last ... – Vid Merljak Jul 21 '17 at 07:18
  • Is it possible to get the pageref? Like for example, in Document 2, I want to reference the label, and also the page that label appears in Document 1? – Nicholas Hamilton Jul 25 '17 at 05:35
  • this is useful and working. We can refer equations from external document but equation numbers in new document clash with old so for this we can use set the starting line number using: \setcounter{equation}{16} – Vinod Kumar Chauhan Sep 23 '17 at 09:22
  • @SibbsGambling it sounds like you normally compile the whole document (with your structure file), but need to compile a version with only A.tex that cross references to B.tex without additional packages. I needed to do something similar for a manuscript submission that wanted a main.tex and a supplementary.pdf. You can manually steal the \newlabel commands from the combined aux file. – Jeff Jun 21 '18 at 00:18
  • 1
    This works well for me, thanks! I must emphasize that I was compiling in my local machine. However, the package cannot read the aux file when you use an online TeX editor like Overleaf. That being said, make sure you compile locally and then upload the aux file to the overleaf repository. – MauOlivares Jan 27 '22 at 10:52
  • For this to work should we compile chapterI.tex file first like: pdflatex chapterI.tex ? – alper Nov 15 '22 at 12:57
  • @alper: Yes, correct. – Martin Scharrer Nov 19 '22 at 18:53
26

As explained earlier xr or xr-hyper do the trick. They need however the corresponding .aux file of the included file. Online LaTeX editors like overleaf do not present these files. A solution is given here:

in your preamble after including the 'xr' package define some helper functions

\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}% }

Then include your external document with \myexternaldocument{...} (note the my)

Then add a file called latexmkrc with the following content:

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

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

Now the required .aux file will be preserved, and you are good to go.

mbeyss
  • 369
16

Beamer and xr: If you want to use the xr package to pull external references into a Beamer document, you need to be sure that the source of the references has been typeset using the hyperref package. This is because Beamer uses hyperref by default and the format of the references in the .aux file is different for hyperref. If the references are coming from another Beamer document, there is no need to do anything. If they are coming from a basic LaTeX document you may need to add \usepackage{hyperref} and typeset the source document again. As far as I can tell, there is no downside to always using hyperref.

14

This FAQ from the UK TUG suggests to use xr-hyper if using hyperref:

\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument[V1-]{volume1}    % Not volume1.tex
...
... (see Volume~I, Section~\ref{V1-sIntroduction}) ...

The FAQ cites zref-xr as a superset.

Dear users of zref: please edit this answer to list some benefits of zref.

2

There is a fully functioning minimal LaTeX template at this GitHub repo to do just that. Separate chapters stay in separate source files with cross-referencing enabled. Also, there is no problem if the label names in two different source files clash. See the readme for the repo to see how all this works.

-1

subfiles also a solution for this, usage is:

\usepackage{subfiles}
\begin{document}
\subfile{file.tex}
MadyYuvi
  • 13,693
-1

That works well for me:

\chapter{tobereferenced} \label{tobereferenced}

bla bla bla \ref{tobereferenced}.

Credits: https://www.overleaf.com/learn/latex/Cross%20referencing%20sections,%20equations%20and%20floats

TobiBS
  • 5,240
-1

To expand on @MadyYuvi's answer, if you use the subfiles package you can just put your references in as you would in a single document, and they will compile and work just fine. This worked wonders for me.