3

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}
Eliah
  • 115
  • @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 08 '14 at 08:51
  • @DG' That does never happen in my case, each ref points to a different label. – Eliah Oct 08 '14 at 09: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 08 '14 at 09:20
  • @DG' I've asked the question because I don't want to do it manually. – Eliah Oct 08 '14 at 09: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 08 '14 at 11:07
  • @Mico by the way, is it really this difficult to achieve? I thought it was a matter of using some hyperref option or a specific package. Had no idea this was an "hard" problem. – Eliah Oct 08 '14 at 11:09
  • @mico So how do you set up the two-way jump between refs and labels given that I don't have multiple refs per label? Could you give an example? It will not be confusing because each ref is an answer and each label is a question. So if the reader found himself on the answers page, he will know that clicking the answer will bring him to the relative question and vice versa. – Eliah Oct 08 '14 at 11:32

2 Answers2

2

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]{%
  \hyperlink{back:#1}{\hypertarget{#1}{#2}}
}
\newcommand{\myref}[2]{%
  \hyperlink{#1}{\hypertarget{back:#1}{#2}}
}

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

  \pagebreak

  \myref{l1}{test2}
\end{document}
DG'
  • 21,727
2

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.

enter image description here

enter image description here

\documentclass{article}
\usepackage{amssymb}  % for \Box macro
\usepackage[colorlinks=true,
            linkcolor=red]
           {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}

\newenvironment{answer}[2]{%
   \par\bigskip\noindent%
   \hypertarget{#2}{\textbf{Answer to Question \ref{#1}}}%
   \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
\begin{answer}{quest:life}{ans:life}
Mumble, mumble, mumble \dots
\end{answer}

\begin{answer}{quest:pyth}{ans:pyth}
Mumble, mumble, mumble \dots
\end{answer}
\end{document}
Mico
  • 506,678