# Automatically generating appendix containing full definitions and page references

I have a large document that contains a number of definitions. I would like to create an appendix that includes all of the definitions in one place for convenience. Ideally, the appendix should print the definition how it appears in the document, including the original citation number and the full body of the definition; additionally, the appendix should provide a page reference for the definition. My document currently uses ntheorem to define the theorem-like environments (including definitions), and I would like to keep it that way if possible. What I have in mind is shown below.

\documentclass{report}
\usepackage[standard]{ntheorem}

\begin{document}

\begin{definition}
The body of my first definition.
\end{definition}

\begin{definition}
The body of my second definition.
\end{definition}

\appendix

\begin{description}
\item[Definition 1 (page 1)] The body of my first definition.
\item[Definition 2 (page 1)] The body of second definition.
\end{description}

\end{document}


Easy: collect the definition texts and print them at the end. ;-)

We need to cope with possible \label commands in the body in order to avoid duplications. So, if a \label command is found, its argument is used for the \pageref, otherwise a label is generated automatically.

The bodies are collected in a token register, together with the relative \item command.

\documentclass{report}
\usepackage[standard]{ntheorem}
\usepackage{environ}

\makeatletter
\let\ntheorem@definition\definition
\let\ntheorem@enddefinition\enddefinition
\newtoks\definition@toks
\newcounter{definitioncount}

\RenewEnviron{definition}[1][]{%
\@tempswafalse
\expandafter\catch@definitionlabel\BODY\label\@@nil\@nil
\if\relax\detokenize{#1}\relax
\ntheorem@definition
\else
\ntheorem@definition[#1]%
\fi
\if@tempswa
\refstepcounter{definitioncount}%
\edef\definition@label{\romannumeral\value{definitioncount}}%
\label{\definition@label}%
\fi
\BODY
\ntheorem@enddefinition
\edef\@tempa{%
\noexpand\item[Definition\noexpand~\thedefinition\noexpand~%
(page\noexpand~\noexpand\pageref{\definition@label})]%
\unexpanded\expandafter{\BODY}%
}
\global\definition@toks=\expandafter{\the\expandafter\definition@toks\@tempa}%
}
\def\catch@definitionlabel#1\label#2#3\@nil{%
\if\relax\detokenize{#3}\relax
% no \label
\@tempswatrue
\else
% \label
\def\definition@label{#2}%
\fi
}
\def\printdefinitions{%
\begin{description}\let\label\@gobble
\the\definition@toks
\end{description}
}
\makeatother

\begin{document}

\begin{definition}\label{first}
The body of my first definition.
\end{definition}

\begin{definition}[Something]
The body of my second definition.
\end{definition}

\appendix

The collection of definitions follows.

\printdefinitions

\end{document}


ntheorem is not necessary. Here's a version for amsthm.

\documentclass{book}
\usepackage{amsthm}
\usepackage{environ}

\theoremstyle{definition}
\newtheorem{defnINN}{Definition}[chapter]

\makeatletter
\newtoks\definition@toks
\newcounter{definitioncount}

\NewEnviron{defn}[1][]{%
\@tempswafalse
\expandafter\catch@definitionlabel\BODY\label\@@nil\@nil
\if\relax\detokenize{#1}\relax
\defnINN
\else
\defnINN[#1]%
\fi
\if@tempswa
\refstepcounter{definitioncount}%
\edef\definition@label{\romannumeral\value{definitioncount}}%
\label{\definition@label}%
\fi
\BODY
\enddefnINN
\edef\@tempa{%
\noexpand\item[Definition\noexpand~\thedefnINN\noexpand~%
(page\noexpand~\noexpand\pageref{\definition@label})]%
\unexpanded\expandafter{\BODY}%
}
\global\definition@toks=\expandafter{\the\expandafter\definition@toks\@tempa}%
}
\def\catch@definitionlabel#1\label#2#3\@nil{%
\if\relax\detokenize{#3}\relax
% no \label
\@tempswatrue
\else
% \label
\def\definition@label{#2}%
\fi
}
\def\printdefinitions{%
\begin{description}\let\label\@gobble
\the\definition@toks
\end{description}
}
\makeatother

\begin{document}

\chapter{Title}

\begin{defn}\label{first}
The body of my first definition.
\end{defn}

\begin{defn}[Something]
The body of my second definition.
\end{defn}

\appendix

\chapter{Definitions}

The collection of definitions follows.

\printdefinitions

\end{document}