# Isolate references of included files within a "namespace"

I want to compile a large series of mostly independent latex files into a single document, and I would like to be able to assume that each \label lives in its own "namespace". Ideally, what I would like to have is:

file1.tex:

$$\label{eq:a} a+b$$

This is \eqref{eq:a}


file2.tex:

$$\label{eq:a} a+2b$$

This is a very different \eqref{eq:a}


main.tex:

\include{file1.tex}
\include{file2.tex}

I would like to reference (with hyperref) both \ref{eq:a@file1.tex} and \ref{eq:a@file2.tex}


The purpose of this is to cross-reference and assemble a large collection of independent documents (but not to spend days doing so).

A solution in which the cross-referencing would work across multiple PDF file would suit me too, but I fear it would be a lot harder.

• To prefix the labels from other files in a "central" file you could use the xr or xr-hyper package. To include the independent files, I suggest to compile them separately and include the pdfs Jul 11 '17 at 15:02
• @samcarter seems like a starting point, I'll have to dig into that more. The xr documentation is, hmm, terse Jul 11 '17 at 15:16

For example with document.tex:

\documentclass{article}
\usepackage{hyperref}

\begin{document}

$$\label{eq:a} a+2b$$

This is a very different \ref{eq:a}

\end{document}


you could reference the label with a prefix using the xr package (or xr-hyper if combined with hyperref)

\documentclass{article}

\usepackage{xr-hyper}
\usepackage{hyperref}

\externaldocument[doc-]{document}

\usepackage{pdfpages}

\begin{document}

\includepdf[xr-prefix=doc-]{document}

bla \ref{doc-eq:a}

\end{document}


(the option xr-prefix is an experimental option of pdfpages)

• The link targets the original PDF file (not the one included), but this is exactly what I'm looking for, thanks ! Jul 11 '17 at 15:33