# Use label to refer to text and print it

I want to make requirements in a latex document and I would like to refer to them. What I have so far is this:

\documentclass{memoir}

\usepackage{hyperref}
\usepackage{lipsum}

\newcounter{requirements}
\setcounter{requirements}{0}

\newcommand{\Requirement}[1]{%
~\linebreak\noindent%
{\hspace*{1em}{%
\refstepcounter{requirements}%
\textbf{Req.~\arabic{requirements}}%
\label{req:\arabic{requirements}}%
\hspace*{1em}%
#1}\\*[\baselineskip]}
}

\begin{document}
\Requirement{First requirement!}
\Requirement{Requirement number two!}
\Requirement{Requirement number three!}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
See \ref{req:1}.

\end{document}


But how can I now make the \ref command say Req. 1 instead of just 1. Also I would like to be able to print the text of the requirement in the text upon request, such that I could give the \printrequirement{Req-1} command (or something similar) and that it would print the text of the requirement. Is something like this possible? It is important that the Req is included (in Req-1, or it could be something like \printrequirement{Req}{1}), because I want to make several different subsets of requirements so, for instance, there will also be a new counter using Subreq- as prefix.

Thanks!

-
I just want to note that it is dangerous to add labels automatically. If the order of requirements change, then your cross-references will necessarily break. –  Sean Allred Apr 16 '14 at 16:14
We ran into a similar requirement for requirements. We also wanted (A) \label and \ref to "work" so we could give requirements a label and refer to them by label (typically within an inspection or test), (B) requirements, inspections, and tests to optionally appear in the table of contents, (C) automatically generated traceability tables from requirements to mechanisms used to verify/validate them, and (D) works with hyperref. We ended up with almost 1000 lines of TeX code, and that's without bidirectional traceability. –  David Hammen Apr 16 '14 at 17:35
I am aware that adding labels automatically can be dangerous. However in this case it is just a project of two months in which all team members will work at the same time in the same project and if everyone uses the labels, then it should be fine, I believe. If a requirement will be removed, then the counter can be increased by 1. But Sean Allred, if you have a better idea of how to do it, be my guest. You would want to add the labels manually? –  maero21 Apr 16 '14 at 17:57

## 3 Answers

I'm not sure it's a good thing to have the \label automatically prepared. Anyway, cleveref makes the thing you ask very simple.

\documentclass{memoir}

\usepackage{hyperref}
\usepackage{cleveref}

\newcounter{requirements}
\setcounter{requirements}{0}
\crefname{requirements}{Req.}{Req.}

\newcommand{\Requirement}[1]{%
\par\addvspace{\topsep}% <----- Adjust to suit
\noindent\quad % <------------- Are you sure about this?
\refstepcounter{requirements}%
\label{req:\arabic{requirements}}%
\textbf{Req.~\arabic{requirements}}%
\quad
#1%
\par
\addvspace{\topsep}% <--------- Adjust to suit
}

\begin{document}
\Requirement{First requirement!}
\Requirement{Requirement number two!}
\Requirement{Requirement number three!}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

See \cref{req:1}.

\end{document}


I modified your definition of \Requirement because ~\linebreak and \\*[\baselineskip] are surely wrong. Adjust the spacings to suit your needs; \topsep is the usual vertical space used before and after lists, with \addvspace you ensure it's not added twice.

-

We usually redefine \the_counter_, please see an example.

% run: *latex mal-req.tex
\documentclass{memoir}
\usepackage[colorlinks]{hyperref}
\usepackage{lipsum}
\newcounter{requirements}
\setcounter{requirements}{0}
\newcommand{\Requirement}[1]{%
~\linebreak\noindent%
{\hspace*{1em}{%
\refstepcounter{requirements}%
\textbf{Req.~\arabic{requirements}}%
\label{req:\arabic{requirements}}%
\hspace*{1em}%
#1}\\*[\baselineskip]}
}
\renewcommand\therequirements{Req.\,\arabic{requirements}}
\begin{document}
\Requirement{First requirement!}
\Requirement{Requirement number two!}
\Requirement{Requirement number three!}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
See \ref{req:1}.
\end{document}


-

I added the macro \Rqref{1} to provide Req. 1 (it is longer to type, but the OP asked for it, and it allows the OP to change the definition in one place to make all occurrences say "Requirement 1" instead of "Req. 1", for example).

I also provide the macro \Rqtext{2} to provide the actual text of the requirement.

\documentclass{memoir}
\usepackage{hyperref}
\usepackage{lipsum}
\newcounter{requirements}
\setcounter{requirements}{0}
\newcommand{\Requirement}[1]{%
~\linebreak\noindent%
{\hspace*{1em}{%
\refstepcounter{requirements}%
\textbf{Req.~\arabic{requirements}}%
\label{req:\arabic{requirements}}%
\hspace*{1em}%
#1}\\*[\baselineskip]}%
\expandafter\def\csname ReqNo\romannumeral\therequirements\endcsname{#1}%
}
\newcommand\Rqtext[1]{\csname ReqNo\romannumeral#1\endcsname}
\newcommand\Rqref[1]{Req.~\ref{req:#1}}
\begin{document}
\Requirement{First requirement!}
\Requirement{Requirement number two!}
\Requirement{Requirement number three!}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
See \Rqref{1}.

The text of the second requirement was \Rqtext{2}''.
\end{document}


-