FormaTeX
FatalDocument Structure

Preamble-Only Command Used in Body

! LaTeX Error: Can only be used in preamble.

What this error means

A command that can only appear in the preamble (before \begin{document}) was used inside the document body. Common offenders are \documentclass, \usepackage, and \pagestyle.

Common causes

  • \usepackage or \documentclass placed after \begin{document}
  • \pagestyle{} used in the body instead of the preamble
  • Preamble commands accidentally placed at the end of the file

How to fix it

Move the offending command to the preamble, before \begin{document}. If you need to change page style mid-document, use \thispagestyle{} instead of \pagestyle{}.

Code examples

Causes the error
\documentclass{article}
\begin{document}

\usepackage{graphicx}  % Too late! Must be in preamble

\includegraphics{image.png}

\end{document}
Fixed
\documentclass{article}
\usepackage{graphicx}   % Correct: in preamble

\begin{document}

\includegraphics{image.png}

\end{document}

Related errors

Test the fix live

Paste your LaTeX into the playground and compile instantly.

ملاحظة سريعة

نتتبع الاستخدام المجهول — مشاهدات الصفحات واستخدام الميزات وأحداث الترجمة — لفهم ما يعمل وما لا يعمل. بدون إعلانات، بدون بيانات شخصية، بدون مشاركة مع أطراف ثالثة.