|
| 1 | +--- |
| 2 | +layout: "lesson" |
| 3 | +lang: "zh-hant" |
| 4 | +title: "長文件結構" |
| 5 | +description: "本課展示了如何將 LaTeX 原始檔分割成更小、更易管理的檔案,以及這如何使構建長文件更容易和更快速。" |
| 6 | +toc-anchor-text: "原始檔結構" |
| 7 | +toc-description: "以可控的方式分割原始檔。" |
| 8 | +--- |
| 9 | + |
| 10 | +# 長文件結構 |
| 11 | + |
| 12 | +<script> |
| 13 | +runlatex.preincludes = { |
| 14 | + "pre0": { |
| 15 | + "pre1": "front.tex", |
| 16 | + "pre2": "pref.tex", |
| 17 | + "pre3": "chap1.tex", |
| 18 | + "pre4": "chap2.tex", |
| 19 | + "pre5": "append.tex", |
| 20 | + "pre6": "frontcover.tex", |
| 21 | + "pre7": "dedication.tex", |
| 22 | + "pre8": "copyright.tex", |
| 23 | + "pre9": "backcover.tex", |
| 24 | + } |
| 25 | +} |
| 26 | +</script> |
| 27 | + |
| 28 | +<span |
| 29 | + class="summary">本課展示了如何將 LaTeX 原始檔分割成更小、更易管理的檔案,以及這如何使構建長文件更容易和更快速。</span> |
| 30 | + |
| 31 | +當你在寫一個較長的文件時,你可能想要將原始檔分割成多個檔案。例如,對於一本書或論文,通常會有一個"主"/"根"檔案,然後每章一個原始檔(對於書或論文),或每個重要章節一個原始檔(對於長文章)。 |
| 32 | + |
| 33 | +## 結構化原始檔 |
| 34 | + |
| 35 | +LaTeX 允許我們以可控的方式分割原始檔。有兩個重要的命令:`\input`和`\include`。我們可以使用`\input`讓一個檔案"就像它被輸入在這裡一樣"工作,所以它可以用於(基本上)任何材料。`\include`命令只能用於章節:它開始新的一頁並進行一些內部調整。但它有一個很大的優勢:它允許我們選擇性地包含章節,所以你可以處理文件的一部分而不是整個文件。 |
| 36 | + |
| 37 | +因此,一個較長的文件可能看起來像這樣: |
| 38 | + |
| 39 | +<!-- pre0 {% raw %} --> |
| 40 | +```latex |
| 41 | +% !TEX program=lualatex |
| 42 | +
|
| 43 | +\documentclass{ctexbook} |
| 44 | +\usepackage{biblatex} |
| 45 | +\addbibresource{biblatex-examples.bib} |
| 46 | +
|
| 47 | +\title{一本示例書} |
| 48 | +\author{約翰·多伊 \and 喬·布洛格斯} |
| 49 | +
|
| 50 | +\IfFileExists{\jobname.run.xml} |
| 51 | +{ |
| 52 | +\includeonly{ |
| 53 | + front, |
| 54 | +% chap1, |
| 55 | + chap2, |
| 56 | +% append |
| 57 | + } |
| 58 | +} |
| 59 | +{ |
| 60 | +% 初始時做一個完整的文件以生成 |
| 61 | +% 所有的輔助檔案 |
| 62 | +} |
| 63 | +
|
| 64 | +\begin{document} |
| 65 | +\frontmatter |
| 66 | +\include{front} |
| 67 | +
|
| 68 | +% ========================= |
| 69 | +\mainmatter |
| 70 | +\include{chap1} |
| 71 | +\include{chap2} |
| 72 | +\appendix |
| 73 | +\include{append} |
| 74 | +
|
| 75 | +% ======================== |
| 76 | +\backmatter |
| 77 | +\printbibliography |
| 78 | +\newpage |
| 79 | +\input{backcover} |
| 80 | +\end{document} |
| 81 | +``` |
| 82 | +<!-- {% endraw %} --> |
| 83 | + |
| 84 | +我們將在下面檢視這個檔案的各個方面。(各個支援檔案在本頁底部。) |
| 85 | + |
| 86 | +## 使用`\input` |
| 87 | + |
| 88 | +`\input`命令適用於不是獨立章節的長檔案的部分。在示例中,我們用它來分離出前封面和後封面,保持主檔案簡短明了,同時也意味著我們可以在另一個文件中重用這些封面。我們也用它來處理文件開頭的"非章節"部分:像前言這樣的內容。這同樣是為了幫助保持主檔案清晰。 |
| 89 | + |
| 90 | +## 使用`\include`和`\includeonly` |
| 91 | + |
| 92 | +`\include`命令適用於章節,所以我們對每個完整的章節都使用了它;它總是開始新的一頁。我們已經使用`\includeonly`選擇了實際要排版的章節,如你所見,它接受一個逗號分隔的檔名列表。當你使用`\includeonly`時,你可以縮短排版時間並生成一個用於校對的"選擇性"PDF。此外,`\includeonly`的關鍵優勢是 LaTeX 將使用其他包含檔案的`.aux`檔案中的所有交叉引用資訊。 |
| 93 | + |
| 94 | +## 建立目錄 |
| 95 | + |
| 96 | +`\tableofcontents`命令使用章節命令中的資訊來填充目錄。它有自己的輔助檔案,副檔名為`.toc`,所以你可能需要執行 LaTeX 兩次來解析資訊。目錄是自動從章節標題生成的。還有類似的命令用於`\listoffigures`和`\listoftables`,它們分別從浮動環境的標題工作,並使用副檔名為`.lof`和`.lot`的檔案。 |
| 97 | + |
| 98 | +## 將文件分成部分 |
| 99 | + |
| 100 | +`\frontmatter`、`\mainmatter`和`\backmatter`命令影響格式。例如,`\frontmatter`將頁碼改為羅馬數字。`\appendix`命令將編號改為`A`、`B`等,所以例如在`\appendix`之後的第一章中,頁首顯示"附錄A"。 |
| 101 | + |
| 102 | +## 練習 |
| 103 | + |
| 104 | +嘗試演示文件的基本結構,嘗試新增和刪除`\includeonly`的條目,看看效果。 |
| 105 | + |
| 106 | +新增一些浮動體並生成圖表和表格列表。如果使用本地安裝的 LaTeX,你能看到需要多少次 LaTeX 執行嗎?(線上系統在"幕後"重新執行 LaTeX,所以額外需要的執行不那麼明顯。) |
| 107 | + |
| 108 | +---- |
| 109 | + |
| 110 | +### front.tex |
| 111 | +<!-- pre1 {% raw %} --> |
| 112 | +```latex |
| 113 | +\input{frontcover} |
| 114 | +\maketitle |
| 115 | +\input{dedication} |
| 116 | +\input{copyright} |
| 117 | +\tableofcontents |
| 118 | +\input{pref} |
| 119 | +``` |
| 120 | +<!-- {% endraw %} --> |
| 121 | + |
| 122 | +#### pref.tex |
| 123 | +<!-- pre2 {% raw %} --> |
| 124 | +```latex |
| 125 | +\chapter{前言} |
| 126 | +前言文字。參見\cite{doody}。 |
| 127 | +``` |
| 128 | +<!-- {% endraw %} --> |
| 129 | + |
| 130 | +#### chap1.tex |
| 131 | +<!-- pre3 {% raw %} --> |
| 132 | +```latex |
| 133 | +\chapter{引言} |
| 134 | +第一章文字。 |
| 135 | +``` |
| 136 | +<!-- {% endraw %} --> |
| 137 | + |
| 138 | +#### chap2.tex |
| 139 | +<!-- pre4 {% raw %} --> |
| 140 | +```latex |
| 141 | +\chapter{其他內容} |
| 142 | +第二章文字。 |
| 143 | +``` |
| 144 | +<!-- {% endraw %} --> |
| 145 | + |
| 146 | +#### append.tex |
| 147 | +<!-- pre5 {% raw %} --> |
| 148 | +```latex |
| 149 | +\chapter*{附錄} |
| 150 | +第一個附錄文字。 |
| 151 | +``` |
| 152 | +<!-- {% endraw %} --> |
| 153 | + |
| 154 | +#### frontcover.tex |
| 155 | +<!-- pre6 {% raw %} --> |
| 156 | +```latex |
| 157 | +\begin{center} |
| 158 | +前封面 |
| 159 | +\end{center} |
| 160 | +``` |
| 161 | +<!-- {% endraw %} --> |
| 162 | + |
| 163 | +#### dedication.tex |
| 164 | +<!-- pre7 {% raw %} --> |
| 165 | +```latex |
| 166 | +\begin{center} |
| 167 | +\large |
| 168 | +獻給 \ldots |
| 169 | +\end{center} |
| 170 | +``` |
| 171 | +<!-- {% endraw %} --> |
| 172 | + |
| 173 | +#### copyright.tex |
| 174 | +<!-- pre8 {% raw %} --> |
| 175 | +```latex |
| 176 | +\begin{center} |
| 177 | +版權所有 2020 learnlatex。 |
| 178 | +\end{center} |
| 179 | +``` |
| 180 | +<!-- {% endraw %} --> |
| 181 | + |
| 182 | +#### backcover.tex |
| 183 | +<!-- pre9 {% raw %} --> |
| 184 | +```latex |
| 185 | +\begin{center} |
| 186 | +後封面 |
| 187 | +\end{center} |
| 188 | +``` |
| 189 | +<!-- {% endraw %} --> |
| 190 | + |
| 191 | +---- |
0 commit comments