
私の文書には章と節があります。同じ章内の節を参照する場合は と書き、\ref{section_label}
別の章内の節を参照する場合は と書きます\ref{chapter_label}.\ref{section_label}
。紙の上では、これで望み通りの効果が得られます。
残念ながら、hyperref
1つのリンクの代わりに2つの異なるリンクが生成されます。そのため、後者の参照を次のように置き換えます。
\hyperref[section_label]{\ref{chapter_label}.\ref{section_label}}
これにより、一部のビューア (Okular、Evince) では目的の動作が生成されますが、他のビューア (PDF.js) には影響がありません。
上記の「ハック」は の正しい使い方ではないと思いますhyperref
。何か提案はありますか?
答え1
\label
通常、書式設定は(実際には)で行われます\refstepcounter
。参照の見た目に応じて複数のラベルを使用できます。これは\mylabel
、ここ。
Hyperref は、 を使用して見つけることができる独自のラベルを使用します\getrefbykeydefault
。また、\ref
は保護されている (印刷されるまで展開されない) ため、マクロで使用したい場合は、代わりに を使用する必要があります\getrefnumber
。refcount のマニュアルを参照してください。
比較のために 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}