filecontents (LaTeX2e unofficial reference manual (November 2018))

Next: flushleft, Previous: figure, Up: Environments [Contents][Index]

### 8.11 `filecontents`

Synopsis:

    \begin{filecontents}{filename}
      text
    \end{filecontents}

or

    \begin{filecontents*}{filename}
      text
    \end{filecontents*}

Create a file named filename and fill it with text. The unstarred version of the environment `filecontents` prefixes the content of the created file with a header; see the example below. The starred version `filecontents*` does not include the header.

This environment can be used anywhere in the preamble, although it often appears before the `\documentclass` command. It is typically used when a source file requires a nonstandard style or class file. The environment will write that file to the directory containing the source and thus make the source file self-contained. Another use is to include `bib` references in the file, again to make it self-contained.

The environment checks whether a file of that name already exists and if so, does not do anything. There is a `filecontents` package that redefines the `filecontents` environment so that instead of doing nothing in that case, it will overwrite the existing file.

For example, this document

    \documentclass{article}
    \begin{filecontents}{JH.sty}
    \newcommand{\myname}{Jim Hef{}feron}
    \end{filecontents}
    \usepackage{JH}
    \begin{document}
    Article by \myname.
    \end{document}

produces this file `JH.sty`.

    %% LaTeX2e file `JH.sty'
    %% generated by the `filecontents' environment
    %% from source `test' on 2015/10/12.
    %%
    \newcommand{\myname}{Jim Hef{}feron}

Unofficial LaTeX2e reference manual