# Can I introduce a special environment which will reappear at the end?

I am writing a book-length document, and occasionally I have some text that I would like to automatically repeat at the end. For example, something like

\begin{important_thing}
\medskip
{\bf Lorem Ipsum Principle.} Blah blah blah.
\end{important_thing}


This text should display in the document as if the \begin and \end lines weren't there. In addition I want all the "important things" to appear again at the end of the document, in the order that they appeared.

I feel like it should be possible to define some macro to handle this automatically for me, but I don't know how to do it. (I could define a macro consisting of my Lorem Ipsum Principle, and then refer to it twice -- this comes close to what I'm asking for, although if I reorder things in the document I would then have to manually reorder them at the end.) Am I asking too much?

Minimal changes from my answer at Collect the input of all \TODO commands used in the document at the end. I called the stuff "cross-refs" only because that was a tag in your question. You can call it whatever you want.

MACRO VERSION See later for environment version.

\documentclass{article}
\usepackage{ifthen}
\newcounter{crossrefindex}
\setcounter{crossrefindex}{0}
\newcommand\CROSSREF[1]{%
\expandafter\def\csname crossref\roman{crossrefindex}\endcsname{#1}%
#1%
}
\newcounter{index}
\newcommand\showCROSSREFs{%
\vspace{5ex}%
\rule{10ex}{.5ex}CROSS-REF LIST\rule{10ex}{.5ex}\\%
\setcounter{index}{0}%
\whiledo{\value{index} < \value{crossrefindex}}{%
\arabic{index}):  \csname crossref\roman{index}\endcsname\\%
}%
}
\begin{document}

I start hear \CROSSREF{Fix this bug} and do some work.

Then I do thiis \CROSSREF{Get spelling fixed, too} which I have to get back
to

and then I am done

\showCROSSREFs

\end{document}


ENVIRONMENT VERSION

Unfortunately, this eats spaces after the environment, and so one must add an explicit {} after the environment if one wants to preserve it.

\documentclass{article}
\usepackage{ifthen,environ,etoolbox}
\newcounter{crossrefindex}
\setcounter{crossrefindex}{0}
\makeatletter
\NewEnviron{CROSSREF}{%
\expandafter\xdef\csname crossref\romannumeral\value{crossrefindex}\endcsname{%
\expandonce{\BODY}}%
\BODY%
}
\makeatother
\newcounter{index}
\newcommand\showCROSSREFs{%
\vspace{5ex}%
\rule{10ex}{.5ex}CROSS-REF LIST\rule{10ex}{.5ex}\\%
\setcounter{index}{0}%
\whiledo{\value{index} < \value{crossrefindex}}{%
\arabic{index}):  \csname crossref\romannumeral\value{index}\endcsname\\%
}%
}
\begin{document}

I start hear \begin{CROSSREF}Fix this bug\end{CROSSREF}{} and do some work.

Then I do thiis \begin{CROSSREF}Get spelling fixed, too\end{CROSSREF}{} which I have to get back
to

and then I am done

\showCROSSREFs

\end{document}

• @Anonymous EDITED to (correctly) provide an environment version. – Steven B. Segletes Sep 15 '16 at 17:34

Several variations are possible, but here's a start.

\documentclass{article}
\usepackage{environ}

\usepackage{lipsum} % just for the example

\newcounter{repeatatend}
\makeatletter
\def\repeat@at@end{} % initialize

\NewEnviron{important}{%
\par
\medskip
\BODY\par
\medskip
\refstepcounter{repeatatend}%
\label{repeatatend@\romannumeral\value{repeatatend}}%
\xdef\repeat@at@end{%
\unexpanded\expandafter{\repeat@at@end}%
\unexpanded{\noindent(p.~\pageref}{repeatatend@\romannumeral\value{repeatatend}}) %
\unexpanded\expandafter{\BODY\par}%
}%
}
\AtEndDocument{\section*{Important}\repeat@at@end}

\begin{document}

Here I state something very important
\begin{important}
\textbf{Lorem Ipsum Principle.} Blah blah blah.
\end{important}

\lipsum % to get to another page

Then another important thing
\begin{important}
Ducks can fly.
\end{important}
which is something to take into account.

And then I am done.

\end{document}


Here's the relevant part at page 1

Here's what happens at the end

A variation of the answer given by the master @egreg, with support for verbatim :) courtesy of the package scontents.

\documentclass{article}
\usepackage[print-env=true,store-env=important]{scontents}
\setlength{\parindent}{0pt}
\pagestyle{empty}
% Uncomment this to automatic repeat ...thanks @egreg :)
%\AtEndDocument{\section*{Important}
%\begin{enumerate}
%\foreachsc[before={\item }]{important}
%\end{enumerate}}

\begin{document}
\section{Important things}
Here I state something very important

\begin{scontents}
\textbf{Lorem Ipsum Principle.} Blah blah blah.
\end{scontents}

Here I state something very important

\begin{scontents}
\textbf{Lorem Ipsum Principle.} Tick Tack Tack.
\end{scontents}

Then another important thing

\begin{scontents}
\verb*|Ducks can fly?|.
\end{scontents}

\section{Repeat manually}

\getstored[1]{important}\\[10pt]
\getstored[2]{important}\\[10pt]
\getstored[3]{important}

\section{Repeat all}

\begin{enumerate}
\foreachsc[before={\item }]{important}
\end{enumerate}

\end{document}