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?


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.

  • 4
    Well, you can't use \begin{document} before \documentclass! – Joseph Wright Nov 23 '11 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.


I just came to this because I saw the same error. Instead of telling (xe)latex to process my .tex file, I had told it to process my .sty file.

I'm sure nobody else would do a stupid thing like that...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.