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 themain.tex
below, you runpdflatex
onmainPreamble.tex
which is defined as follows:mainPreamble.tex
:This extracts the preamble to the file
mainPreamble.sta
. Then in you other document where you need to use this preamble you simply use:and the extracted preamble from
main.tex
is used. So, the MWE below yields:which used the definition of
question
as originally defined inmain.tex
.Note:
\UsePackage
as I am not that familiar with this particular usage of thestandalone
package. So, perhaps that can be simplified, but I believe this does what you require.main.tex
:Code:
Following is to be run only after compiling
mainPreamble.tex
as it needsmainPreamble.sta
: