TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 15 down vote accepted

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.

share|improve this answer
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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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