
Estoy intentando crear un enlace interno del documento usando el hyperref
paquete. 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 hyperref
paquete 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 hyperref
hacia dónde debe apuntar el enlace?
No puedo usarlo \refstepcounter
porque 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 \refstepcounter
se emite, pero debe hacerlo manualmente cuando \@currentlabel
se 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 \hyperlink
y \hypertarget
las macros. No requieren contadores, \refstepcounter
acciones 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\hypertarget
instrucció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 \hyperlink
instrucciones que apunten al mismo nombre de ancla, pero solo debe haber una \hypertarget
instrucció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}