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.
\documentclass{article}
\begin{document}
Results are in the table.
\usepackage{booktabs}
\begin{tabular}{ll}
\toprule
A & B \\
\bottomrule
\end{tabular}
\end{document}\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}.
\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}\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.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\DeclareMathOperator{\Var}{Var}
$\Var(X) = \sigma^2$
\end{document}\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
- LaTeX Error: Something's wrong--perhaps a missing \itemA list has text before its first \item, or no items at all; an empty thebibliography is a common case. Start every list with \item.
- LaTeX Error: Lonely \item--perhaps a missing list environmentAn \item appears outside any list: the \begin{itemize} or \begin{enumerate} is missing, or the list was closed too early.
- LaTeX Error: \include cannot be nestedA file loaded with \include contains another \include. Only the main file may \include; inside an included file use \input instead.
- Package hyperref Warning: Token not allowed in a PDF string (<encoding>):A heading contains math or a command that cannot go into a PDF bookmark. The page is fine; give the bookmark plain text with \texorpdfstring.
Every example on this page was compiled with pdfLaTeX to check that it fails and is fixed as described. All LaTeX errors