# Reference to items in a custom list does not reference the list item

I am trying to define a custom environment to display requirements. The requirements shall have a counter, so automatically be counted R1, R2, R2... (this works). I also want to reference each requirement with a label (this does not work, instead the references points to the section).

Code:

\newcounter{reqcount}
%\renewcommand\p@reqcount{R}
\renewcommand{\thereqcount}{\arabic{reqcount}}
\newcommand*{\labelreqcount}{\thereqcount}
\newenvironment{requirements}
{
\begin{list}
{\refstepcounter{reqcount}\textbf{R\thereqcount}}
{\setlength{\rightmargin}{\leftmargin}}}
{\end{list}}

\begin{document}

\begin{requirements}
\item test
\item long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines \label{test2}
\end{requirements}

\ref{test2}

\end{document}


In the intended result the 5.3.1 should be R2 (the counter).

Edit after Bernards answer: Sub Requirement definition:

\newcounter{subreqcount}[reqcount]
\renewcommand\p@subreqcount{R}
\renewcommand\thesubreqcount{\thereqcount.\arabic{subreqcount}}
\newenvironment{subrequirements}%
{
\begin{list}
{\refstepcounter{subreqcount}\textbf{R{\thesubreqcount}}}%
{\setlength{\rightmargin}{\leftmargin}}%
\setlength{\labelsep}{10pt}\setlength{\leftmargin}{35pt}%
\setlength{\labelwidth}{40pt}\setlength{\listparindent}{0pt}}%
{\end{list}}


Update because references to labels do not work

• References in first level list work fine
• References to requirements in second-level list do not work, always referencing to R1

Example (runs, but shows faulty behaviour)

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{enumitem}
\newlist{requirements}{enumerate}{2}
\setlist[requirements,1]{wide=0pt, label=R\arabic*, widest=\textbf{R9}, font=\bfseries, leftmargin=*, ref=\textbf{R\arabic*}}
\setlist[requirements,2]{label*=.\arabic*, widest=\textbf{R.9.9}, font=\bfseries, ref=\textbf{R\arabic*}}

\begin{document}

\section{section 1}
\begin{requirements}
\item test
\end{requirements}

Some textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome text

\section{section 2}
\begin{requirements}[resume]
\item The system shall offer a User Interface for the management console.
\begin{requirements}
\item The user interface may be customizable.\label{req-2.1}
\item The user interface shall show relevant and  specific information in a clear way.
\end{requirements}
\item The management console shall be easy-to-use.
\item All reporting functionalities shall be easy-to-use.
\end{requirements}

\section{section 3}
\begin{requirements}[resume]
\item The system shall offer a User Interface for the management console.
\item The management console shall be easy-to-use.
\item All reporting functionalities shall be easy-to-use. \ref{req-2.1}
\end{requirements}

\end{document}


• make a small but complete example. That makes it much easier to test your issue and possible solutions. – Ulrike Fischer Apr 21 at 14:16
• changing the question after answers posted makes the site very hard for anyone to read later, however ref=\textbf{R\arabic*} you are specifying that references to 2nd level lists just use the number of that liist and not the number of the item in the outer list. – David Carlisle Apr 24 at 13:04

I propose this solution, with the enumitem package, and cleveref to simplifying the yping of cross references:

\documentclass[11pt]{report}
\usepackage{enumitem}
\newlist{requirements}{enumerate}{1}
\setlist[requirements,1]{wide=0pt, label=R\arabic*, widest=\textbf{R9}, font=\bfseries, leftmargin=*, ref=\textbf{R\arabic*}}
\usepackage{cleveref}
\crefname{requirementsi}{requirement}{requirements}
\Crefname{requirementsi}{Requirement}{Requirements}

\begin{document}
\setcounter{section}{3}

\begin{requirements}
\item A minimalistic requirement. \label{test1}
\item Long text that needs two lines long text that needs two lines, long text that needs two lines, long text that needs two lines. \label{test2}
\end{requirements}

\Cref{test2,test1}

\end{document}


• Thanks for the quick help, Bernard! Would this also work with sub requirements? I added my current definition of sub-requirements in the original question. – Ronja Bertsch Apr 21 at 19:12
• I guess it should be tested, but I think it would be simpler to define a requirements list with two levels, like this: \newlist{requirements,2} and adding \setlist[requirements,2]{label*=.\arabic*, widest=\textbf{R.9.9}}., crefname{requirementsii}{subrequirement}{subrequirements} and similarly for \Crefname. – Bernard Apr 21 at 19:35
• Thanks, I'll try it immediately :) – Ronja Bertsch Apr 21 at 19:41
• Works like a charm, thanks a lot! – Ronja Bertsch Apr 21 at 20:01
• You're welcome! Always glad to help! – Bernard Apr 21 at 20:20