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 checkfancyvrbthat provides a way to write verbatim to another file. – egreg Aug 27 '13 at 09:34filecontentspackage, and i'm not sure what actually is wrong with flodd's use of the command. what is the rule that prevents such use? – wasteofspace Aug 27 '13 at 10:07VerbatimOutmuch more flexible. – egreg Aug 27 '13 at 10:12MyNewEnvironment? 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. – Heiko Oberdiek Aug 27 '13 at 10:12newenvironmentworks :) – FloDD Aug 27 '13 at 10:31