Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have two documents (A and B) with labels. I would like to reference the labels in document A from document B. I would like to use the hyperref package to create hyperlinks in document B to the labels within document B. I want to resolve the references in document B to document A with the correct numbering, but I do not want to create hyperlinks for theses references. Is this possible? My motivation is that document A is being published on an external site, so I can't link to references within it and I don't want broken hyperlinks.

  • If I use the xr-hyper package (a great minimal example is available in this answer to the question "Does hyperref work between two files?"), LaTeX will create a link to document A from document B when document B references a label in document A.
  • If I use the xr package instead of xr-hyper, LaTeX will create a hyper link to label 1 of document B when document B references label 1 of document A (i.e. it still tries to create hyperlinks but it does not link them outside of the document).

For a full minimal working example, look at this adapted version of the example given in the answer here:

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}

  Here is a hyperlinked internal reference to \ref{eq:x}.
\end{document}

I want to produce a pdf with the first \ref in document B turned into a "1" but not hyperlinked to an external document and the last \ref in document B turned into a "6" and hyperlinked to eq:x.

share|improve this question
    
Did you look at the xr-hyper package? –  egreg May 5 '13 at 15:14
2  
Can't you simply use \ref*{...} for the external references? –  Ulrike Fischer May 5 '13 at 16:24
    
I have looked at xr-hyper a little bit, but I thought that was more for getting the links to actually point somewhere in the other document. I will look more closely at it for an option to turn off hyperlinks to the external document. –  ws_e_c421 May 5 '13 at 21:24
    
Yes, good catch. I had overlooked the \setcounter statement. (Before finding that other question to reference, I had actually worked out basically the same minimal working example myself. Then when I found that question, I switched to its version, but I guess I didn't try compiling it to compare). –  ws_e_c421 Aug 4 at 2:36

2 Answers 2

Thanks for posting an example with compilable code. As @UlrikeFischer has pointed out in an earlier comment, if the hyperref package is loaded, you should use \ref* instead of \ref to create a cross-reference that's not also a hypertarget. Please note that this applies regardless of whether the cross-reference's target is located in the same document or in some other document.

Still assuming that hyperref is loaded, one should use \autoref* rather than \autoref to create cross-references that (a) automatically provide the name of the item being cross-referenced (e.g., "section", "figure", "footnote", etc) as part of the call-out and (b) aren't hypertargets. Likewise, if the cleveref package is loaded in addition to the hyperref package, one should use \cref* and \Cref* instead of \cref and \Cref to create "clever" cross-references that aren't also hypertargets.

share|improve this answer
1  
Thank you for the answer. So what if I wanted to be able to turn on and off the external hyperlinks quickly? Would the best option be to use a text editor that can find all \ref{A- instances and replace them with \ref*{A-, and vice versa? I suppose as long as I used a unique prefix like A- to distinguish the external labels from the internal ones that this method would work. I didn't know about \ref* when I asked the question, but I had been expecting there would be some option I could set in the preamble rather than changing the reference syntax. –  ws_e_c421 Aug 4 at 2:44
    
@ws_e_c421 - Your idea of doing a global search-and-replace in DocB for instances of \ref{A- and replacing the instances with \ref*{A- would seem to be the right way to proceed. To turn hyperlinking back on, replacing all instances of \ref* with \ref should be rather easy to accomplish, right? I suspect that messing with, emmm, modifying the definition of \ref purely to "automate" the non-generation of hyperlinks seems like an open invitation to disaster. –  Mico Aug 4 at 8:49

My idea is:

  1. to patch the xr-hyper package so that it records all labels that it read from external documents; this allows us to distinguish between internal references that should be automatically hyperlinked and external references that should not.
  2. to redefine the unstarred version of hyperref's \ref to check whether the reference is external or not, and to essentially assume that the starred version was used if so.

I believe the following does what you want, changes only necessary to docB.tex:

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


% following code is to meet objectives of [How to cross-reference labels between two documents without hyperlinks, while still creating hyperlinks to labels within the same document?](http://tex.stackexchange.com/questions/112603/how-to-cross-reference-labels-between-two-documents-without-hyperlinks-while-st)
% insert this after loading xr-hyper and hyperref but before calling \externaldocument
\makeatletter
% patch XR so that it records whether or not the label has come from an external file
\long\def\XR@test#1#2#3#4\XR@{%
  \ifx#1\newlabel
     \expandafter\protected@xdef\csname r@\XR@prefix#2\endcsname
       {\XR@addURL{#3}}%
     \expandafter\protected@xdef\csname isxr@r@\XR@prefix#2\endcsname{1}%
  \else\ifx#1\@input
     \edef\XR@list{\XR@list\filename@area#2\relax}%
  \fi\fi
  \ifeof\@inputcheck\expandafter\XR@aux
  \else\expandafter\XR@read\fi}

% every time \ref is used without an explicit star, check if it is an external reference or not
\AtBeginDocument{%
    \let\orig@T@ref\T@ref
    %patched \T@ref checks if the ref is from xr, and redirects to \@refstar if so
    % else it passes to \orig@T@ref
    \def\T@ref#1{%
        \ifcsname isxr@r@#1\endcsname
            %\typeout{sending #1 to refstar}%
            \@refstar{#1}%
        \else
            %\typeout{sending #1 to orig@T@ref}%
            \orig@T@ref{#1}%
        \fi
    }%
}%
\makeatother
% end patch

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

  Here is a hyperlinked internal reference to \ref{eq:x}.
\end{document}

Output:

docB output

Note that this may not play well with other packages that hook into \ref etc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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