하이퍼참조에서 \ref{}의 기본 링크를 변경합니다.

하이퍼참조에서 \ref{}의 기본 링크를 변경합니다.

내 문서에는 장과 섹션이 있습니다. 같은 장의 섹션을 참조할 때 \ref{section_label}다른 장의 섹션을 참조할 때 씁니다 \ref{chapter_label}.\ref{section_label}. 종이에서는 원하는 효과가 나타납니다.

불행하게도 hyperref(물론) 두 개의 서로 다른 링크를 하나의 위치에 생성합니다. 따라서 후자의 참조를 다음으로 바꾸십시오.

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

이는 일부 뷰어(Okular, Evince)에서 원하는 동작을 생성하지만 다른 뷰어(PDF.js)에는 영향을 미치지 않습니다.

나는 위의 '해킹'이 의 올바른 사용이라고 생각하지 않습니다 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}

관련 정보