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.
Anybody knows how to do this?
Kjetil Halvorsen
\usepackage{verbatim}
\verbatiminput{filename}
--
The sooner you start to code, the longer the program will take.
--- Roy Carlson
You can use filecontents to create a file and \verbatiminput to read it
back in. I don't believe TeX has intrinsic support for appending to
an existing file, however. You'd need to either open a file and leave
it open for the duration of the session or create one file per code
example and concatenate them outside of LaTeX. (I'd recommend the
latter.)
-- Scott
>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