
パッケージを使用してドキュメント内部リンクを作成しようとしていますhyperref
。リンクが指す位置で、次のコマンドを実行します (LaTeX 環境のセットアップの一環として)。
\edef\@currentlabel{LABEL}
この後、 を使用\label{...}
して参照を作成し、 を使用します\ref{...}
。hyperref
パッケージは を\ref{...}
期待どおりにハイパーリンクに変換しますが、リンクは間違った場所 (テキストの上部) を指します。 リンクが指すべき場所を確認するにはどうすればよいでしょうかhyperref
?
\refstepcounter
ラベルがテキストであり、LaTeX カウンターに関連付けられていないため、使用できません。
問題を説明するための「最小限の」(正確には「小さい」)実例を以下に示します。
\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}[section]
\makeatletter
\newenvironment{algorithm}[2]%
{\medbreak
\edef\@currentlabel{#1}%
% more stuff here (put entry in table of algorithms, etc.)
\noindent
\textbf{Algorithm~\@currentlabel\ (#2)}\hfill\break
\ignorespaces}%
{\medbreak}
\makeatother
\begin{document}
\section{Test}
\begin{theorem}
\lipsum[1]
\end{theorem}
\begin{algorithm}{TEST}{Test Algorithm}\label{alg:TEST}%
\lipsum[2]
\end{algorithm}
The following link points to the theorem instead of the algorithm: \ref{alg:TEST}.
\end{document}
答え1
適切な場所にアンカーを設定する必要があります。 が発行されると自動的に行われますが、 がカウンターを使用せずに設定される\refstepcounter
と手動で行う必要があります。\@currentlabel
アンカーは\phantomsection
(名前は悪いですが)で設定できます。
\makeatletter
\newenvironment{algorithm}[2]%
{\medbreak
\edef\@currentlabel{#1}%
% more stuff here (put entry in table of algorithms, etc.)
\noindent\phantomsection % <------------------------ add the anchor
\textbf{Algorithm~\@currentlabel\ (#2)}\hfill\break
\ignorespaces}%
{\medbreak}
\makeatother
答え2
パッケージの\hyperlink
およびマクロを使用できます。 のカウンター、アクション、または の再定義\hypertarget
は必要ありません。\refstepcounter
\@currentlabel
\hypertarget{<anchor name>}{<some text>}
場所に挿入これに読者はびっくりするはずです。\hyperlink{<anchor name>}{<other text>}
1つ以上の場所に挿入するそこから読者は、指示によって文書内の他の場所で指定された場所にジャンプする必要があります\hypertarget
。
非常に簡単な例:
\documentclass{article}
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}
\begin{document}
\hypertarget{jump_destination}{\textbf{A wonderful tale}}
Once upon a time, \dots
\clearpage
If you want to read a wonderful tale, click \hyperlink{jump_destination}{here}.
\end{document}
2 ページ目の「ここ」という文字が青色で表示され、それをクリックすると前のページの「すばらしい物語」という行に移動します。
\hyperlink
同じアンカー名を指す命令が複数あることは可能ですが、\hypertarget
特定のアンカー名に対しては命令は 1 つだけ存在する必要があります。
これらの考え方をテスト コードに適用すると、次のようになります。
\documentclass{article}
\usepackage{lipsum,amsthm,hyperref}
\newtheorem{theorem}{Theorem}[section]
\newenvironment{algorithm}[2]%
{\par\medbreak\noindent
% more stuff here (put entry in table of algorithms, etc.)
\hypertarget{#1}{\textbf{Algorithm~#1 (#2)}}%
\par\noindent\ignorespaces}%
{\medbreak}
\begin{document}
\section{Test}
\begin{theorem}
\lipsum[1]
\end{theorem}
\begin{algorithm}{TEST}{Test Algorithm}
\lipsum[2]
\end{algorithm}
\clearpage
The following link now points to the algorithm: \hyperlink{TEST}{here}.
\end{document}