I am using the fancyvrb
package. I'd like to create a verbatim-like environment that does the following two things:
1) Append the verbatim material as-is to a file that has already been opened.
2) Output the verbatim material like UseVerbatim
would do.
I have tried to re-use the SaveVerbatim
environment and to output the material with \immediate\write\FILE{\UseVerbatim{VERB}}
but this doesn't as \UseVerbatim
needs more than expansion. Adding \protect
in front of \UseVerbatim
didn't help either.
A non-working example follows:
\documentclass{article}
\usepackage{fancyvrb}
\newwrite\FILE
\immediate\openout\FILE=\jobname.out
\newenvironment{chunk}{%
\VerbatimEnvironment{chunk}\begin{SaveVerbatim}[gobble=2,commentchar=\%,commandchars=\\\{\}]{VERB}%
}{%
\end{SaveVerbatim}%
\UseVerbatim{VERB}%
\immediate\write\FILE{\UseVerbatim{VERB}}%
}
\begin{document}
\begin{chunk}
First line.
\texttt{Second} line.
\end{chunk}
\end{document}
fancyvrb
but I don't think it is a good idea to reinvent the wheel.\documentclass{...}
and ending with\end{document}
.