6

Hi I build a customer list environment with a counter, and I'd like to be able to reference the name the label associated with the counter. The code below produces an output similar to this:

Requirements

  • REQ1 Requirement one
  • REQ2 Requirement two

Another Section

  • We want read "Requirement One" here : Requirements.
  • But instead we get the section name!

Code:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[bookmarks, colorlinks=true, linkcolor=black, citecolor=black, urlcolor=black]{hyperref}

\newcounter{reqcount}
\renewcommand{\thereqcount}{\textbf{REQ\arabic{reqcount}}}

\newenvironment{nfrequirements}
  { \begin{list}{\textbf{\thereqcount}}{\usecounter{reqcount}} }
  { \end{list} }

\newcommand{\requirement}[1]{
  \item \textbf{#1}
  \addcontentsline{toc}{subsection}{\thereqcount \quad #1}
}


\begin{document}
\tableofcontents

\section{Requirements Section}
\begin{requirements}
  \requirement{Requirement one} \label{req_one} \\
  description
  \requirement{Requirement two} \label{req_two} \\
  description
\end{requirements}

\section{Another Section}
We want read "Requirement One" here : \nameref{req_one}.
But instead we get the section name.

\end{document}
11

Set \@currentlabelname:

\makeatletter
\newcommand{\requirement}[1]{%
  \item
  \def\@currentlabelname{#1}%
  \textbf{#1}
  \addcontentsline{toc}{subsection}{\thereqcount \quad #1}
}
\makeatother
1

It's not quite the same thing, but consider the following:

\newenvironment{requirements}
  { \begin{list}{\textbf{\thereqcount}}{} }
  %{ \begin{list}{\textbf{\thereqcount}}{\usecounter{reqcount}} }
  { \end{list} }
\newcommand{\requirement}[1]{
  \refstepcounter{reqcount}
  \item \textbf{#1}
  \addcontentsline{toc}{subsection}{\thereqcount \quad #1}
}

The inclusion of \refstepcounter means that the value of the reqcount counter becomes the thing that \label now picks up (rather than the argument of \section, which will have been the last thing that invoked \refstepcounter).

After this, \ref{req_one} expands to "REQ1". Getting \nameref to do the same is presumably not far away (ie, exercise for reader territory!)

  • 1
    Responding to Ulrike's suggestion: \refstepcounter ultimately results in setting \@currentlabel (though with hyperref being part of the game, that's a biiig 'ultimately'). I suspect that \@currentlabelname is a hyperref addition, to support \nameref. – Norman Gray Oct 27 '10 at 9:23

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.