I would like to create an environment foo
and a command \listfoo
so that:
- The content of the
foo
environment is hidden; and - The command
\listfoo
lists all the text that was contained in afoo
environment.
In other words, the following code:
\documentclass{article}
\usepackage{lipsum}
\newenvironment{foo}{% Some code here
}
{% Some code here
}
\newcommand{\listfoo}{
% Some code to list the content of the `foo` environments.
}
\begin{document}
\lipsum[1]
\begin{foo}
\bf Foo text 1
\end{foo}
\lipsum[2]
\begin{foo}
\bf Foo text 2
\end{foo}
\lipsum[3]
\begin{foo}
\bf Foo text 3
\end{foo}
\listfoo
\end{document}
should produce the following output:
Any suggestions for how to proceed would be much appreciated. Thanks!
답변1
This uses a macro \foocollect
and \NewEnviron
instead of \newenvironment
, the \foocollect
expands the previous \foocollect
versions and then the local \BODY
of the current environment.
\documentclass{article}
\usepackage{lipsum}
\usepackage{environ}
\def\foocollect{}
\NewEnviron{foo}{% Some code here
\xdef\foocollect{\expandafter\unexpanded\expandafter{\foocollect}\par\expandafter\unexpanded\expandafter{\BODY}}%
}[% Some code here
]
\newcommand{\listfoo}{%
\foocollect%
% Some code to list the content of the `foo` environments.
}
\begin{document}
\lipsum[1]
\begin{foo}
\bfseries Foo text 1
\end{foo}
\lipsum[2]
\begin{foo}
\bfseries Foo text 2
\end{foo}
\lipsum[3]
\begin{foo}
\bfseries Foo text 3
\end{foo}
\listfoo
\end{document}
Update with storing to a file instead of memory
\documentclass{article}
\usepackage{lipsum}
\usepackage{environ}
%\usepackage{morewrites}% only if you run out of filehandles
\newwrite\collectedcontentfile
\AtBeginDocument{%
% Automatically open the file at the beginning of the document
\immediate\openout\collectedcontentfile=\jobname.cll
}
\NewEnviron{foo}{% Some code here
\immediate\write\collectedcontentfile{%
\expandafter\unexpanded\expandafter{\BODY}^^J
}%
}[% Some code here
]
\newcommand{\listfoo}{%
%Closing the file
\immediate\closeout\collectedcontentfile%
\InputIfFileExists{\jobname.cll}{}{}
}
\begin{document}
\lipsum[1]
\begin{foo}
\bfseries Foo text 1
\end{foo}
\lipsum[2]
\begin{foo}
\bfseries Foo text 2
\end{foo}
\lipsum[3]
\begin{foo}
\bfseries Foo text 3
\end{foo}
\listfoo
\end{document}
답변2
An alternative using the scontents
package:
\documentclass{article}
\usepackage{lipsum}
\usepackage{scontents}
\newenvsc{foo}[store-env=foopost,print-env=false]
\begin{document}
\lipsum[1]
\begin{foo}
\textbf{Foo text 1
\end{foo}
\lipsum[2]
\begin{foo}
\textbf{Foo text 2}
\end{foo}
\lipsum[3]
\begin{foo}
\textbf{Foo text 3}
\end{foo}
\noindent
\foreachsc[after={\\}]{foopost}
\end{document}