我正在尝试从 beamer 文件链接到文章中的标签。参考本身可以正常工作,但超链接不行。它默认为 beamer 文件的第 1 页。
以下是文章内容:
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{section}\label{target}
\end{document}
这是投影机:
\documentclass{beamer}
\usepackage{xr-hyper}
\externaldocument[article-]{article}[article.pdf]
\begin{document}
see \ref{article-target}
\end{document}
当不使用 beamer 类时它可以正常工作。
答案1
问题是beamer
请求hyperref
无法完全加载,因此hyperref
不\ref
使用 (和支持命令)。相反,尝试在链接支持方面模拟的beamer
行为,但不尝试模拟hyperref
\ref
外部链接支持。
我确信下面的代码确实很糟糕,会破坏很多东西,但你可以在 中手动重新启用hyperref
的处理。但大概出于好的理由不使用的版本,所以要小心!\ref
beamer
beamer
hyperref
至少,这也会改变\pageref
为使用链接。
开始:
\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}