Can one define a bunch of macros, include them in a LaTeX source file
main.tex and after issuing
$ latex main.tex
find the preamble of main.tex in a file, say main.nem ?
It is a requirement, that the source main.tex does not have to
be touched by me.
So, I receive a file main.tex, that already includes the magic
macros, that I ask for in this question.
And all I have to do, is to compile the file and have the preamble in
a file.
The burden for the author of main.tex shall not be greater than
to include the file with the magic macros in them.
It is not allowed, to ask the author to use a special documentclass,
or have to write
\begin{filecontents}...
or some such into it. The only trace of me, using his file, is
\input{magic_macros.tex}
somewhere in his preamble.
I can do a somewhat analogous sort of thing with macros that I define, and that has proven to be extremely useful in my application domain.
A stripped down example of it is:
macros.tex:
\newwrite\nemWrite
\immediate\openout\nemWrite=\jobname.nem
\newtoks\nemToks
\newcommand{\toNem}[1]{%
\nemToks={#1}
\immediate\write\nemWrite{\the\nemToks}
}
\newcommand{\question}[1]{%
\toNem{#1}%
{#1}%
}
main.tex:
\documentclass{article}
\input{macros.tex}
\begin{document}
\question{%
What does $2+2$ evaluates to?
}
I was hoping that there exists something similar for the preamble.

\documentclassinto a file. – Peter Grill Oct 25 '12 at 16:15