# How to \label + \ref arbitrary lines of text?

We are currently working on a document which contains lists of things that are identified by 'semi-numbered' identifiers in a way that is easy for humans to understand but difficult to 1:1 automate using a new counter.

(To be precise, we're tasked with numbering our 'functional and nonfunctional requirements' according to the naming scheme indicated by the professor of a university course in software architecture. Some examples are HL-1, FR-1.2, NFR-2.4b)

Nonetheless We'd like to refer to these from throughout the document, as their identifier and ordering might change.

This is the reason why we are looking for a general way to define a label on a small piece of text that we can refer to (using \ref, \nameref or \autoref) which should then print the name we specified rather than e.g. the name of the section it was written in.

How could this be done?

There is some overlap between this question and Link to arbitrary part of text?. However, whereas the solution posted there works well when the reference is free in deciding what to name the links, in our case we'd want to use whatever name is configured at the label to be used for all the references to it.

• This will partly depend on how you're creating the small piece of text. Are you labeling a theorem like environment? Or a line of code? Or your own thing that you've created with newenvironment? Or newcommand? Could you give us a short example? – Teepeemm Nov 29 '20 at 1:03
• @Teepeemm Interesting, I had not considered that. They are usually (at least currently) introduced as lines in a tabularx environment. – Qqwy Nov 29 '20 at 9:32

The following is a basic implementation that sets problems using the description environment while you can set a specific "named \label" to these items that are correctly hyperlinked.

\documentclass{article}

\usepackage{hyperref}

\makeatletter
\newcommand{\namelabel}[1]{%
\phantomsection
\renewcommand{\@currentlabel}{#1}% Update the label text/name
\label{#1}% Set the label
}
\makeatother

\begin{document}

Here is a list:
\begin{description}
\item[HL-1] \namelabel{HL-1}
This is a strange problem.

\item[FR-1.2] \namelabel{FR-1.2}
This is a weird problem.

\item[NFR-2.4b] \namelabel{NFR-2.4b}
This is a complicated problem.
\end{description}

See the weird problem called \ref{FR-1.2}.

\end{document}