# Link from label to ref and back?

I would like that the objects marked with label clickable so that when you click on it moves you to the relative \ref object and the other way around.

How should I do it?

\documentclass{article}
\begin{document}
\begin{enumerate}
\item test\label{l1}
\end{enumerate}

\pagebreak

test2\ref{l1}
\end{document}

• @DG' If I apply that to the MWE above I still don't get clickable links. And it is also incompatible with hyperref. – Eliah Oct 8 '14 at 8:51
• @DG' That does never happen in my case, each ref points to a different label. – Eliah Oct 8 '14 at 9:13
• maybe it was a too minimal example. Actually the labels are always attached to items, so with hyperref the link from ref->label is already there. I want the other way too (label->ref) – Eliah Oct 8 '14 at 9:20
• @DG' I've asked the question because I don't want to do it manually. – Eliah Oct 8 '14 at 9:29
• @Mico As I said it is not the case that there is more than one \ref per \label. I cannot rely on the pdf viewer because this will be used with many different viewers, it's not for personal use. – Eliah Oct 8 '14 at 11:07

I understand your setup to be as follows: You have numbered answers in one part of the document, you have answers somewhere else in the document, and you would like to provide two-way hyperlinks between Question 1 and Answer to Question 1, Question 2 and Answer to Question 2, etc.

• If you have numbered questions and increment the question-related counter using \refstepcounter, you can use \ref in the answer to create a link to the numbered question. If hyperref is loaded, the \ref will be resolved into a hyperlink automatically. The only tricky part is to create a hyperlink from the question to the answer; you can't use \ref as in all likelihood there's no (independent) counter associated with the answers. Fortunately, hyperref provides the \hyperlink/\hypertarget pair of macros for just this case.

• In the example below, I've set up an environment called question that takes three arguments: The question's title, the text of the \label that's associated with the question, and the name of the hypertarget associated with the answer. The third argument is used in the environment's \hyperlink instruction.

• The corresponding answer entry takes two arguments: the argument of the \label macro of the associated question, and the name of the answer's hypertarget. (Note that arguments 2 and 3 of the question should be the same as the arguments 1 and 2 of the associated answer.) The second argument is used in the environment's \hypertarget instruction.

\documentclass{article}
\usepackage{amssymb}  % for \Box macro
{hyperref}
\usepackage[textheight=3cm]
{geometry} %% just for this example

\newcounter{quest}
\newenvironment{question}[3]{%
\refstepcounter{quest} \label{#2}
\par\bigskip\noindent
\textbf{Question \thequest: #1?}
\par\medskip

For an answer, click here: \hyperlink{#3}{$\Box$}}{\par\bigskip}

\par\bigskip\noindent%
\par\medskip}{\par\bigskip}

\begin{document}

\begin{question}{What's the meaning of life}%
{quest:life}{ans:life}
\end{question}

\begin{question}{What is Pythagoras' Theorem}%
{quest:pyth}{ans:pyth}
\end{question}

\newpage
Mumble, mumble, mumble \dots

Mumble, mumble, mumble \dots
\end{document}


If you don't use multiple \refs per \label you could use the following:

\documentclass{article}
\usepackage{hyperref}

\newcommand{\mylabel}[2]{%
\hyperref[#1]{#2}\label{back:#1}
}
\newcommand{\myref}[2]{%
\hyperref[back:#1]{#2}\label{#1}
}

\begin{document}
\begin{enumerate}
\item \mylabel{l1}{test}
\end{enumerate}

\pagebreak

\myref{l1}{test2}
\end{document}


Another solution would be to pack \hyperlink and \hypertarget in a macro:

\documentclass{article}
\usepackage{hyperref}

\newcommand{\mylabel}[2]{%
}
\newcommand{\myref}[2]{%