Labeled item with custom label text


In a document I am currently working on, I need an item list where each \item is labeled, so I can reference it later on. Instead of a simple numeric label, I want some text to be displayed at the position of the label and the same text at each position where I reference the label. I could not find a package that does this for me, so, inspired by the answer of Michael Underwood in this thread, I built a set of custom commands for item definition and referencing. Here’s the code:

\newcounter{reqcount}
\newcommand{\descitem}[1]{%
  \item[R\ref*{#1}] \refstepcounter{reqcount}\label{#1}
}
\newcommand{\descref}[1]{%
  \hyperref[#1]{R\ref*{#1}}%
}

Basically, this enhances Michaels version with some text and a \ref* command to get the right label description and number, formatted as a single link in conjunction. The description to appear in the text consists of the single letter ‘R’ together with the index number of the label. So first of all, a new counter is needed, named reqcount here. Thereafter, the descitem command, to be used in some item list is defined. It simply reuses the \item command and displays the name to appear in the text using \ref*, increases the counter and sets the label. To get the same text later on, an extra ref-command, \descref is necessary. It takes the label-id, references it using hyperref and builds the text to display, properly linked of course, in the same fashion as before. Please note that you need the \ref command to get the right index number and the * to include the complete text, the ‘R’ and the index number, in the link. Also, the %-signs are vital. If you omit them, there are superfluous white spaces directly following your references. Here is a sample usage in the text:

....
\begin{itemize}
 \descitem{item:xyz} foo
 \descitem{item:abc} bar
\end{itemize}
...
As defined in \descref{item:xyz} and opposed to \descref{item:abc}, ...

Compiling the document, the text: “As defined in R1 and opposed to R2, …” is produced. R1 and R2 are properly linked. Obviously the command presented here works only for a single indexed set of items (no matter in how many actual item lists you use \descitem). If you need multiple of those lists, a quick suggestion would be to replicate the commands with different names or use multiple counters and some kind of if-then-else in the commands.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s