0

I have two documents file1.tex and file2.tex in two different folds fold1 and fold2, which are in the fold father.

In file1, I defined \label{file1:hello}, how do I use it in file2?

4
  • I think the xr package would help. Feb 12 '14 at 20:51
  • 1
    If you wish to cross-reference with the aid of hyperref, then see Does hyperref work between two files?
    – Werner
    Feb 12 '14 at 20:53
  • @Werner how to set the relative path correct?
    – Ma Ming
    Feb 12 '14 at 20:58
  • 1
    @MaMing: You should read the linked post. It requires you to define (say) \externaldocument{../fold2/file2} in file1.tex. Then you would use the label as-is: \label{hello}. Otherwise, if you may have duplicate labels (hello in file1.tex and hello in file2.tex), then you should use \externaldocument[<prefix>]{...}.
    – Werner
    Feb 12 '14 at 21:01
2

I replace here the same answer given for this question: in this case solutions are for files in the same folder.

Solution 1

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

In the file supplementmain.tex you say:

\usepackage{xr}
\externaldocument{supplement}
...
\ref{ext:supplement}

Note: normally xr doesn't add hyperlinks. Use zref package for this. You only need to replace \externaldocument with \zexternaldocument.

Solution 2

A minimal working example taken from the following site: GUIT

First.tex

\documentclass[a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument[S-]{Second}[Second.pdf]
\begin{document}
\chapter{One}\label{chp:one}
\chapter{Two}\label{chp:two}
Bla Bla Bla.
\end{document}

Second.tex

\documentclass[a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument[F-]{First}[First.pdf]
\begin{document}
\chapter{Beginning}\label{chp:One}
Hello!
\chapter{Continuation}
I read chapters~\ref{F-chp:one}~and~\ref{F-chp:two} of the first file.

\end{document}

GUIT also says that in this way you have the hyperlink to the first file.

If files are in different folders, you can add paths into \externaldocument

Not the answer you're looking for? Browse other questions tagged or ask your own question.