objetivos de hiperref sin \refstepcounter

objetivos de hiperref sin \refstepcounter

Estoy intentando crear un enlace interno del documento usando el hyperrefpaquete. En la posición a la que debería apuntar el enlace, ejecuto el siguiente comando (como parte de la configuración de un entorno LaTeX):

\edef\@currentlabel{LABEL}

Después de esto, uso \label{...}para crear una referencia y el archivo \ref{...}. El hyperrefpaquete convierte el \ref{...}en un hipervínculo como se esperaba, pero el vínculo apunta a la ubicación incorrecta (más arriba en el texto). ¿Cómo puedo saber hyperrefhacia dónde debe apuntar el enlace?

No puedo usarlo \refstepcounterporque mis etiquetas son textuales y no están asociadas a un contador LaTeX.

Aquí hay un ejemplo de trabajo "mínimo" (bueno, "pequeño") para ilustrar el problema:

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

Respuesta1

Debes fijar un ancla en el lugar apropiado. Esto se hace automáticamente cuando \refstepcounterse emite, pero debe hacerlo manualmente cuando \@currentlabelse configura sin la ayuda de un contador.

Se puede establecer un ancla \phantomsection(aunque es un mal nombre).

\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

Respuesta2

Puede utilizar los paquetes \hyperlinky \hypertargetlas macros. No requieren contadores, \refstepcounteracciones o redefiniciones de \@currentlabel.

  • Insertar \hypertarget{<anchor name>}{<some text>}en la ubicacióna la queel lector debería saltar.

  • Insertar \hyperlink{<anchor name>}{<other text>}en una o más ubicacionesa partir del cualel lector debe saltar a la ubicación especificada en otra parte del documento mediante una \hypertargetinstrucción.

Un ejemplo muy sencillo:

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

La palabra "aquí" en la segunda página se mostrará en azul y al hacer clic en ella accederá a la línea "Un cuento maravilloso" en la página anterior.

Es posible tener varias \hyperlinkinstrucciones que apunten al mismo nombre de ancla, pero solo debe haber una \hypertargetinstrucción para un nombre de ancla determinado.

Adaptando estas ideas a su código de prueba, podría verse así:

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

información relacionada