13

Is it possible to refer to a word in a sentence?

Example: Hi, I love\label{Word: Love} learning so I ask a lot of questions.

I would like to refer to the word "love" in the above sentence. For instance:

"I `ref{Word: Love}` you" would then read "I love you" but with "love" being clickable etc.

Is this possible?

(I know there are some related question here, but they are too complicated and different from the present one. I believe I have a simple and to the point question. )

  • 1
    \hyperlink and \hypertarget? – egreg Feb 25 '15 at 19:09
  • \hyperlink links to the given word but I want the name of the linked word to show as in equations e.g. (\ref{Eq: NewtonsSecond}) would automatically be written (9) if it happens that Newtons Second Law is the ninth equation in my paper. But I would like to refer to a word where the word is written out as in \ref{Word: Word1} would give "Amazing" if I define Word1 = "Amazing". Maybe I'm still unclear? – Your Majesty Feb 25 '15 at 19:21
  • Just to clarify: you want the "love" in "I love you" to be clickable and the link brings you to the sentence "I love learning...", and that the "love" in "I love learning..." is not clickable. Is this correct? – Herr K. Feb 25 '15 at 19:48
17

You have somehow to mark the word you want to refer to:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage[colorlinks]{hyperref}

\makeatletter
\newcommand{\setword}[2]{%
  \phantomsection
  #1\def\@currentlabel{\unexpanded{#1}}\label{#2}%
}
\makeatother

\begin{document}

Some text just to see where the link will point to

Hi, I \setword{l\'ove}{Word:Love} learning so I ask a lot of questions.

\newpage

"I \ref{Word:Love} you"

\end{document}

Note the usage of the accent in order to show that it works even with special characters.

enter image description here

  • 5
    That's just a perfect answer. Your code will now permeate spacetime and remain long after the Sun burns out. – Your Majesty Feb 25 '15 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.