When looking in books or at sample code, I always see \documentclass appearing first. Is that absolutely necessary?

I have created some test code which seems to compile fine:

    This is some text.

Is there any problem with placing other items, such as \input, \usepackage, or such details before the \documentclass?


2 Answers 2


There aren't really problems and in fact some techniques use input before \documentclass, for example to set a conditional. One has to be careful not to define commands that can be later defined by the class or a package via \newcommand (for instance \large).

Of course it would have little sense to set the text width or the text height before \documentclass, but it's not disallowed: simply it won't work.

You can even say \input{file}, as long as the code in file.tex doesn't break the "rule" stated above.

The only command that is formally disallowed before \documentclass is \usepackage, as pointed out by doncherry. Other commands just can't because they would be nonsensical (\begin{document} is an example).

Of course, no text producing command should go before \documentclass, nor any definition or measurement that relies on document properties such as the default font and size, which are set later. But one can say \AtBeginDocument.

  • 5
    Well, you can't use \begin{document} before \documentclass!
    – Joseph Wright
    Nov 23, 2011 at 9:44

You can't use \usepackage before \documentclass, you'll get an error. There are a few packages recommended to be loaded before it, though, e.g. nag and hyphsubst; you have to use \RequirePackage for them.

You must log in to answer this question.

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