# list of requirements, problem with listing text of requirements

I would like to have "requirements" in my document numbered. Then I wanted to have list of requirements, but not only number of requirement and corresponding page/section but also theirs text.

I tried with:

\newtheorem{requirement}{Req.}

\begin{requirement}
first requirement
\end{requirement}

some text

\begin{requirement}
second requirement
\end{requirement}

But I couldn't create list of requirements.

I tried solution from (where I think the same problem is presented): Custom list of hypotheses in table of contents format

when I used thmtools I got the same result as using package ntheorem I mean:

Req. ...... 1
Req ....... 2

etc.

While I want something like:

first requirement ..... 1
second requirement .... 2

etc.

I was also thinking about another solution, what I need is to clone "figure" environment and clone table of figures, and then I can keep requirements in captions. However figure is a float, which is not what I want.

I know it is possible to use brackets [], but then I have to have to copies of the same text, one in [] second outside.

Thanks a lot,

first try:

\documentclass{book}

\usepackage{amsthm}
\renewcommand{\listtheoremname}{List of Requirements}
\newtheorem{requirement}{Req.}

\begin{document}
\listoftheorems{requirement}

\begin{requirement}
first requirement
\end{requirement}

some text.

\begin{requirement}
second requirement
\end{requirement}

\end{document}

second try:

\documentclass{book}

\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[numberwithin=chapter]{requirement}

\renewcommand{\listtheoremname}{List of Requirements}

\begin{document}

\listoftheorems[ignoreall,show={requirement}]

\begin{requirement}
first requirement
\end{requirement}

some text.

\begin{requirement}
second requirement
\end{requirement}

\end{document}
-
I can confirm that the answers in the linked question do not work as advertised (except for the sweave solution, which strikes me as needlessly complicated). –  Caramdir Dec 11 '10 at 17:00
I posted a new answer to the original question. Sorry for sending you around in circles, I thought the answers to that question would do what the question-asker wanted. –  Caramdir Dec 11 '10 at 17:29

You haven't gotten an answer for a while, so I figured I'd try and hack together a custom solution. This will probably require some tweaking for your specific desires, but it should work. Basically, I created a custom environment for the requirements that saves a copy of their text to the .aux file. The \listofrequirements macro then simply reads them from the .aux. (This of course requires at least two passes of LaTeX, though.)

Here is a documented example:

\documentclass{book}

%% This package will make dealing with the requirements'' environment a lot easier:
\usepackage{environ}

%% This counter is used to track the number of requirements:
\newcounter{requirements}
\setcounter{requirements}{0}

%% This is a macro that gets called by the .aux file to load in data.
\gdef\savedreq#1#2{\expandafter\gdef\csname req#1\endcsname{#2}}

%% This macro will save the given requirement to the .aux file so we will have it during the next LaTeX pass to put in the list:
\makeatletter
\def\recordrequirement#1{\immediate\write\@mainaux{\string\savedreq{\the\value{requirements}}{#1}}}
\makeatother

\NewEnviron{requirement}{
\noindent
\begingroup
%% Increment the requirement counter
\refstepcounter{requirements}
%% Here is the Req.'' text:
\textbf{Req.~\arabic{requirements}}
%% Make a label that we can use to refer to this counter:
\label{req:\arabic{requirements}}
%% Next, save this requirement to the .aux file so we will have it during the next LaTeX pass to put in the list:
\recordrequirement{\BODY}
%% We will make the remainder italic:
\it
\BODY
\endgroup
}

\newcommand{\listofrequirements}{
%% First, we need to make sure that this is not the first LaTeX pass (i.e., that all of the information has already been recorded to the .aux file):
\expandafter\ifx\csname totalreqsplusone\endcsname\relax
Please run \LaTeX\ again to populate this list!
\else
\begingroup
%% This \reqi counter is what we are going to use to iterate over the requirements:
\newcount\reqi
\reqi=0
\loop
\ifnum\reqi<\totalreqsplusone
%% The requirement numbered \reqi exists!
\repeat
\endgroup
\fi
}

%% At the end of the document, we need to record to the .aux the total number of requirements!
\makeatletter
\AtEndDocument{
\refstepcounter{requirements}
\immediate\write\@mainaux{\string\xdef\string\totalreqsplusone{\the\value{requirements}}}
}
\makeatother

\begin{document}

\section*{List of Requirements}

\listofrequirements

\section*{Requirements}

\begin{requirement}
First requirement!
\end{requirement}\\

\begin{requirement}
Requirement number two!
\end{requirement}

\vfill
\pagebreak

\begin{requirement}
You can even have very long requirements: Lorem ipsum dolor sit
amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.
\end{requirement}

\end{document}

Here is like what the first page looks:

And here is the second page:

-
This one is working, except one thing: I cannot place \label{}, or even \ref{}. Is it simple way to solve it? –  user6561 Jul 5 '11 at 19:27
The \addtocounter{requirements}{1} should be \refstepcounter{requirements} so that \label can be used inside correctly. –  Martin Scharrer Jul 6 '11 at 8:35
@Adam: Are you the Adam1024 of the original question? If so, did this question got migrated from another stackexchange site (and the note about that got somehow deleted) or was it an local account which you deleted? –  Martin Scharrer Jul 6 '11 at 8:37
@Martin: Thanks! It's fixed now. –  ESultanik Jul 6 '11 at 12:15
BTW: If you don't put % direct after lines which end in } (or {) the line-end is taken as a space, which is normally not wanted. –  Martin Scharrer Jul 6 '11 at 12:20