LaTeX Error: Can be used only in preamble

A preamble-only command such as \usepackage or \documentclass appears after \begin{document}. Move it above \begin{document}.

Help centre › LaTeX errors › Document structure · Error: the build stops or the output is wrong

What the log says

! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.5 \usepackage{booktabs}

What it means

Some commands set the document up and are allowed only between \documentclass and \begin{document}: \usepackage, \documentclass itself, \DeclareMathOperator, \newtheorem in some classes, and others. LaTeX found one of them after \begin{document}, where it can no longer take effect.

Causes and fixes

Most common first. Each example is a complete document; the first fails with this message, the second is the same document fixed.

1. \usepackage was added in the body

Move the line above \begin{document}. This happens most when a package is added next to the table or figure that needs it.

Fails
\documentclass{article}
\begin{document}
Results are in the table.

\usepackage{booktabs}
\begin{tabular}{ll}
\toprule
A & B \\
\bottomrule
\end{tabular}
\end{document}
Fixed
\documentclass{article}
\usepackage{booktabs}
\begin{document}
Results are in the table.

\begin{tabular}{ll}
\toprule
A & B \\
\bottomrule
\end{tabular}
\end{document}

2. An \input file has its own preamble

A section written as a stand-alone document brings its \documentclass, \usepackage lines and \begin{document} along when it is \input. Remove them from the section file (move any packages it needs into the main preamble) and keep only the body text; also remove its \end{document}.

Fails
\begin{filecontents*}[overwrite]{methods.tex}
\documentclass{article}
\begin{document}
Samples were dried at 60 degrees.
\end{filecontents*}
\documentclass{article}
\begin{document}
\section{Methods}
\input{methods}
\end{document}
Fixed
\begin{filecontents*}[overwrite]{methods.tex}
Samples were dried at 60 degrees.
\end{filecontents*}
\documentclass{article}
\begin{document}
\section{Methods}
\input{methods}
\end{document}

3. A setup command is placed in the body

\DeclareMathOperator, \geometry options that change the paper, \newtheorem with some classes and similar setup commands go in the preamble. Move the definition up; the command it defines is then usable everywhere in the body.

Fails
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\DeclareMathOperator{\Var}{Var}
$\Var(X) = \sigma^2$
\end{document}
Fixed
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\Var}{Var}
\begin{document}
$\Var(X) = \sigma^2$
\end{document}

In Manulyx

When the line is a \usepackage written after \begin{document}, the editor offers a one-click Fix that moves it just above \begin{document}.

Write in Manulyx for free · Journal templates

More in document structure

Every example on this page was compiled with pdfLaTeX to check that it fails and is fixed as described. All LaTeX errors