> 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/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` ，因为这会导致该文件无法被输入（扩展名可以通过 `输入` 和 `import`）来可选地包含。无法嵌套 `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}

%Header styles
\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{} % remove everything
\renewcommand{\headrulewidth}{0pt} % remove lines as well
\renewcommand{\footrulewidth}{0pt}}

%makes available the commands \proof, \qedsymbol and \theoremstyle
\usepackage{amsthm}

%Ruler
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}

%Lemma definition and lemma counter
\newtheorem{lemma}{Lemma}[section]

%Definition counter
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

%Corolary counter
\newtheorem{corolary}{Corolary}[section]

%Commands for naturals, integers, topology, hull, Ball, Disc, Dimension, boundary and a few more
\newcommand{\E}{{\mathcal{E}}}
\newcommand{\F}{{\mathcal{F}}}
...

%Example environment
\theoremstyle{remark}
\newtheorem{examle}{Example}

%Example counter
\newcommand{\reiniciar}{\setcounter{example}{0}}
```

这个文件中的所有命令本可以放在导言区中，但由于代码量太大，主文件会变得很混乱，而在如此大的文件中定位文档的实际正文也会是一项繁琐的任务。

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

请注意示例中的第一行是

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

这意味着我们必须将该宏包按如下方式导入： *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`，但如果需要嵌套导入文件，这些方法容易出错。因此，你可能要考虑使用宏包 **import**.

下面是一个书籍示例，其中各章节和用户自定义命令分别存放在不同文件中，而每章的图片文件与该章对应的 .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{First chapter}
\import{sections/}{section1-1.tex}
\import{sections/}{section1-2.tex}

\chapter{Additional chapter}
\import{sections/}{section2-1.tex}

\chapter{Last chapter}
\import{sections/}{section3-1.tex}

\backmatter

\import{./}{bibliography.tex}

\end{document}
```

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

```latex
\chapter{First chapter}
\import{sections/}{section1-1.tex}
\import{sections/}{section1-2.tex}
```

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

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

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

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

```latex
\section{First section}

下面是一个简单的 3D 图

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

[...]
```

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

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

从 "sections" 文件夹中的 "img" 文件夹导入。

如果 `\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/)
* [`import` 包文档](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://ayakaleaf-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.
