22

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, 2015 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? Feb 25, 2015 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, 2015 at 19:48

1 Answer 1

30

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
  • 6
    That's just a perfect answer. Your code will now permeate spacetime and remain long after the Sun burns out. Feb 25, 2015 at 21:39
  • Where did you find documentation on \setword?
    – mcp
    May 1, 2020 at 17:03
  • @young_souvlaki I defined it myself
    – egreg
    May 1, 2020 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.
    – mcp
    May 3, 2020 at 12:53
  • @young_souvlaki \phantomsection just sets an anchor point. The next \label command will use this anchor.
    – egreg
    May 3, 2020 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .