4

I'm trying to link to a label in an article from a beamer file. The reference itself works fine, but the hyperlink doesn't. It gets defaulted to page 1 of the beamer file.

This is the article:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\section{section}\label{target}
\end{document}

This is the beamer:

\documentclass{beamer}
\usepackage{xr-hyper}

\externaldocument[article-]{article}[article.pdf]

\begin{document}
see \ref{article-target}
\end{document}

It works fine when not using the beamer class.

  • See here tex.stackexchange.com/questions/71307/…. That combination works. Maybe you can adapt it to your needs. – cacamailg Aug 9 '13 at 18:49
  • @cacamailg That might be possible, but that, in effect, means completely re-implementing the features of xr-hyper, just because beamer messes with the ref-functionality. – Carsten Thiel Aug 9 '13 at 20:14
4

The problem is that beamer requests hyperref not to load fully, and so hyperref's \ref (and support commands) are not used. Instead, beamer tries to emulate the behaviour of hyperref's \ref in terms of link support, but does not attempt to emulate the external link support.

I'm sure the following is really bad and will break lots of things, but one can re-enable hyperref's \ref handling manually in beamer. But presumably beamer doesn't use hyperref's version for good reason, so be warned!

At the very least, this also changes \pageref to use links.

Here goes:

\RequirePackage{xr-hyper}% need to load before hyperref, therefore need to load before beamer
\documentclass{beamer}

\errorcontextlines\maxdimen

%%%% [Referencing an article from a beamer document with xr-hyper](http://tex.stackexchange.com/q/127495)
\makeatletter
% undo beamer's changes to \ref, leaving us with hyperref's version
\AtBeginDocument
{
    \let\ref=\beamer@origref
}
% actually use hyperref's changes to commands used by \ref
% that were not loaded due to implicit=false
% these are copied from hyperref.sty
\let\real@setref\@setref
\def\@setref#1#2#3{% csname, extract group, refname
  \ifx#1\relax
    \protect\G@refundefinedtrue
    \nfss@text{\reset@font\bfseries ??}%
    \@latex@warning{%
      Reference `#3' on page \thepage \space undefined%
    }%
  \else
    \expandafter\Hy@setref@link#1\@empty\@empty\@nil{#2}%
  \fi
}
\def\Hy@setref@link#1#2#3#4#5#6\@nil#7{%
  \begingroup
    \toks0={\hyper@@link{#5}{#4}}%
    \toks1=\expandafter{#7{#1}{#2}{#3}{#4}{#5}}%
    \edef\x{\endgroup
      \the\toks0 {\the\toks1 }%
    }%
  \x
}
\def\@pagesetref#1#2#3{% csname, extract macro, ref
  \ifx#1\relax
    \protect\G@refundefinedtrue
    \nfss@text{\reset@font\bfseries ??}%
    \@latex@warning{%
      Reference `#3' on page \thepage \space undefined%
    }%
  \else
    \protect\hyper@@link
      {\expandafter\@fifthoffive#1}%
      {page.\expandafter\@secondoffive#1}%
      {\expandafter\@secondoffive#1}%
  \fi
}
\def\HyRef@StarSetRef#1{%
  \begingroup
    \Hy@safe@activestrue
    \edef\x{#1}%
    \@onelevel@sanitize\x
    \edef\x{\endgroup
      \noexpand\HyRef@@StarSetRef
        \expandafter\noexpand\csname r@\x\endcsname{\x}%
    }%
  \x
}
\def\HyRef@@StarSetRef#1#2#3{%
  \ifx#1\@undefined
    \let#1\relax
  \fi
  \real@setref#1#3{#2}%
}
\def\@refstar#1{%
  \HyRef@StarSetRef{#1}\@firstoffive
}
\def\@pagerefstar#1{%
  \HyRef@StarSetRef{#1}\@secondoffive
}
\def\@namerefstar#1{%
  \HyRef@StarSetRef{#1}\@thirdoffive
}
\makeatother
% end changes needed by [Referencing an article from a beamer document with xr-hyper](http://tex.stackexchange.com/q/127495)


\externaldocument[article-]{article}[article.pdf]

\begin{document}
see \ref{article-target} (or \ref*{article-target} without the link)
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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