[Tex/LaTex] How to label a word inside a paragraph

cross-referencinghyperref

The idea is to place \LBL{some_label_name} in a paragraph so that I can refer back to the particular point inside that paragraph where the \LBL was placed. I would like to define the macro for \LBL.

Unfortunately the \label must be placed inside an environment with a counter. I tried phantomsection but the label picks up the true section counter. I'm using the hyperref package.

How can I achieve this, what would the \def for \LBL look like?

Best Answer

There are many ways. The following is using \refstepcounter that generates implicitly an anchor. Also instead of a section number \theword is redefined to get the word instead, if called by \ref.

\documentclass{article}
\usepackage{hyperref}
\usepackage{lipsum}
\newcounter{word}
\makeatletter
\newcommand*{\LBL}{%
  \@dblarg\@LBL
}
\def\@LBL[#1]#2{%
  \begingroup
    \renewcommand*{\theword}{#2}%
    \refstepcounter{word}%
    \label{#1}%
    #2%
  \endgroup
}
\makeatother

\begin{document}
  The \ref{word} is referenced.

  \lipsum*[1] \LBL{word} \lipsum[2]

\end{document}

If the word is complicate that it cannot be used as label name, then the optional argument helps: \LBL[labelword]{weird word}\ref{labelword}.

Related Question