I TeX the following code with pdflatex --shell-escape but the compilation got paused because of an error as the produced prepare.tex does not contain \end{document}.
\documentclass{article}
\begin{filecontents*}{prepare.tex}
\documentclass{beamer}
\usepackage{graphicx}
\usepackage[active,tightpage,graphics]{preview}
\PreviewBorder=0pt
\begin{document}
\begin{frame}
\only<1>{\includegraphics{example-image-a}}
\only<2>{\includegraphics{example-image-b}}
\end{frame}
\end{document}
\end{filecontents*}
\usepackage{animate}
\begin{document}
\immediate\write18{pdflatex.exe prepare.tex}
\animategraphics{10}{prepare.pdf}{}{}
\end{document}
How to keep filecontents* from getting confused when it encloses \begin{document}..\end{document}?
Bonus question (rather than I create a new question for it): How can I force filecontents to override the specified file if it already exists?
Edit: Actually there is no problem with filecontents. The source of problem is the extension .pdf in the \animategraphics. Now unfortunately, the title is misleading.


filecontents. – Marco Daniel Jul 29 '12 at 7:59prepare.texfile as per your MWE. It gets created properly. Have a look at that file with a text editor. – Peter Grill Jul 29 '12 at 8:05mweis needed to get the graphicsexample-image-aandexample-image-b– Marco Daniel Jul 29 '12 at 8:19graphicxalready provides us with them out of the box I think. – Click Me Jul 29 '12 at 8:22mweinto the document but you need to have installedmwe(or at least extracted the images from the mwe distribution and placed them in the input path) – David Carlisle Jul 29 '12 at 9:29