# Hooking in before \documentclass

In the consequence of a previously asked question (Is it possible to hook in after options have been processed in a certain package or class?) I had the idea of a hook that adds code even before \documentclass is called. So sticking to the technique presented in the mentioned post one could try

\let\savedDC\documentclass
\renewcommand{\documentclass}[2][]{%
\typeout{my stuff here}
\savedDC[#1]{#2}
}


Or directly in Form of a hook macro:

\newcommand{\BeforeDocumentclass}[1]{%
\renewcommand{\documentclass}[2][]{%
#1
\savedDC[##1]{##2}
}}


Assuming these definitions were made after using \documentclass (Otherwise no hook is needed), this will fail due to the fact that they will have no actual effect on the use of \documentclass. Or in short: To define a hook that puts stuff before \documentclass you need just exactly the same hook. Is there a way/procedure to get rid of this recursivity?

Summerizing Edit:

From the comments arise two possible ways.

1) The filecontents package: From the docu I understood that the usage must be exactly the same as the "old" filecontents environment; i.e. it writes something into an external file which can be retrieved through input. Til yet I dont get how to write a macro that invoces the input command in order to get the desired result.

[Per memoriam: I try to figure out with this question how to write a macro like \BeforeDocumentclass{<sth>} or \begin{beforedocumentclass} <sth> \end{beforedocumentclass}] that moves the code before documentclass or maybe to the very beginning.

<sth>
\documentclass{someclass}
...
\BeforeDocumentclass{<sth>}
...


]

2) The initex mechanism: By using google I just found out that it is (somehow?) a alternative compiler; nowadays accessable through the -ini-option. But I could not find out how it does actually work. If anyone has a link to a documentation or anything similar, i would be very grateful.

-
You don't need a hook, just use \typeout{my stuff here}\documentclass{article} –  David Carlisle Aug 16 '13 at 18:06
And what if you layout stuff to external files? Moreover i'd like to know more about techniques. Here, especially about avoiding the recursivity. –  Ruben Aug 16 '13 at 18:27
Making the definitions after \documentclass is pointless; once TeX has read something it can't go back. This is why there are e.g. .aux and .toc files. –  You Aug 16 '13 at 18:30
@Ruben filecontents environment is designed to go before \documentclass and write an external file. –  David Carlisle Aug 16 '13 at 20:16
You might also want to explore \everyjob and \dump and the INITEX mechanism that create custom formats for TeX. They provide a way for TeX to do things before starting a job. –  Thruston Aug 17 '13 at 12:56

Create a rubenlatex.tex file such as

\makeatletter

\def\ruben@whatever{} % here is the hook, define it to suit

\let\latex@@documentclass\documentclass
\def\documentclass{\ruben@whatever\latex@@documentclass}

\makeatother
\dump


and then run from the command line

pdftex -ini '&pdflatex rubenlatex'


This will create a rubenlatex.fmt file that you can preload by calling

pdftex -fmt rubenlatex


Check the TeX Live documentation for how to store the rubenlatex.fmt format file in order to be readable when launching the engine from any directory.

Of course there are some problems with this approach; the first one is minor, the second is the real big one.

1. You need to create similarly formats for LaTeX in DVI output (if you need it), for XeLaTeX and LuaLaTeX

2. The solution is not portable; documents relying on the modified \documentclass cannot be distributed unless the recipient has the same set up for the new formats.

-