更改 hyperref 中 \ref{} 的預設鏈接

更改 hyperref 中 \ref{} 的預設鏈接

我的文檔有章節。當我引用同一章中的部分時,我會寫;\ref{section_label}當我引用不同章節中的部分時,我會寫\ref{chapter_label}.\ref{section_label}。從紙面上看,這達到了預期的效果。

不幸的是,hyperref(當然)在一個連結上產生兩個不同的連結。因此將後一個參考替換為

\hyperref[section_label]{\ref{chapter_label}.\ref{section_label}}

這會在某些檢視器(Okular、Evince)中產生所需的行為,但對其他檢視器(PDF.js)沒有影響。

我不認為上面的「hack」是hyperref.有什麼建議嗎?

答案1

通常格式化是透過\label(實際上\refstepcounter)完成的。人們可以使用多個標籤,這取決於人們想要參考文獻的外觀。這使用了相同的\mylabel這裡

Hyperref 使用自己的標籤,可以使用 找到這些標籤\getrefbykeydefault。另外,\ref它是受保護的(在列印之前不會展開),因此如果您想在巨集中使用它,您需要使用它\getrefnumber。請參閱重新計數手冊。

我放入egreg的解決方案進行比較。

\documentclass{report}
\usepackage{hyperref}

\renewcommand{\thesection}{\arabic{section}}% non-standard definition

\makeatletter
\@ifpackageloaded{hyperref}%
  {\newcommand{\mylabel}[2]% #1=name, #2 = contents
    {\protected@write\@auxout{}{\string\newlabel{#1}{{#2}{\thepage}%
      {\@currentlabelname}{\@currentHref}{}}}}}%
  {\newcommand{\mylabel}[2]% #1=name, #2 = contents
    {\protected@write\@auxout{}{\string\newlabel{#1}{{#2}{\thepage}}}}}
\makeatother

\newcommand{\getrefanchor}[1]% #1 = label
  {\getrefbykeydefault{#1}{anchor}{Doc-Start}}

\begin{document}

One can use \ref*{chapter}.\ref{section}, \ref{mylabel} or 
\hyperlink{\getrefanchor{section}}{\getrefnumber{chapter}.\getrefnumber{section}}.

\chapter{chapter}\label{chapter}
\section{dummy}
\section{section}\label{section}\mylabel{mylabel}{\thechapter.\thesection}%
\end{document}

相關內容