Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
I suspect that zref can do what you want. –  Lev Bishop Oct 27 '10 at 5:49
    
A similar question was asked and answered here: stackoverflow.com/questions/2767389/… might be worth looking at. –  Seamus Oct 28 '10 at 10:17

2 Answers 2

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!)

share|improve this answer
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

Set \@currentlabelname:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.