> 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/wen-dang-jie-gou/07-management-in-a-large-project.md).

# 大型项目中的管理

## 引言

在大型项目中，比如书籍，将文档的各个部分分别放在多个 .tex 文件中，可以更容易地纠正错误并进行后续修改。在短文件中更容易定位某个特定的单词或元素。为此，本文将介绍如何管理大型项目。

## 输入和包含文件

将一个 LaTeX 文件插入另一个文件中的标准工具有 `\input` 和 `\include`.

**input 命令**

\input{filename}

在文档主体中使用此命令插入另一个名为 `filename.tex`的文件内容；该文件不应包含任何 LaTeX 导言区代码（即不含 `\documentclass`, `\begin{document}` 或 `\end{document}` ）。LaTeX 在处理 `filename.tex`. `\input` 时不会开始新的一页 `\input` 命令，可以将其嵌套在根文件已经输入的文件中。

**include 命令**

\include{filename}

在文档主体中使用此命令插入另一个名为 `filename.tex`；同样，这个文件也不应包含任何 LaTeX 导言区。LaTeX 会在处理来自 `filename.tex`的内容之前先开始新的一页。请确保不要在文件名中包含扩展名， `.tex` 因为这会导致文件无法被输入（扩展名也可以选择性地与 `输入` 和 `导入`一起包含）。无法嵌套 `include` 命令。每个被 `\include`d 拥有自己的 .aux 文件，其中存储着已创建标签以及目录、图表目录等内容的信息。你可以使用 `\includeonly` 配合以逗号分隔的文件名列表（确保前后没有空格）。如果这样做，LaTeX 只会处理该列表中的文件。这可用于提高编译速度，尤其是在你只处理较大文档的一小部分时。不过，页码和交叉引用仍然会正常工作，因为未包含文件的 .aux 文件仍会被处理。

[在 Overleaf 上打开一个大型项目示例](https://www.overleaf.com/project/new/template/20618?id=70769185\&templateName=Managing+a+large+project+on+Overleaf\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 单独文件中的导言区

如果你的文档导言区中有许多用户自定义命令或术语表的术语定义， [术语表](/latex/zh-cn/wen-dang-jie-gou/05-glossaries.md)，你可以把它放在单独的文件中。正确的做法是创建一个自定义宏包，即一个扩展名为 .sty 的文件。来看一个例子：

```latex
\ProvidesPackage{example}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[latin1]{inputenc}
\usepackage[spanish, english]{babel}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{textcomp}
\usepackage{pgfplots}

\pgfplotsset{width=10cm,compat=1.9}

%页眉样式
\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textbf{\textit{\nouppercase{\leftmark}}}}
\fancyhead[LO]{\textbf{\textit{\nouppercase{\rightmark}}}}
\fancypagestyle{plain}{ %
\fancyhf{} % 移除所有内容
\renewcommand{\headrulewidth}{0pt} % 以及移除横线
\renewcommand{\footrulewidth}{0pt}}

%使 \proof、\qedsymbol 和 \theoremstyle 命令可用
\usepackage{amsthm}

%标尺
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}

%引理定义和引理编号器
\newtheorem{lemma}{Lemma}[section]

%定义编号器
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

%推论编号器
\newtheorem{corolary}{Corolary}[section]

%自然数、整数、拓扑、凸包、球、圆盘、维度、边界等的一些命令
\newcommand{\E}{{\mathcal{E}}}
\newcommand{\F}{{\mathcal{F}}}
...

%示例环境
\theoremstyle{remark}
\newtheorem{examle}{Example}

%示例编号器
\newcommand{\reiniciar}{\setcounter{example}{0}}
```

此文件中的所有命令本可以放在导言区，但由于代码量太大，主文件会因此变得混乱，而在这样的大文件中找到文档主体本身也会是一项繁琐的任务。

这个文件也可以放在普通的 .tex 文件中，并通过命令 `导入` 导入（见 [下一节](#importing-files)），但 .sty 文件可以防止文件被意外导入多次时出现潜在错误。

请注意，示例的第一行是

```latex
\ProvidesPackage{example}
```

这意味着我们必须将此宏包作为 *示例* 在主文件中导入，即使用命令

```latex
\usepackage{example}
```

，如 [引言](#introduction).

*注意：.sty 文件要灵活得多，它可用于定义你自己的宏，并且可以传递可选参数，参见* [*编写你自己的宏包*](/latex/zh-cn/lei-wen-jian/03-writing-your-own-package.md).

[在 Overleaf 上打开一个大型项目示例](https://www.overleaf.com/project/new/template/20618?id=70769185\&templateName=Managing+a+large+project+on+Overleaf\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 使用 import 宏包

如上所述，将一个 LaTeX 文件插入另一个文件中的标准工具有 `\input` 和 `\include`，但如果需要嵌套导入文件，就容易出错。因此，你可能需要考虑使用宏包 **导入**.

下面是一个书籍示例，其中各个章节和用户自定义命令存放在不同的文件中，而每个章节的图像文件也与该章节对应的 .tex 文件一起存放在单独的文件夹中。

```latex
\documentclass[a4paper,11pt]{book}
\usepackage{import}
\usepackage{example}

\usepackage{makeidx}
\makeindex

\begin{document}

\frontmatter
\import{./}{title.tex}

\clearpage
\thispagestyle{empty}

\tableofcontents

\mainmatter
\chapter{第一章}
\import{sections/}{section1-1.tex}
\import{sections/}{section1-2.tex}

\chapter{附加章节}
\import{sections/}{section2-1.tex}

\chapter{最后一章}
\import{sections/}{section3-1.tex}

\backmatter

\import{./}{bibliography.tex}

\end{document}
```

正如你所见，这个示例是一本包含三章和若干节的书，主文件整洁地调用外部文件来生成最终文档。book 文档类中的命令 `\frontmatter` 用于文档的前几页，该命令将页码样式设为罗马数字；命令 `\mainmatter` 会重置页码并将样式改为阿拉伯数字， `\backmatter` 则会禁用章节编号（适用于参考文献和附录）。

```latex
\chapter{第一章}
\import{sections/}{section1-1.tex}
\import{sections/}{section1-2.tex}
```

首先，将这一行添加到你文档的导言区：

```latex
\usepackage{import}
```

然后使用 `\import{ }{ }`。第一个花括号内的参数是文件所在目录，它可以是相对于当前工作目录的相对路径，也可以是绝对路径。第二个参数是要导入的文件名

另外还有命令 `\subimport` ，其语法相同，但如果在主文件中导入的某个文件里使用它，则路径将相对于该子文件。例如，下面是前一个示例中导入的 "section1-1.tex" 文件的内容：

```latex
\section{第一节}

下面是一个简单的三维绘图

\begin{figure}[h]
\centering
\subimport{img/}{plot1.tex}
\caption{标题}
\label{fig:my_label}
\end{figure}

[...]
```

正如你所见，这个文件导入了一个 [pgf 绘图](/latex/zh-cn/te-ding-ling-yu/08-pgfplots-package.md) 名为 "plot1.tex" 的文件，它创建了一个三维绘图。该文件是通过

```latex
\subimport{img/}{plot1.tex}
```

从 "img" 文件夹导入的，而该文件夹位于 "sections" 文件夹中。

如果 `\import` 被使用，那么路径 *img/* 就会相对于主文件，而不是相对于保存 "section1-1.tex" 的 "sections" 文件夹。

[在 Overleaf 上打开一个大型项目示例](https://www.overleaf.com/project/new/template/20618?id=70769185\&templateName=Managing+a+large+project+on+Overleaf\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 进一步阅读

更多信息请参见：

* [多文件 LaTeX 项目](/latex/zh-cn/wen-dang-jie-gou/08-multi-file-latex-projects.md)
* [章节和公式的交叉引用](/latex/zh-cn/wen-dang-jie-gou/03-cross-referencing-sections-equations-and-floats.md)
* [索引](/latex/zh-cn/wen-dang-jie-gou/04-indices.md)
* [术语表](/latex/zh-cn/wen-dang-jie-gou/05-glossaries.md)
* [超链接](/latex/zh-cn/wen-dang-jie-gou/09-hyperlinks.md)
* [页码编号](/latex/zh-cn/ge-shi-hua/03-page-numbering.md)
* [单面和双面文档](/latex/zh-cn/ge-shi-hua/08-single-sided-and-double-sided-documents.md)
* [多栏](/latex/zh-cn/ge-shi-hua/09-multiple-columns.md)
* [段落格式](/latex/zh-cn/ge-shi-hua/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [页面大小和页边距](/latex/zh-cn/ge-shi-hua/07-page-size-and-margins.md)
* [计数器](/latex/zh-cn/ge-shi-hua/10-counters.md)
* [边注](/latex/zh-cn/ge-shi-hua/15-margin-notes.md)
* [加粗、斜体和下划线](/latex/zh-cn/latex-ji-chu/03-bold-italics-and-underlining.md)
* [字体大小、字体族和样式](/latex/zh-cn/zi-ti/01-font-sizes-families-and-styles.md)
* [字体字形](/latex/zh-cn/zi-ti/02-font-typefaces.md)
* [使用 XeLaTeX 支持现代字体](/latex/zh-cn/zi-ti/03-xelatex.md)
* [国际语言支持](/latex/zh-cn/yu-yan/03-international-language-support.md)
* [字体大小、字体族和样式](/latex/zh-cn/zi-ti/01-font-sizes-families-and-styles.md)
* [编写你自己的宏包](/latex/zh-cn/lei-wen-jian/03-writing-your-own-package.md)
* [编写你自己的类](/latex/zh-cn/lei-wen-jian/04-writing-your-own-class.md)
* [LaTeX2ε 不那么简短的介绍](http://www.ctan.org/tex-archive/info/lshort/)
* [`导入` 包文档](ftp://sunsite.icm.edu.pl/pub/CTAN/macros/latex/contrib/import/import.pdf)


---

# 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/wen-dang-jie-gou/07-management-in-a-large-project.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.
