How can one write the body of an environment verbatim to an external file. I tried the following but get problems if the body contains undefined macros (error) or e.g. a % (disappears in the output).

Try 1
Using environ to get the environment content.



% counter for external files


    \typeout{Name: \TF@outFile}
    \typeout{\TF@outFile\space opened}
    \typeout{\TF@outFile\space written}
    \typeout{\TF@outFile\space closed}

% test.tex



That's {a short Test}
which works
but ignores line breaks


\xx That's {another Test}%
This one doesn't work ...
it gives an error because \xx is undefined

This one creates the two desired files ext-test-1.tex and ext-test-1.tex but linebreaks are ignored and an undefined control sequence (\xx) causes an error. Furthermore the percent char is interpreted as comment not as simple character. I guess come catcode magic could help.

Try 2
Using the filecontents package.



% counter for external files



Same test file as above ...

This one creates only the first file ext-test-1.tex and the an error occurs because the end of filecontens can’t be determined correctly.

Is there another way do do this, maybe a package?

  • 1
    The reason why the filecontents solutions doesn't work is that it looks for \end{filecontents} verbatim and can't find it inside \end{tofile}. In general you can't include verbatim macros or environments in other macros or environments. Dec 10, 2011 at 11:43
  • @MartinScharrer: Yea, I thought so …
    – Tobi
    Dec 10, 2011 at 14:58
  • have a look at the answers package too
    – cmhughes
    Dec 10, 2011 at 16:14
  • About writing % into a file see also tex.stackexchange.com/q/34390/6865
    – Stephen
    Dec 10, 2011 at 17:13
  • Depends on the use case, scontents or filecontentsdef to store the content in memory instead of an external file may be better. //// The filecontents environment is actually extensible, but you need \filecontents ... \endfilecontents -- example. Also the starred version does not include the header -- see section 8.11 (page 62) of latex2e.pdf.
    – user202729
    Nov 11, 2021 at 7:35

2 Answers 2


You can look at the definition of verbatimwrite in verbatim.sty or use the facilities provided by the fancyvrb package:


  {\typeout{Writing file #1}\VerbatimOut{#1}}



Instead of \typeout you can do whatever you want (for example, checking whether the file already exists with \IfFileExists).

  • @MartinScharrer, egreg: Thank you both seems like both answers math my questions. Could you pleas figure out the differences if there are any?
    – Tobi
    Dec 10, 2011 at 14:59
  • @Tobi A very late answer, but it seems I missed seeing your comment. What are the differences? Well, listings loads fancyvrb, first of all. However, the definition of \lst@BeginWriteFile is different from \VerbatimOut, since many more settings are necessary for supporting the full power of listings. If you're only interested in verbatim copy, \VerbatimOut is more efficient.
    – egreg
    Feb 2, 2013 at 16:28

It is possible to use internal macros from the listings package to do this.



  % some code before
  % including \lstset{..}
  % Write file to given filename
  \lst@EndWriteFile% closes output file

% ...    

Compare with the similar code I provided in my answers to the following questions:
LaTeX documentation with verbatim environment
How to use a \write command inside a \newenvironment ?

  • Hi, is it possible to remove an indention (white space at the beginning of the line) while writing the file?
    – Tobi
    Nov 10, 2014 at 22:55
  • @Tobi: Have a look at listings gobble option. Not sure if it works with writting files. Nov 11, 2014 at 17:43
  • Hm … Seems like the gobble function is hard wired with the user level macros and can’t be used with \lst@BeginWriteFile
    – Tobi
    Nov 13, 2014 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.