> 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-tw/wen-jian-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`匯入的檔案都有自己的 .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-tw/wen-jian-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}{引理}[section]

%定義計數器
\theoremstyle{definition}
\newtheorem{definition}{定義}[section]

%推論計數器
\newtheorem{corolary}{推論}[section]

%自然數、整數、拓撲、凸包、球、圓盤、維度、邊界以及其他一些命令
\newcommand{\E}{{\mathcal{E}}}
\newcommand{\F}{{\mathcal{F}}}
...

%範例環境
\theoremstyle{remark}
\newtheorem{examle}{範例}

%範例計數器
\newcommand{\reiniciar}{\setcounter{example}{0}}
```

這個檔案中的所有命令本來都可以放進導言區，但主文件會因為這麼大量的程式碼而變得難以閱讀，而且在這麼大的檔案中找到文件的實際本文也會是一項繁瑣的工作。

這個檔案也可以放在一般的 .tex 檔中，並使用以下命令匯入： `import` （請參見 [下一節](#importing-files)），但 .sty 檔可以避免檔案在不小心被匯入超過一次時產生錯誤。

請注意，範例的第一行是

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

這表示我們必須將此套件以 *範例* 的方式在主文件中匯入，也就是使用命令

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

如同 [引言](#introduction).

*注意：.sty 檔案彈性高得多，可用來定義自己的巨集，並且可以傳遞可選參數，請參見* [*撰寫你自己的套件*](/latex/zh-tw/lei-bie-dang/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{第一節}

以下是一個簡單的 3D 圖

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

[...]
```

如你所見，這個檔案匯入了一個 [pgf 圖形](/latex/zh-tw/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-tw/wen-jian-jie-gou/08-multi-file-latex-projects.md)
* [章節與方程式交叉參照](/latex/zh-tw/wen-jian-jie-gou/03-cross-referencing-sections-equations-and-floats.md)
* [索引](/latex/zh-tw/wen-jian-jie-gou/04-indices.md)
* [詞彙表](/latex/zh-tw/wen-jian-jie-gou/05-glossaries.md)
* [超連結](/latex/zh-tw/wen-jian-jie-gou/09-hyperlinks.md)
* [頁碼編號](/latex/zh-tw/ge-shi-hua/03-page-numbering.md)
* [單面與雙面文件](/latex/zh-tw/ge-shi-hua/08-single-sided-and-double-sided-documents.md)
* [多欄排版](/latex/zh-tw/ge-shi-hua/09-multiple-columns.md)
* [段落格式設定](/latex/zh-tw/ge-shi-hua/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [頁面大小與邊界](/latex/zh-tw/ge-shi-hua/07-page-size-and-margins.md)
* [計數器](/latex/zh-tw/ge-shi-hua/10-counters.md)
* [邊界註記](/latex/zh-tw/ge-shi-hua/15-margin-notes.md)
* [粗體、斜體與底線](/latex/zh-tw/latex-ji-chu/03-bold-italics-and-underlining.md)
* [字型大小、字族與樣式](/latex/zh-tw/zi-xing/01-font-sizes-families-and-styles.md)
* [字型字體](/latex/zh-tw/zi-xing/02-font-typefaces.md)
* [使用 XeLaTeX 支援現代字型](/latex/zh-tw/zi-xing/03-xelatex.md)
* [國際語言支援](/latex/zh-tw/yu-yan/03-international-language-support.md)
* [字型大小、字族與樣式](/latex/zh-tw/zi-xing/01-font-sizes-families-and-styles.md)
* [撰寫你自己的套件](/latex/zh-tw/lei-bie-dang/03-writing-your-own-package.md)
* [撰寫你自己的類別](/latex/zh-tw/lei-bie-dang/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://overleaf-pro.ayaka.space/latex/zh-tw/wen-jian-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.
