\refstepcounter가 없는 하이퍼참조 대상

\refstepcounter가 없는 하이퍼참조 대상

패키지 를 사용하여 문서 내부 링크를 만들려고 합니다 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>}하나 이상의 위치에 삽입어떤에서독자는 명령에 따라 문서의 다른 곳에서 지정된 위치로 점프해야 합니다 \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} 

두 번째 페이지의 "여기"라는 단어는 파란색으로 표시되며, 이를 클릭하면 이전 페이지의 "A 멋진 이야기" 줄로 이동합니다.

동일한 앵커 이름을 가리키는 여러 명령이 있을 수 있지만 지정된 앵커 이름에 대한 명령은 \hyperlink하나만 있어야 합니다 .\hypertarget

이러한 아이디어를 테스트 코드에 적용하면 다음과 같이 보일 수 있습니다.

\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}

관련 정보