On 27 May 2003 19:12:27 -0700, kje
...@entelnet.bo (Kjetil Halvorsen)
wrote:
>Hola!
>I want to have an environment like
>\begin{verbatim}
>\end{verbatim}
>which in addition to typeset the content writes (appends)
>the content to a file, so that I can have all my code examples
>from my document in a separate text file.
>I should also have a related environment which only appends to the
>file, without typesetting the content.
The verbatim package (in the tools bundle) is accompanied by a
file verbtest.tex that contains the following environment:
\newenvironment{verbatimwrite}[1]%
{\@bsphack
\immediate\openout \verbatim@out #1
\let\do\@makeother\dospecials\catcode`\^^M\active
\def\verbatim@processline{%
\immediate\write\verbatim@out{\the\verbatim@line}}%
\verbatim@start}%
{\immediate\closeout\verbatim@out\@esphack}
Note that it opens and closes a file specified as the
argument. If you want to append only, you should allocate
an output stream:
\newwrite\myoutfile
then open the file
\immediate\openout\myoutfile=myfile.tmp
then modify the above code to:
\newenvironment{myverbatimwrite}%
{\@bsphack
\let\do\@makeother\dospecials\catcode`\^^M\active
\def\verbatim@processline{%
\immediate\write\myoutfile{\the\verbatim@line}}%
\verbatim@start}%
{\@esphack}
Usually LaTeX can be relied upon to close the file at the end, but
in certain circumstances you may want to read it back in. If so,
close it when you are done with it, using
\immediate\closeout\myoutfile.
(Note: opening a preexisting file for output destroys what was in
it before.)
The above will only write to a file. To make it also print the material
in verbatim mode, modify the above replacing the definition of
\verbatim@processline with:
\def\verbatim@processline{%
\immediate\write\myoutfile{\the\verbatim@line}%
\the\verbatim@line\par}%
(Caveat: All code is modulo typing errors, none has been tested.)
Dan
--
Dan Luecking Department of Mathematical Sciences
University of Arkansas Fayetteville, Arkansas 72701