[Tex/LaTex] Collecting contents of LaTeX environment

content-replicationenvironments

I have the following for collecting contents of an environment and printing them later, it works but for my real task i want to use the environ package because i need to be able to have access to \BODY. How can i produce the same output with \NewEnviron instead of \newenvironment used?

\documentclass{article}
\usepackage{collect}
\usepackage{environ}

\definecollection{notes}


%works but not what i realy want i need access to \BODY and for that i cant  use \newenvironment command
\makeatletter
\newenvironment{note}
  {\@nameuse{collect}{notes}{\par\noindent}{\par}{}{}}
   {\@nameuse{endcollect}}
\makeatother

%what i realy want
%\NewEnviron{note}{
%\@nameuse{collect}{notes}{\par\noindent}{\par}{}{}
  %\BODY
%\@nameuse{endcollect}
%}


\begin{document}

\begin{note}
 First note
 \end{note}

\begin{note}
 Second note
 \end{note}

 \begin{note}
Third note
\end{note}

\section{collected}
\includecollection{notes}
\end{document}

Best Answer

If your collected notes are only needed at the end of the document, you can avoid writing out files: just use token registers.

\documentclass{article}
\usepackage{environ}

\newtoks\mainnotetoks
\newtoks\tempnotetoks
\newtoks\prenotetoks
\newtoks\postnotetoks

\NewEnviron{note}{%
  \tempnotetoks=\expandafter{\BODY}%
  \edef\notetemp{%
    \the\mainnotetoks % what was already stored
    \the\prenotetoks % text before the new note
    \the\tempnotetoks % the current note
    \the\postnotetoks % text after the new note
  }%
  % update \mainnotetoks
  \global\mainnotetoks=\expandafter{\notetemp}%
}
\newcommand\includenotes{\the\mainnotetoks}

% set the pre and post note
\prenotetoks={\par\noindent}
\postnotetoks={\par}

\begin{document}

Here we have a first note.
\begin{note}
First note
\end{note}

Here we have a second note.
\begin{note}
Second note
\end{note}

Here we have a third note.
\begin{note}
Third note
\end{note}

\section{collected}
\includenotes
\end{document}

enter image description here

Related Question