超链接到具有给定超目标的页面顶部

超链接到具有给定超目标的页面顶部

我需要创建hyperlink指向给定页面顶部的引用hypertarget。默认情况下,hyperlink引用指向hypertarget引用所在的文本行hypertarget。但是,我希望它转到此超目标所在的页面顶部,就像页面一样\hyperlink{page.3}{page 3}

以下是 MWE:

\documentclass{book}
\usepackage{lipsum}
\usepackage{hyperref}

\begin{document}

Following link will go to top of \hyperlink{page.3}{page 3}.

I want \hyperlink{mytarget1}{this link} to also go to top of page 3, because ``mytarget1'' is located on this page.

And \hyperlink{mytarget2}{this one} to top of page 4, because ``mytarget2'' is located on this page.

\lipsum[1-15]

\phantomsection\hypertarget{mytarget1}{mytarget1 is on this page}

\lipsum[5]

\phantomsection\hypertarget{mytarget2}{mytarget2 is on this page}

\end{document}

我发现了一个回答类似的问题,但问题和答案涉及用作\label“目标”目的地。

这个问题不是重复的,因为我想通过超目标来实现这一点,而根本不需要使用标签。

是否可以?

答案1

我将和\hypertarget和合并\label为一个宏,对 PDF 链接和辅助文件使用相同的标签名称\newlabel。可以使用\hyperlink\ref返回\hypertarget文本)\pageref和新宏\pagelink(链接到页面锚点)。

\documentclass{book}
\usepackage{lipsum}
\usepackage{hyperref}

\makeatletter
\newcommand{\pagetarget}[2]% #1=label (both hypertarget and label), #2=text
{\hypertarget{#1}{#2}\protected@write\@auxout{}{%
   \string\newlabel{#1}{{#2}{\thepage}{page.\thepage}{#1}{}}}}
\makeatother
% \hyperlink{#1}{button} will link to #2.
% \pagelink{#1}{button} will link to the page anchor.
% \ref{#1} will return #2 and link to #2.
% \pageref will return the page and link to #2.
% \getrefbykeydefault{#1}{name}{Doc-Start} returns the page anchor.

\newcommand{\pagelink}[2]% #1=label, #2=text
{\hyperlink{\getrefbykeydefault{#1}{name}{Doc-Start}}{#2}}

\begin{document}

Following link will go to top of \hyperlink{page.3}{page 3}.

I want \pagelink{mytarget1}{this link} to also go to top of page 3, because ``mytarget1'' is located on this page.

And \pagelink{mytarget2}{this one} to top of page 4, because ``mytarget2'' is located on this page.

\lipsum[1-15]

\pagetarget{mytarget1}{mytarget1} is on this page

\lipsum[5]

\pagetarget{mytarget2}{mytarget2} is on this page

\end{document}

相关内容