Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

\def\SetClass{article}
\documentclass{\SetClass}
\begin{document}
    This is some text.
\end{document}

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

share|improve this question
add comment

2 Answers 2

up vote 14 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
3  
Well, you can't use \begin{document} before \documentclass! –  Joseph Wright Nov 23 '11 at 9:44
add comment

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
add comment

Your Answer

 
discard

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.