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

  \typeout{my stuff here}

Or directly in Form of a hook macro:


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.



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.

  • 2
    You don't need a hook, just use \typeout{my stuff here}\documentclass{article} Aug 16, 2013 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, 2013 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, 2013 at 18:30
  • 1
    @Ruben filecontents environment is designed to go before \documentclass and write an external file. Aug 16, 2013 at 20:16
  • 1
    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, 2013 at 12:56

1 Answer 1


Create a rubenlatex.tex file such as


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



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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .