I need to write some inlined code on an external file, then load it with listings
. I would like to define a new environment, but if I do, filecontents
does not save the first line of code (see MWE):
\documentclass[]{article}
\usepackage{listings}
\usepackage{filecontents}
\newenvironment{MyNewEnvironment}[2]{%
\def\temp{#1}%
\csname filecontents*\endcsname{\temp}%
#2%
}%
{\csname endfilecontents*\endcsname%
\lstinputlisting{\temp}%
}%
\begin{document}
\begin{MyNewEnvironment}{foo.txt}%
one
two
three
four
five
\end{MyNewEnvironment}
\end{document}
The resulting foo.txt contains:
two
three
four
five
filecontents*
like that; I suggest you to checkfancyvrb
that provides a way to write verbatim to another file.filecontents
package, and i'm not sure what actually is wrong with flodd's use of the command. what is the rule that prevents such use?VerbatimOut
much more flexible.MyNewEnvironment
? It causes the trouble, because it is not read in the control of\filecontents*
with its changed catcodes and line ends. Removing the second argument solves your problem as shown in karlkoeller's answer.newenvironment
works :)