[Tex/LaTex] How to write the preamble into a file

external filespreamble

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.

Best Answer

You can use the standalone package to extract the preamble for you. Given a complete document as in the main.tex below, you run pdflatex on mainPreamble.tex which is defined as follows:

mainPreamble.tex:

\documentclass{article}
\usepackage[subpreambles=true]{standalone}

\begin{document}
    \input{main}
    \typeout{Sub preamble is now in \jobname.sta.}
    Sub preamble is now in \jobname.sta.
\end{document}

This extracts the preamble to the file mainPreamble.sta. Then in you other document where you need to use this preamble you simply use:

\UsePackage{main}

and the extracted preamble from main.tex is used. So, the MWE below yields:

enter image description here

which used the definition of question as originally defined in main.tex.

Note:

  • This required me to do some hacking in \UsePackage as I am not that familiar with this particular usage of the standalone package. So, perhaps that can be simplified, but I believe this does what you require.

main.tex:

%% This is main.tex
\documentclass{article}
%% This is the preamble of main.tex
\usepackage{amsmath}

\newcommand{\question}[1]{%
    \textbf{Question:}~#1%
}

\begin{document}
\question{%
    What does $2+2$ evaluates to?
}

Code:

Following is to be run only after compiling mainPreamble.tex as it needs mainPreamble.sta:

\documentclass{article}

\newcommand*{\UsePackage}[1]{%
    \let\standalonepreambles\relax
    \let\endstandalonepreambles\relax
    \newcommand{\subpreamble}[1]{}
    \let\endsubpreamble\relax
    \input{#1Preamble.sta}
}%

\UsePackage{main}

\begin{document}
    \question{%
        What does $1+1$ evaluates to?
    }
\end{document}
Related Question