> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/latex/zh-cn/latex-cuo-wu/13-latex-error-can-be-used-only-in-preamble.md).

# LaTeX 错误：只能在导言区使用

当你在文档主体中而不是在导言区包含了某个宏包或类时，就会出现此错误。文档的导言区由在此之前写入的所有内容组成。 **`\begin{document}`**。在导言区中，你定义所写文档的类型、语言以及其他一些元素。例如，普通文档的导言区会像这样：

```latex
\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\title{First document}
\author{Hubert Farnsworth \thanks{funded by the ShareLaTeX team}}
\date{February 2014}
```

如果你写： **`\usepackage{...}`** 在文档主体中（在……之后） **`\begin{document}`**）而不是在导言区中，你就会产生下面显示的错误信息。

main.tex，第 7 行

LaTeX 错误：只能在导言区中使用。

请参阅 LaTeX 手册或 LaTeX Companion 获取解释。输入 H 可立即获得帮助。... l.4 \usepackage {amsmath} 你的命令已被忽略。输入 I 可将其替换为另一个命令，或继续而不使用它。\[1

要修复此错误，请确保所有 **`\usepackage{...}`** 命令，以及 **`\documentclass[...]{...}`**，都写在……之前 **`\begin{document}`**。要了解更多关于如何在 LaTeX 中组织文档的信息，请查看我们的 [文档](/latex/zh-cn/latex-ji-chu/01-learn-latex-in-30-minutes.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ayakaleaf-pro.ayaka.space/latex/zh-cn/latex-cuo-wu/13-latex-error-can-be-used-only-in-preamble.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
