I am trying to output some text in a file. I use filecontents
and it works nicely. However, if I put the filecontents
environment inside a macro, it only ouputs the comments but not the text when the macro is called.
Here is an example:
\documentclass{article}
\usepackage{filecontents}
\usepackage{etoolbox}
\begin{filecontents}{\jobname.abc}
this gets in the file
}
\end{filecontents}
\newcommand{\toto}{
\begin{filecontents}{\jobname.def}
this won't...
\end{filecontents}
}
\begin{document}
\toto
\end{document}
I also tried using the filecontents
in the \AtEndDocument
hook, but again, nothing is found in the file. To that end, I added in the preamble of the above code
\AtEndDocument{
\typeout{AtEndDocument is being executed}
\begin{filecontents}{\jobname.ghi}
this won't either...
\end{filecontents}
}
In all three cases, I have the warning saying that the file is created/replaced:
LaTeX Warning: Overwriting file xxx.ghi
Anyone has any clue what is going on? How this could be corrected so that contents gets to the files?
filecontents
is supposed to only be used in the preamble, so writing it at the end of the document seems likely to fail. You are probably better off writing to an external file directly (or using a specialized package). – jon Feb 23 '16 at 4:23\documentclass
. – cfr Feb 23 '16 at 12:28filecontents
package is usually recommended beyond the most simple applications.... – jon Feb 23 '16 at 18:47\RequirePackage{}
if necessary. It depends on the content of the file. – cfr Feb 23 '16 at 20:23filecontents
environement should only be used in the way you say.... – jon Feb 23 '16 at 22:34