16

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. )

3
  • 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
20

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

6
  • 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
  • Where did you find documentation on \setword? – young_souvlaki May 1 '20 at 17:03
  • @young_souvlaki I defined it myself – egreg May 1 '20 at 18:46
  • oh very cool! So is phantom section just a blank tag that can be labeled? I was looking for this since \paragraph{} \label{} worked but added weird behavior like indenting and in some cases making the text invisible. So I was looking for some kind of no action tag I could label. – young_souvlaki May 3 '20 at 12:53
  • @young_souvlaki \phantomsection just sets an anchor point. The next \label command will use this anchor. – egreg May 3 '20 at 13:41

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.