234

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.

6
  • 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? Commented Mar 27, 2011 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
    Commented Mar 27, 2011 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. Commented Mar 27, 2011 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
    Commented Sep 14, 2011 at 23:39
  • 2
    @zfm I think "Cross-referencing between different documents" would be even more appropriate as a title. Commented Apr 15, 2018 at 8:00

8 Answers 8

248

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.

24
  • 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
    Commented Sep 14, 2011 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
    Commented Sep 15, 2011 at 6:05
  • 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 latex-community.org/forum/viewtopic.php?f=5&t=6531).
    – dshepherd
    Commented Mar 2, 2015 at 10:43
  • 2
    zref-xr does not create hyperlinks. hyperref module is not ninished in zref. Commented Nov 29, 2015 at 6:17
  • 8
    It seems that including .tex extension to the file path in \externaldocument{...} ruins the whole thing, just a warning. Commented Jan 23, 2017 at 15:49
29

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.

4
  • 13
    +1 for including instructions for Overleaf
    – irene
    Commented Oct 11, 2021 at 4:59
  • Thanks, I was stuck with Overleaf and this solved it for me!
    – psyguy
    Commented Sep 29, 2022 at 15:33
  • Can this solution applied for the non-Online LaTeX editors using pdflatex? or should I require to compile subfiles first in order to create their .aux files?
    – alper
    Commented Nov 15, 2022 at 13:14
  • I still get this error "No file article1.aux LABELS NOT IMPORTED. on input line 17"
    – 6-0
    Commented Jun 18 at 4:12
17

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.

0
3

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}
3
  • 4
    Please make your answer a complete one (including a compilable example). Remember: The main aim is not including a file, but referencing.
    – TeXnician
    Commented Aug 8, 2017 at 9:54
  • 2
    The subfiles package has no influence at all on cross-referencing. Commented Aug 8, 2017 at 12:11
  • what if it references from multiple subfiles?
    – alper
    Commented Nov 15, 2022 at 10:58
-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

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

You must log in to answer this question.

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