> For the complete documentation index, see [llms.txt](https://overleaf-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://overleaf-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{第一份文档}
\author{Hubert Farnsworth \thanks{由 ShareLaTeX 团队资助}}
\date{2014年2月}
```

如果你写 **`\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://overleaf-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.
