# Is there a command to define a “virtual file” in a tex document

I wonder whether there is something like the filecontents environment, but without writing the actual data to the filesystem.

With the filecontents environment, I can write a texfile like this:

\documentclass{article}
\begin{filecontents}{myfile.tex}
Hello world!
\end{filecontents}
\begin{document}
\input{myfile.tex}
\end{document}


This compiles nicely, but creates the file myfile.tex in the working directory. I wonder whether there is an alternative way to get the same compilation result, but without creating the file myfile.tex in the file system?

Alternatively, is there a command that I can put after the \end{document} to have myfile.tex removed?

-
In ConTeXt buffers do exactly what you ask for. – Marco May 30 '13 at 12:18

I think luatex in principle allows you to use lua buffers in exactly the way you describe, but I don't know the details. If you do not have any verbatim material in the file you are writing out you can of course just use a macro

\documentclass{article}
\newcommand\mystuff{
Hello world!
}
\begin{document}
\mystuff
\end{document}


To remove a file at the end, if you execute latex with -enable-shell-escape you could use

\AtEndDocument{\write18{rm myfile.tex}}


or del or whatever command deletes files on your system, although it is probably just as easy to arrange the script or editor that is calling latex to simply clean up afterwards rather than rely on shell escape with latex.

-
I would use a \long\def so that he could use multi-paragraph definitions. – Steven B. Segletes May 30 '13 at 12:36
@StevenB.Segletes or even \newcommand :-) (answer edited) – David Carlisle May 30 '13 at 12:49
@StevenB.Segletes Doesn't apply here: it's arguments that cannot be long with \def, not the replacement text itself. – Joseph Wright May 30 '13 at 12:51
Defining a new macro / command works actually well for my case, thanks for that :) Seems like I had an XY-Problem... – silvado May 30 '13 at 12:52
@JosephWright Thanks. Not sure why I thought that was the case, but I appreciate the correction. – Steven B. Segletes May 30 '13 at 12:56