> 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/texlive/zh-tw/latex-ji-chu/30-fen-zhong-xue-hui-latex.md).

# 30 分鐘學會 LaTeX

> **新增：** 前往全新的 [Overleaf 學習中心](https://learn.overleaf.com/) 以探索免費線上課程與直播網路研討會——掌握 LaTeX 並充分發揮 Overleaf 的所需一切。

這個入門教學不假設您具備任何 LaTeX 經驗，但希望當您完成時，不僅已撰寫了第一份 LaTeX 文件，也已獲得足夠的知識與信心，能邁出通往 LaTeX 熟練度的下一步。

## 什麼是 LaTeX？

LaTeX（發音為「*LAY*-tek」或「*LAH*-tek」）是一個用於排版專業外觀文件的工具。然而，LaTeX 的運作方式與您可能用過的許多其他文件製作應用程式（例如 Microsoft Word 或 LibreOffice Writer）大不相同：那些「[所見即所得](https://en.wikipedia.org/wiki/WYSIWYG)」工具提供使用者一個互動式頁面，讓他們在其中輸入與編輯文字並套用各種格式樣式。LaTeX 的運作方式則非常不同：您的文件其實是一個純文字檔，其中穿插著 LaTeX *指令* 以表達所需的（排版後）結果。要產生可見的排版文件，您的 LaTeX 檔案會由一個稱為 *TeX 引擎* ，它會使用嵌入在文字檔中的指令來引導並控制排版過程，將 LaTeX 指令與文件文字轉換成專業排版的 PDF 檔案。這表示您只需要專注於文件的 *內容* ，而電腦會透過 LaTeX 指令與 TeX 引擎，負責 *視覺外觀* （格式設定）。

## 為什麼要學 LaTeX？

關於學習使用 LaTeX 取代其他文件撰寫應用程式，可提出支持與反對的各種理由；但最終，這是一個基於偏好、契合度與文件需求的個人選擇。

支持使用 LaTeX 的理由包括：

* 支援排版極其複雜的數學、表格與物理科學技術內容；
* 提供註腳、交互參照與參考文獻管理功能；
* 能輕鬆產生複雜或繁瑣的文件元素，例如索引、術語表、目錄、圖表清單；
* 由於其內建的可程式化與可透過成千上萬的延伸套件擴充，因此可高度客製化，適合量身打造的文件製作 [免費附加套件](https://www.ctan.org/pkg).

總體而言，LaTeX 讓使用者對文件製作擁有相當大的控制權，而這些文件會以極高標準排版。當然，有些文件類型或出版品並不適合 LaTeX，包括許多雜誌類出版品常見的「自由形式」版面設計。

LaTeX 的一項重要優點是將文件內容與文件樣式分離：一旦撰寫好內容，外觀便能輕鬆變更。同樣地，您也可以建立一個定義特定文件類型版面／樣式的 LaTeX 檔案，而該檔案可作為 *範本* 以標準化該類型其他文件的撰寫／製作；例如，這讓科學出版商能以 LaTeX 建立文章範本，作者可使用這些範本撰寫投稿期刊的論文。Overleaf 也有一個 [含有數千個範本的圖庫](https://www.overleaf.com/gallery)，涵蓋範圍極廣的文件類型——從科學文章、報告與書籍，到履歷與簡報皆有。由於這些範本定義了文件的版面與樣式，作者只需在 Overleaf 中開啟它們——建立新專案——並開始撰寫以加入自己的內容。

## 撰寫您的第一段 LaTeX

第一步是建立一個新的 LaTeX 專案。您可以在自己的電腦上透過建立新的 `.tex` 檔案；或者，您可以 [在 Overleaf 中開始一個新專案](broken://pages/e074527bb27423eb00f1d62a6508ed3fe3daaea7).

讓我們從最簡單的可運作範例開始，您可以直接在 Overleaf 中開啟它：

```latex
\documentclass{article}
\begin{document}
第一個文件。這是一個簡單的範例，不含任何 
額外的參數或套件。
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Simplest+working+example+LaTeX+document\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AFirst+document.+This+is+a+simple+example%2C+with+no+%0Aextra+parameters+or+packages+included.%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![範例文件](https://images.ctfassets.net/nrgyaltdicpt/5oBbZABsWEnHZpwhfvV2PD/95ba7d5496616b4f10dffe6fb90f578a/LL30Fig1-plain.svg)

您可以看到 LaTeX 已自動縮排段落的第一行，替您處理了該格式設定。讓我們更仔細看看程式碼的每個部分在做什麼。

程式碼的第一行， `\documentclass{article}`，宣告了文件類型，也就是其 *類別*，它控制文件的整體外觀。不同類型的文件需要不同的類別；也就是說，履歷／簡歷所需的類別會與科學論文不同，而科學論文可能使用標準 LaTeX 的 `article` 類別。您可能正在撰寫的其他類型文件，可能需要不同的類別，例如 `book` 或 `report`。若想了解可用的眾多 LaTeX 類別類型， [請造訪 CTAN（Comprehensive TeX Archive Network）上的相關頁面](https://www.ctan.org/topic/class).

設定好文件類別後，我們的內容，也就是所謂的 *本文* ，會寫在 `\begin{document}` 與 `\end{document}` 標籤之間。打開上述範例後，您可以修改文字，完成後再透過 *重新編譯文件*。要在 Overleaf 中這麼做，只要按下 **重新編譯**，如這段簡短影片所示：

任何 Overleaf 專案都可以設定為在每次編輯時自動重新編譯：請點擊該 **重新編譯** 按鈕旁的小箭頭，並將 **自動編譯** 設為 **開啟**，如下方螢幕截圖所示：

![顯示如何將自動編譯設為開啟或關閉的螢幕截圖](/files/903f5197bbcc5d8e58922a82861c2ef897213a38)

看過如何將內容加入文件後，下一步就是替它加上標題。為此，我們必須簡短談一下 **前言**.

## 文件的前言

上方的螢幕截圖顯示 Overleaf 將 LaTeX 文件儲存為名為 `main.tex`： `.tex` 檔案副檔名，依慣例，用於命名包含您文件 LaTeX 程式碼的檔案。

前一個範例展示了如何在 `\begin{document}` 指令之後輸入文件內容；然而，您的 `.tex` 檔案中出現在 *之前的* 那個位置之前的所有內容稱為 *前言*前言 *套件* ，以及您想使用的套件（這部分稍後 [會再](#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages)），並且也會在這裡套用其他類型的設定。

最簡單的文件前言可能會長這樣：

```latex
\documentclass[12pt, letterpaper]{article}
\usepackage{graphicx}
```

其中 `\documentclass[12pt, letterpaper]{article}` 定義文件的整體類別（類型）。其他參數必須以逗號分隔，並以方括號（`[...]`）並用來設定此 article 類別的實例；也就是我們希望用於這個特定 `article`為基於該類別的文件。

在此範例中，這兩個參數的作用如下：

* `12pt` 設定字體大小
* `letterpaper` 設定紙張大小

當然也可使用其他字體大小， `9pt`, `11pt`, `12pt`，但若未指定任何值，預設大小為 `10pt`。至於紙張大小，其他可能的值包括 `a4paper` 與 `legalpaper`。欲了解更多資訊，請參閱關於 [頁面大小與邊界](broken://pages/4da25affa03ee2282ab0ebbc038e6905cd1f683a).

前言中的這一行

```latex
\usepackage{graphicx}
```

是載入外部套件的範例（此處為 [graphicx](https://ctan.org/pkg/graphicx?lang=en)）以擴充 LaTeX 的功能，使其能匯入外部圖形檔案。LaTeX 套件會在以下章節中討論： [找尋與使用 LaTeX 套件](#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages).

## 加入標題、作者與日期資訊

為文件加入標題、作者與日期需要在 *前言* (*不是* 文件主體）。這些行如下：

* `\title{我的第一份 LaTeX 文件}`：文件標題
* `\author{Hubert Farnsworth}`：在這裡您撰寫作者姓名，並可選擇加上 `\thanks` 指令，置於大括號內：
  * `\thanks{由 Overleaf 團隊資助。}`：可以在作者姓名之後、放在 `author` 指令的大括號內加入。這會新增上標與腳註，腳註內容為大括號中的文字。若您需要在文章中致謝某個機構，這很有用。
* `\date{2022年8月}`：您可以手動輸入日期，或使用指令 `\today` 來在每次編譯文件時排版當前日期

加上這些行後，您的前言應該會像這樣：

```latex
\documentclass[12pt, letterpaper]{article}
\title{我的第一份 LaTeX 文件}
\author{Hubert Farnsworth\thanks{由 Overleaf 團隊資助。}}
\date{2022年8月}
```

要排版標題、作者與日期，請使用 `\maketitle` 指令，位於文件的 *本文* 正文中：

```latex
\begin{document}
\maketitle
我們現在已為第一份 \LaTeX{} 文件加上標題、作者與日期！
\end{document}
```

前言與本文現在可以結合，產生一份可在 Overleaf 中開啟的完整文件：

```latex
\documentclass[12pt, letterpaper]{article}
\title{我的第一份 LaTeX 文件}
\author{Hubert Farnsworth\thanks{由 Overleaf 團隊資助。}}
\date{2022年8月}
\begin{document}
\maketitle
我們現在已為第一份 \LaTeX{} 文件加上標題、作者與日期！
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=My+first+LaTeX+document\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0AWe+have+now+added+a+title%2C+author+and+date+to+our+first+%5CLaTeX%7B%7D+document%21%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![以 LaTeX 產生的簡單文件圖片](https://images.ctfassets.net/nrgyaltdicpt/3WzMd0bx2zlj3zLTKS8SxT/dc2b7c16adbc96bdd750b6d2c8b4227e/LL30Fig2r-plain.svg)

## 加入註解

LaTeX 是一種「程式碼」，但專門用於文件排版；因此，和以任何其他程式語言撰寫的程式碼一樣，在文件中加入註解可能非常有用。LaTeX 註解是一段不會被排版，也不會以任何方式影響文件的文字——常用於加入「待辦」註記；補充說明；在行內解釋較棘手的巨集，或在除錯時將 LaTeX 程式碼的行／區塊註解掉。

要在 LaTeX 中加入註解，只要在行首寫上 `%` 符號，如下列使用上方範例的程式碼所示：

```latex
\documentclass[12pt, letterpaper]{article}
\title{我的第一份 LaTeX 文件}
\author{Hubert Farnsworth\thanks{由 Overleaf 團隊資助。}}
\date{2022年8月}
\begin{document}
\maketitle
我們現在已為第一份 \LaTeX{} 文件加上標題、作者與日期！

% 這一行是一則註解。它不會在文件中被排版。
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=My+first+LaTeX+document+with+a+comment\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0AWe+have+now+added+a+title%2C+author+and+date+to+our+first+%5CLaTeX%7B%7D+document%21%0A%0A%25+This+line+here+is+a+comment.+It+will+not+be+typeset+in+the+document.%0A%5Cend%7Bdocument%7D)

此範例產生的輸出與前一段不含註解的 LaTeX 程式碼相同。

## 粗體、斜體與底線

接下來，我們將看看一些文字格式化指令：

* **粗體**：LaTeX 中的粗體文字是使用 `\textbf{...}` 指令排版的。
* *斜體*：斜體文字是使用 `\textit{...}` 指令排版的。
* 底線：要為文字加底線，請使用 `\underline{...}` 指令排版的。

下一個範例示範這些指令：

```latex
一些最 \textbf{偉大的}
在 \underline{科學} 中的發現 
是因 \textbf{\textit{偶然}} 而產生的。
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=First+example+with+formatted+text\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0ASome+of+the+%5Ctextbf%7Bgreatest%7D%0Adiscoveries+in+%5Cunderline%7Bscience%7D+%0Awere+made+by+%5Ctextbf%7B%5Ctextit%7Baccident%7D%7D.%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示 LaTeX 文件中文字格式化的圖片](https://images.ctfassets.net/nrgyaltdicpt/6vo15r0tLCByTpcvn1w2E8/8f14edc7ece1f10ce4ba2c76e1e58a56/LL30Fig3r-plain.svg)

另一個非常有用的指令是 `\emph{_argument_}`，其對其 `_argument_` 的效果取決於上下文。在一般文字中，強調文字會以斜體顯示，但如果在斜體文字中使用，這個行為會反轉——請看下一個範例：

```latex
科學中的一些最偉大的 \emph{發現} 
是偶然發現的。

\textit{科學中的一些最偉大的 \emph{發現} 
在科學中是偶然發現的。}

\textbf{科學中的一些最偉大的 \emph{發現} 
在科學中是偶然發現的。}
```

[開啟此 **\emph** 範例於 Overleaf 中。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Using+the+emph+command\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cparindent0pt%25+To+remove+the+paragraph+indentation%0A%5Cmaketitle%0ASome+of+the+greatest+%5Cemph%7Bdiscoveries%7D+in+science+%0Awere+made+by+accident.%0A%0A%5Ctextit%7BSome+of+the+greatest+%5Cemph%7Bdiscoveries%7D+%0Ain+science+were+made+by+accident.%7D%0A%0A%5Ctextbf%7BSome+of+the+greatest+%5Cemph%7Bdiscoveries%7D+%0Ain+science+were+made+by+accident.%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![示範使用 LaTeX \emph 指令的圖片](https://images.ctfassets.net/nrgyaltdicpt/1K0A8RXXOomakJBAr2usmp/62d0e8b5275092dd0b1b4a59c745b632/LL30Fig4r-plain.svg)

* **註：** 某些套件，例如 [Beamer](broken://pages/2b4d45a9dbbbe815083ab6a09145dfc1760f94c2)，會改變 `\emph` 指令排版的。

## 加入圖片

在本節中，我們將看看如何將圖片加入 LaTeX 文件。Overleaf 支援三種插入圖片的方式：

1. 使用 [**插入圖形** 按鈕](https://learn.overleaf.com/learn/Kb/How%5Fto%5Finsert%5Ffigures%5Fin%5FOverleaf#Using%5FInsert%5FFigure%5Fto%5Fadd%5Fa%5Ffigure%5Fto%5Fyour%5Fproject)(![The Insert Figure button on the editor toolbar](https://sharelatex-wiki-cdn-671420.cdn77.org/learn-scripts/images/a/a8/InsertFigureButton.png)），位於編輯器工具列上，用來將圖片插入 **視覺編輯器** 或 **程式碼編輯器**.
2. [複製並貼上圖片](broken://pages/7e92b21aea35848c92b3ca00ec93bb17d4cabd4d) 到 **視覺編輯器** 或 **程式碼編輯器**.
3. 使用 **程式碼編輯器** 來撰寫插入圖形的 LaTeX 程式碼。

選項 1 和 2 會自動產生插入圖片所需的 LaTeX 程式碼，但這裡我們介紹選項 3——請注意，您需要 [上傳那些圖片](https://docs.overleaf.com/writing-and-editing/inserting-images) 到您的 Overleaf 專案中。以下範例示範如何包含圖片：

```latex
\documentclass{article}
\usepackage{graphicx} %LaTeX 套件，用於匯入圖形
\graphicspath{{images/}} %設定 graphicx 套件
 
\begin{document}
宇宙浩瀚，且看似在 
大尺度上，我們所見之處皆大致均勻。

% \includegraphics 指令是 
% 由以下提供（實作）： 
% graphicx 套件
\includegraphics{universe}  
 
上方有一張星系的圖片。
\end{document}
```

[在 Overleaf 中開啟此圖片範例。](https://www.overleaf.com/project/new/template/25686?id=107971142\&templateName=Example+using+images+in+LaTeX\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=)

此範例會產生以下輸出：

![顯示匯入 LaTeX 文件的圖片圖示](https://images.ctfassets.net/nrgyaltdicpt/4p5toat08FUgehv4yuNwXS/efc9e75e4342c9caeec94f26627fa337/LL30Fig5v2-plain.svg)

將圖形匯入 LaTeX 文件需要 [一個附加 *套件*](#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages) ，它提供包含外部圖形檔案所需的指令與功能。上方範例載入了 [graphicx 套件](https://ctan.org/pkg/graphicx?lang=en) 它在眾多其他指令中提供 `\includegraphics{...}` 來匯入圖形，以及 `\graphicspath{...}` 來告訴 LaTeX 圖形位於何處。

若要使用 `graphicx` 套件，請在您的 Overleaf 文件前言中加入下列一行：

```latex
\usepackage{graphicx}
```

在我們的範例中，指令 `\graphicspath{{images/}}` 告訴 LaTeX 圖片儲存在名為 `images`的資料夾中，而該資料夾位於目前目錄內：

![顯示 LaTeX 存取儲存在資料夾中的圖片的圖示](https://sharelatex-wiki-cdn-671420.cdn77.org/learn-scripts/images/1/17/ImagesInAFolder.png)

這個 `\includegraphics{universe}` 指令負責實際將圖片插入文件中。在這裡， `universe` 是圖片檔案名稱，但不含副檔名。

**注意**:

* 雖然在 `\includegraphics` 指令中允許使用包含副檔名的完整檔名，但最佳做法是省略副檔名，因為這樣會讓 LaTeX 搜尋所有支援的格式。
* 一般而言，圖形檔名不應包含空白或多個句點；將圖片檔上傳到 Overleaf 時，也建議副檔名使用小寫字母。

有關 LaTeX 套件的更多資訊，可在本教學末尾的章節中找到： [找尋與使用 LaTeX 套件](#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages).

## 標題、標籤與參照

圖片可透過 `figure` 環境加上標題、標籤並建立參照，如下所示：

```latex
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{images/}}
 
\begin{document}

\begin{figure}[h]
    \centering
    \includegraphics[width=0.75\textwidth]{mesh}
    \caption{一張不錯的圖。}
    \label{fig:mesh1}
\end{figure}
 
如您在圖 \ref{fig:mesh1} 中所見，該函數在原點附近會增長。這個範例位於第 \pageref{fig:mesh1} 頁。

\end{document}
```

[在 Overleaf 中開啟此圖片範例。](https://www.overleaf.com/project/new/template/25519?id=107980830\&templateName=Example+using+image+captions+in+LaTeX\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=)

此範例會產生以下輸出：

![顯示在 LaTeX 中使用圖說的圖示](https://images.ctfassets.net/nrgyaltdicpt/4DkAWWkQqZP6QHU2EErogQ/9aa2371f2cde0f3f8bf22aa1c0b91789/LL30Fig6r-plain.svg)

此範例中有幾個值得注意的指令：

* **`\includegraphics[width=0.75\textwidth]{mesh}`**：這種形式的 `\includegraphics` 指示 LaTeX 將圖形寬度設為文字寬度的 75%——其數值儲存在 `\textwidth` 指令排版的。
* **`\caption{一張不錯的圖。}`**：顧名思義，這個指令可設定圖說，且可放在圖形上方或下方。若您建立圖表清單，此圖說將會用於該清單中。
* **`\label{fig:mesh1}`**：若要在文件中參照這張圖片，您可以使用 `\label` 指令為它設定標籤。該標籤用於為圖片產生編號，並結合下一個指令，讓您可以對其進行參照。
* **`\ref{fig:mesh1}`**：這段程式碼會被對應於所參照圖形的編號取代。

LaTeX 文件中加入的圖片應放在 `figure` 環境或類似結構中，這樣 LaTeX 才能自動將圖片放在文件中的適當位置。

更多指引請參閱以下 Overleaf 說明文章：

* [圖形定位](broken://pages/322d03b2a20e9e33d1704b008e6a6e0ecdcf41be)
* [插入圖片](https://docs.overleaf.com/writing-and-editing/inserting-images)

## 在 LaTeX 中建立清單

您可以使用 *環境*，它們用來包裝實作特定排版功能所需的 LaTeX 程式碼。環境以 `\begin{_environment-name_}` 開始，並以 `\end{_environment-name_}` 其中 `_environment-name_` 可能是 `figure`, `tabular` 或其中一種清單類型： `itemize` 用於無序清單，或 `enumerate` 用於有序清單。

### 無序清單

無序清單是由 `itemize` 環境產生的。每個清單項目之前都必須加上 `\item` 指令，如下所示：

```latex
\documentclass{article}
\begin{document}
\begin{itemize}
  \item 個別項目會以黑點，也就是所謂的項目符號，標示。
  \item 項目中的文字可長可短。
\end{itemize}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Example+of+a+LaTeX+unordered+list\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bitemize%7D%0A++%5Citem+The+individual+entries+are+indicated+with+a+black+dot%2C+a+so-called+bullet.%0A++%5Citem+The+text+in+the+entries+may+be+of+any+length.%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![LaTeX 中項目符號清單的範例](https://images.ctfassets.net/nrgyaltdicpt/1lDj29Mg9ZuLNHXwZrTJew/cd7d950c65fd05b251c4aa55746f4eb7/LL30Fig7-plain.svg)

您也可以開啟這個 [較大的 Overleaf 專案](https://www.overleaf.com/project/new/template/25521?id=107987258\&templateName=Demonstrating+various+types+of+LaTeX+list\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=) ，其中示範了各種類型的 LaTeX 清單。

### 有序清單

有序清單使用與無序清單相同的語法，但會使用 `enumerate` 環境建立：

```latex
\documentclass{article}
\begin{document}
\begin{enumerate}
  \item 這是我們清單中的第一個項目。
  \item 隨著我們新增每個項目，清單編號會增加。
\end{enumerate}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Example+of+the+enumerate+environment\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Benumerate%7D%0A++%5Citem+This+is+the+first+entry+in+our+list.%0A++%5Citem+The+list+numbers+increase+with+each+entry+we+add.%0A%5Cend%7Benumerate%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![LaTeX 中產生的編號列表示例](https://images.ctfassets.net/nrgyaltdicpt/2vkfa7RiAIJxv5wIZbg9UK/530c384f159d091a587de279d13fefec/LL30Fig8-plain.svg)

如同 `無序` 清單一樣，每個項目前都必須加上 `\item` 命令；在這裡，它會自動產生數字式有序清單標籤值，從 1 開始。

如需進一步資訊，你可以開啟這篇 [較大的 Overleaf 專案](https://www.overleaf.com/project/new/template/25521?id=107987258\&templateName=Demonstrating+various+types+of+LaTeX+list\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=) ，它展示了各種 LaTeX 清單類型，或者造訪我們專門的 [LaTeX 清單說明文章](broken://pages/faa363230a0d038e9c745e7ad48836d92734c9b9)，其中提供了更多範例，並展示如何建立自訂清單。

## 在 LaTeX 中加入數學

LaTeX 的主要優點之一，是數學表達式可以很容易地書寫。LaTeX 提供兩種用於排版數學的書寫模式：

* *行內* 數學模式，用於撰寫屬於段落一部分的公式
* *顯示* 數學模式，用於撰寫不屬於文字或段落一部分、並以獨立行排版的表達式

### 行內數學模式

讓我們看看行內數學模式的範例：

```latex
\documentclass[12pt, letterpaper]{article}
\begin{document}
在物理學中，質能等價可表述為 
由愛因斯坦於 1905 年發現的方程式 $E=mc^2$。
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Inline+LaTeX+maths+example\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AIn+physics%2C+the+mass-energy+equivalence+is+stated+%0Aby+the+equation+%24E%3Dmc%5E2%24%2C+discovered+in+1905+by+Albert+Einstein.%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示以行內數學模式排版的數學的圖片](https://images.ctfassets.net/nrgyaltdicpt/77oK8pfew1B1VvxiBtALag/15714f2da4e469bbb8cf70a0d3733e5d/LL30Fig9-plain.svg)

若要排版行內模式數學，你可以使用以下其中一組定界符： `\( ... \)`, `$ ... $` 或 `\begin{math} ... \end{math}`，如下例所示：

```latex
\documentclass[12pt, letterpaper]{article}
\begin{document}
\begin{math}
E=mc^2
\end{math} 會在段落中以行內數學模式排版——$E=mc^2$ 亦然，而 \(E=mc^2\) 也是如此。
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Demonstrating+inline+math+mode\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bmath%7D%0AE%3Dmc%5E2%0A%5Cend%7Bmath%7D+is+typeset+in+a+paragraph+using+inline+math+mode---as+is+%24E%3Dmc%5E2%24%2C+and+so+too+is+%5C%28E%3Dmc%5E2%5C%29.%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示使用 LaTeX 行內數學模式產生內容的圖片](https://images.ctfassets.net/nrgyaltdicpt/46VPfEGxYJ3di18TLzaLo9/ed67df07532c52b6543fd2bae7cb7c04/LL30Fig10r-plain.svg)

### 顯示數學模式

以顯示模式排版的方程式可以編號或不編號，如下例所示：

```latex
\documentclass[12pt, letterpaper]{article}
\begin{document}
質能等價可由著名方程式描述
\[ E=mc^2 \]，由愛因斯坦於 1905 年發現。 

在自然單位（$c = 1$）下，該公式表達了以下恆等式
\begin{equation}
E=m
\end{equation}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Display+math+mode+example\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AThe+mass-energy+equivalence+is+described+by+the+famous+equation%0A%5C%5B+E%3Dmc%5E2+%5C%5D+discovered+in+1905+by+Albert+Einstein.+%0A%0AIn+natural+units+%28%24c+%3D+1%24%29%2C+the+formula+expresses+the+identity%0A%5Cbegin%7Bequation%7D%0AE%3Dm%0A%5Cend%7Bequation%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示 LaTeX 中顯示模式數學的圖片](https://images.ctfassets.net/nrgyaltdicpt/6n1FPYFzmzCHPpWoEoYQZM/79b33f1096be06326bff7d2e1976c67c/LL30Fig11-plain.svg)

若要排版顯示模式數學，你可以使用以下其中一組定界符： `\[ ... \]`, `\begin{displaymath} ... \end{displaymath}` 或 `\begin{equation} ... \end{equation}`。從歷史上看，排版顯示模式數學需要使用 `$$` 字元定界符，如同 `$$ _... 顯示數學 ..._$$`，但這種方法 [已不再建議使用](https://texfaq.org/FAQ-dolldoll)：請改用 LaTeX 的定界符 `\[ ... \]` 。

### 更完整的範例

以下範例展示了使用 LaTeX 排版的各類數學內容。

```latex
\documentclass{article}
\begin{document}
數學模式中的下標寫作 $a_b$，上標寫作 $a^b$。兩者可以結合並巢狀，用來撰寫例如

\[ T^{i_1 i_2 \dots i_p}_{j_1 j_2 \dots j_q} = T(x^{i_1},\dots,x^{i_p},e_{j_1},\dots,e_{j_q}) \]
 
我們使用 $\int$ 表示積分，使用 $\frac{a}{b}$ 表示分數。積分的上下限則透過上標與下標來放置：

\[ \int_0^1 \frac{dx}{e^x} =  \frac{e-1}{e} \]

小寫希臘字母寫作 $\omega$ $\delta$ 等，而大寫希臘字母寫作 $\Omega$ $\Delta$。

數學運算子前面要加上反斜線，例如 $\sin(\beta)$、$\cos(\alpha)$、$\log(x)$ 等。
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=More+advanced+LaTeX+math+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0ASubscripts+in+math+mode+are+written+as+%24a%5Fb%24+and+superscripts+are+written+as+%24a%5Eb%24.+These+can+be+combined+and+nested+to+write+expressions+such+as%0A%0A%5C%5B+T%5E%7Bi%5F1+i%5F2+%5Cdots+i%5Fp%7D%5F%7Bj%5F1+j%5F2+%5Cdots+j%5Fq%7D+%3D+T%28x%5E%7Bi%5F1%7D%2C%5Cdots%2Cx%5E%7Bi%5Fp%7D%2Ce%5F%7Bj%5F1%7D%2C%5Cdots%2Ce%5F%7Bj%5Fq%7D%29+%5C%5D%0A+%0AWe+write+integrals+using+%24%5Cint%24+and+fractions+using+%24%5Cfrac%7Ba%7D%7Bb%7D%24.+Limits+are+placed+on+integrals+using+superscripts+and+subscripts%3A%0A%0A%5C%5B+%5Cint%5F0%5E1+%5Cfrac%7Bdx%7D%7Be%5Ex%7D+%3D++%5Cfrac%7Be-1%7D%7Be%7D+%5C%5D%0A%0ALower+case+Greek+letters+are+written+as+%24%5Comega%24+%24%5Cdelta%24+etc.+while+upper+case+Greek+letters+are+written+as+%24%5COmega%24+%24%5CDelta%24.%0A%0AMathematical+operators+are+prefixed+with+a+backslash+as+%24%5Csin%28%5Cbeta%29%24%2C+%24%5Ccos%28%5Calpha%29%24%2C+%24%5Clog%28x%29%24+etc.%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示 LaTeX 中各種數學排版的圖片](https://images.ctfassets.net/nrgyaltdicpt/5xfm4NybWckHL5ndpdnBRq/bc0b5e27044820ea5cdd11648a68f543/LL30Fig12r-plain.svg)

下一個範例使用 `equation*` 環境，該環境由 `amsmath` 套件提供，因此我們需要在文件導言區加入下列一行：

```latex
\usepackage{amsmath}% 適用於 equation* 環境
```

如需進一步了解如何使用 `amsmath` 請參閱 [我們的說明文章](broken://pages/019eff292f0e63a23869d66e0438955fd3a2d4fe).

```latex
\documentclass{article}
\usepackage{amsmath}% 適用於 equation* 環境
\begin{document}
\section{第一個範例}

廣為人知的畢氏定理 \(x^2 + y^2 = z^2\) 已被證明在其他指數下並不成立，也就是說，下面這個方程式對於 \(n>2\) 沒有整數解：

\[ x^n + y^n = z^n \]

\section{第二個範例}

這是一個在文字中出現的簡單數學式 \(\sqrt{x^2+1}\)。 
這也同樣是： 
\begin{math}
\sqrt{x^2+1}
\end{math}
但改用另一個命令。

這是一個不帶編號的簡單數學式
\[\sqrt{x^2+1}\] 
與文字分開。

這也同樣是：
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}

\ldots 以及這個：
\begin{equation*}
\sqrt{x^2+1}
\end{equation*}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=More+math+content+typeset+in+LaTeX\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%25+For+the+equation%2A+environment%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BFirst+example%7D%0A%0AThe+well-known+Pythagorean+theorem+%5C%28x%5E2+%2B+y%5E2+%3D+z%5E2%5C%29+was+proved+to+be+invalid+for+other+exponents%2C+meaning+the+next+equation+has+no+integer+solutions+for+%5C%28n%3E2%5C%29%3A%0A%0A%5C%5B+x%5En+%2B+y%5En+%3D+z%5En+%5C%5D%0A%0A%5Csection%7BSecond+example%7D%0A%0AThis+is+a+simple+math+expression+%5C%28%5Csqrt%7Bx%5E2%2B1%7D%5C%29+inside+text.+%0AAnd+this+is+also+the+same%3A+%0A%5Cbegin%7Bmath%7D%0A%5Csqrt%7Bx%5E2%2B1%7D%0A%5Cend%7Bmath%7D%0Abut+by+using+another+command.%0A%0AThis+is+a+simple+math+expression+without+numbering%0A%5C%5B%5Csqrt%7Bx%5E2%2B1%7D%5C%5D+%0Aseparated+from+text.%0A%0AThis+is+also+the+same%3A%0A%5Cbegin%7Bdisplaymath%7D%0A%5Csqrt%7Bx%5E2%2B1%7D%0A%5Cend%7Bdisplaymath%7D%0A%0A%5Cldots+and+this%3A%0A%5Cbegin%7Bequation%2A%7D%0A%5Csqrt%7Bx%5E2%2B1%7D%0A%5Cend%7Bequation%2A%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示 LaTeX 中各種數學排版的圖片](https://images.ctfassets.net/nrgyaltdicpt/hT85gkewCDlxCHeNU5slE/9256c129b25bec7a93dcfe9e44b0654a/LL30Fig13r-plain.svg)

LaTeX 中數學排版的可能性無窮無盡，因此務必造訪我們的說明頁面，以取得特定主題的建議與範例：

* [數學表達式](broken://pages/dd286b0574aeb68b65c637689e46e63f76b4b3f0)
* [下標與上標](broken://pages/c8765c823e8a79564085ad6ef438a82395642db8)
* [括號與圓括號](broken://pages/074784fa5c9cb390075077a8bcfa7634aff8a04f)
* [分數與二項式](broken://pages/83a25425324807d02fbb775feb801bd6d7936cb3)
* [方程式對齊](broken://pages/afb9cc965bf05a40bbaf6f63d6b4199619649707)
* [運算子](broken://pages/10b425e4215d6b84ef84f5e3b75afce66c221efe)
* [數學模式中的間距](broken://pages/b5e12eb180be0353209b080e051fdbb7a12337bb)
* [積分、總和與極限](broken://pages/f2a8e70590bc1a3f6c8b2eab3f7c150997fa86eb)
* [數學模式中的顯示樣式](broken://pages/1ab7d6e221b7ac08895f547ba901641ccafbacec)
* [希臘字母與數學符號列表](broken://pages/be053efbf416ddf7482ea83788126b4ef868a675)
* [數學字型](broken://pages/49ab6b92e0cfe40ae2aeb457fb0a0159f912f16a)

## 基本文件結構

接下來，我們將探討摘要，以及如何將 LaTeX 文件劃分為不同的章、節與段落。

### 摘要

科學文章通常會提供一個 *摘要* ，這是其核心主題或論點的簡短概覽／摘要。下一個範例示範如何使用 LaTeX 的 `摘要` 環境建立：

```latex
\documentclass{article}
\begin{document}
\begin{abstract}
文件開頭的一段簡單段落。 
關於主要主題的簡短介紹。
\end{abstract}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=How+to+create+a+document+abstract\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Babstract%7D%0AThis+is+a+simple+paragraph+at+the+beginning+of+the+%0Adocument.+A+brief+introduction+about+the+main+subject.%0A%5Cend%7Babstract%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示 LaTeX 中摘要排版的圖片](https://images.ctfassets.net/nrgyaltdicpt/4cPu8x8ZxuDVF6efcZ03yi/91aa6aa84719613b0e25c725b70aa0b8/LL30Fig14-plain.svg)

### 段落與換行

有了摘要之後，我們就可以開始撰寫第一個段落。下一個範例示範：

* 如何透過按兩次「enter」鍵來建立新段落，結束目前行並插入下一個空白行；
* 如何在不建立新段落的情況下開始新的一行，方法是使用 `\\` 命令，也就是雙反斜線；或者使用 `\newline` 指令排版的。

此範例中的第三個段落示範了命令 `\\` 與 `\newline`:

```latex
\documentclass{article}
\begin{document}

\begin{abstract}
文件開頭的一段簡單段落。 
關於主要主題的簡短介紹。
\end{abstract}

在摘要之後，我們可以開始第一個段落，然後按兩次 ``enter'' 來開始第二個段落。

這一行將開始第二個段落。

我將開始第三個段落，然後加入 \\ 一個手動換行，這會讓這段文字從新的一行開始，但仍屬於同一個段落。或者，我也可以使用 \verb|\\newline|\\newline 命令來開始新的一行，這同樣屬於同一個段落。
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Example+to+create+a+new+paragraph\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Babstract%7D%0AThis+is+a+simple+paragraph+at+the+beginning+of+the+%0Adocument.+A+brief+introduction+about+the+main+subject.%0A%5Cend%7Babstract%7D%0A%0AAfter+our+abstract+we+can+begin+the+first+paragraph%2C+then+press+%60%60enter%27%27+twice+to+start+the+second+one.%0A%0AThis+line+will+start+a+second+paragraph.%0A%0AI+will+start+the+third+paragraph+and+then+add+%5C%5C+a+manual+line+break+which+causes+this+text+to+start+on+a+new+line+but+remains+part+of+the+same+paragraph.+Alternatively%2C+I+can+use+the+%5Cverb%7C%5Cnewline%7C%5Cnewline+command+to+start+a+new+line%2C+which+is+also+part+of+the+same+paragraph.%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示 LaTeX 中段落建立的圖片](https://images.ctfassets.net/nrgyaltdicpt/5DdSH06hFli8k4lR0dxw4m/a55cddf9aa10b64302ae7b72ba0c6447/LL30Fig15r-plain.svg)

請注意 LaTeX 會自動縮排段落——除了緊接在文件標題，例如 section 和 subsection 之後——[如我們將看到](#Chapters%5Fand%5Fsections).

建議新使用者注意，多個 `\\` 或 `\newline`空白字元不應被用來「模擬」段落之間更大的間距，因為這可能會干擾 LaTeX 的排版演算法。建議的方法是繼續使用空白行來建立新段落，而不要使用任何 `\\`，並載入 [parskip 套件](https://ctan.org/pkg/parskip?lang=en) ，方法是加入 `\usepackage{parskip}` 到導言區。

關於段落的進一步資訊可見以下文章：

* [段落與換行](broken://pages/7c450fe03cdbf27cf3923c516f37be210bcb0867)
* [如何在 LaTeX 中變更段落間距](broken://pages/f2081457c2b31e11413a1c09a7312744e9646504)
* [LaTeX 錯誤：這裡沒有可結束的行](broken://pages/017067b0bf7c8c4e9752c8327aa60da0433bb4e3) 提供關於使用的更多建議與指引 `\\`.

### 章節與節

較長的文件，不論使用何種撰寫軟體，通常會被劃分為各部分、章、節、子節等等。LaTeX 也提供文件結構命令，但可用的命令及其實作方式（它們實際做什麼）可能取決於所使用的文件類別。舉例來說，使用 `book` 類別可以分成各部分、章、節、子節等等，但 `letter` 類別並未提供（支援）任何用來執行此操作的命令。

下一個範例示範了根據 `book` 類別用來結構化文件的命令：

```latex
\documentclass{book}
\begin{document}

\chapter{第一章}

\section{導言}

這是第一節。

Lorem  ipsum  dolor  sit  amet,  consectetuer  adipiscing  
elit. Etiam  lobortisfacilisis sem.  Nullam nec mi et 
neque pharetra sollicitudin.  Praesent imperdietmi nec ante. 
Donec ullamcorper, felis non sodales...

\section{第二節}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra 
sollicitudin.  Praesent imperdiet mi necante...

\subsection{第一個子節}
Praesent imperdietmi nec ante. Donec ullamcorper, felis non sodales...

\section*{無編號節}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  
Etiam lobortis facilisissem...
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Book+class+example+document\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cchapter%7BFirst+Chapter%7D%0A%0A%5Csection%7BIntroduction%7D%0A%0AThis+is+the+first+section.%0A%0ALorem++ipsum++dolor++sit++amet%2C++consectetuer++adipiscing++%0Aelit.+Etiam++lobortisfacilisis+sem.++Nullam+nec+mi+et+%0Aneque+pharetra+sollicitudin.++Praesent+imperdietmi+nec+ante.+%0ADonec+ullamcorper%2C+felis+non+sodales...%0A%0A%5Csection%7BSecond+Section%7D%0A%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem.++Nullam+nec+mi+et+neque+pharetra+%0Asollicitudin.++Praesent+imperdiet+mi+necante...%0A%0A%5Csubsection%7BFirst+Subsection%7D%0APraesent+imperdietmi+nec+ante.+Donec+ullamcorper%2C+felis+non+sodales...%0A%0A%5Csection%2A%7BUnnumbered+Section%7D%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem...%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示 LaTeX 書籍文件中各節的圖片](https://images.ctfassets.net/nrgyaltdicpt/1A1T7YsurQdM058OwmLqd8/7f6e962c31356bafa702bab9062f6061/LL30Fig16-plain.svg)

各分節命令的名稱大多顧名思義；例如， `\chapter{第一章}` 會建立一個標題為 `第一章`, `\section{導言}` 的節， `導言`，依此類推。節還可以進一步劃分為 `\subsection{...}` 甚至 `\subsubsection{...}`。節、子節等的編號是自動的，但可透過使用所謂的 *星號版本* 來停用適當命令末尾帶有星號 (\*) 的編號，例如 `\section*{...}` 與 `\subsection*{...}`.

*總括而言*，LaTeX 文件類別提供以下分節命令，而特定類別各自只支援其中一部分：

* `\part{部分}`
* `\chapter{章}`
* `\section{節}`
* `\subsection{子節}`
* `\subsubsection{小節}`
* `\paragraph{段落}`
* `\subparagraph{子段落}`

特別是， `\part` 與 `\chapter` 命令僅在 `report` 與 `book` 文件類別中可用。

請參閱 Overleaf 的文章 [關於節與章節的文章](broken://pages/cd89cc03967c2b73344589e4a818759f7d61309e) 以進一步了解文件結構命令。

## 建立表格

Overleaf 提供三種建立表格的選項：

1. 使用 [**插入表格** 按鈕](https://www.overleaf.com/learn/how-to/How%5Fto%5Finsert%5Ftables%5Fin%5FOverleaf%23inserttable#Using%5FInsert%5FTable%5Fto%5Fcreate%5Fa%5Ftable%5Fin%5Fyour%5Fproject) 功能於 **視覺編輯器** （或 **程式碼編輯器**）工具列中。
2. [從另一份文件複製並貼上一個表格](https://www.overleaf.com/learn/how-to/How%5Fto%5Fpaste%5Fformatted%5Fcontent%5Finto%5FOverleaf%23pastingtables) ，同時使用 **視覺編輯器**.
3. 在 Code Editor 中撰寫表格的 LaTeX 程式碼。

如果你是 LaTeX 新手，使用 **視覺編輯器** （選項 1）中的工具列是很好的入門方式——你可以在 **視覺編輯器** 與 **程式碼編輯器** 中切換，以查看表格背後的程式碼。

在這裡，我們聚焦於選項 3——建立表格最靈活的方法——並提供一些範例，示範如何在 LaTeX 中建立表格，包括加入線條（規則線）與標題。隨著經驗增加，請參考我們關於 [如何使用 LaTeX 建立表格](https://www.overleaf.com/learn/latex/Tables).

### 在 LaTeX 中建立基本表格

我們先從一個示範如何排版基本表格的範例開始：

```latex
\begin{center}
\begin{tabular}{c c c}
 儲存格1 & 儲存格2 & 儲存格3 \\ 
 儲存格4 & 儲存格5 & 儲存格6 \\  
 儲存格7 & 儲存格8 & 儲存格9    
\end{tabular}
\end{center}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=An+example+of+a+basic+table\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A%5Cbegin%7Btabular%7D%7Bc+c+c%7D%0A+cell1+%26+cell2+%26+cell3+%5C%5C+%0A+cell4+%26+cell5+%26+cell6+%5C%5C++%0A+cell7+%26+cell8+%26+cell9++++%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D%0A%3C%2Fsource%3E)

此範例會產生以下輸出：

![顯示以 LaTeX 排版的表格的圖](https://images.ctfassets.net/nrgyaltdicpt/5K46QavbKv9vF4RI1GbUf2/f22d4f8a06d00251166f86d2c891321e/LL30Fig17-plain.svg)

這個 `tabular` 環境是 LaTeX 建立表格的預設方法。你必須為此環境指定一個參數，在此例中 `{c c c}` ，這表示 LaTeX 將有三欄，而且每一欄中的文字都必須置中。你也可以使用 `r` 將文字向右對齊，並使用 `l` 將文字向左對齊。對齊符號 `&` 用來在表格列中區分各個儲存格。要結束一列，請使用 *換行* 命令 `\\`。我們的表格包含在一個 `center` 環境中，使其在頁面文字寬度內置中。

### 新增邊框

這個 `tabular` tabular 環境支援作為表格一部分的水平與垂直線（規則線）：

* 若要在列的上方與下方加入水平線，請使用 `\hline` 命令
* 若要在欄與欄之間加入垂直線，請使用垂直線參數 `|`

在此範例中，參數為 `{|c|c|c|}` ，這表示有三個（置中）欄位，每個欄位之間以一條垂直線（規則線）分隔；此外，我們使用 `\hline` 在第一列上方與最後一列下方放置水平線：

```latex
\begin{center}
\begin{tabular}{|c|c|c|} 
 \hline
 儲存格1 & 儲存格2 & 儲存格3 \\ 
 儲存格4 & 儲存格5 & 儲存格6 \\ 
 儲存格7 & 儲存格8 & 儲存格9 \\ 
 \hline
\end{tabular}
\end{center}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Table+example+with+rules\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A%5Cbegin%7Btabular%7D%7B%7Cc%7Cc%7Cc%7C%7D+%0A+%5Chline%0A+cell1+%26+cell2+%26+cell3+%5C%5C+%0A+cell4+%26+cell5+%26+cell6+%5C%5C+%0A+cell7+%26+cell8+%26+cell9+%5C%5C+%0A+%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示包含水平與垂直線之 LaTeX 排版表格的圖](https://images.ctfassets.net/nrgyaltdicpt/5uWYGUhK1XJJJaWz5R1EmX/3ee88bf6b2eaf8d0644b57fa8064800b/LL30Fig18-plain.svg)

以下是另一個範例：

```latex
\begin{center}
 \begin{tabular}{||c c c c||} 
 \hline
 欄1 & 欄2 & 欄2 & 欄3 \\ [0.5ex] 
 \hline\hline
 1 & 6 & 87837 & 787 \\ 
 \hline
 2 & 7 & 78 & 5415 \\
 \hline
 3 & 545 & 778 & 7507 \\
 \hline
 4 & 545 & 18744 & 7560 \\
 \hline
 5 & 88 & 788 & 6344 \\ [1ex] 
 \hline
\end{tabular}
\end{center}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Another+table+example+containing+rules\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A+%5Cbegin%7Btabular%7D%7B%7C%7Cc+c+c+c%7C%7C%7D+%0A+%5Chline%0A+Col1+%26+Col2+%26+Col2+%26+Col3+%5C%5C+%5B0.5ex%5D+%0A+%5Chline%5Chline%0A+1+%26+6+%26+87837+%26+787+%5C%5C+%0A+%5Chline%0A+2+%26+7+%26+78+%26+5415+%5C%5C%0A+%5Chline%0A+3+%26+545+%26+778+%26+7507+%5C%5C%0A+%5Chline%0A+4+%26+545+%26+18744+%26+7560+%5C%5C%0A+%5Chline%0A+5+%26+88+%26+788+%26+6344+%5C%5C+%5B1ex%5D+%0A+%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示包含水平與垂直線之 LaTeX 排版表格的圖](https://images.ctfassets.net/nrgyaltdicpt/1Cy9FcRnDVDRUPQdCgaKq6/c21e5f7d9a53514756c6e1f2996e51e0/LL30Fig19-plain.svg)

### 標題、標籤與參照

你可以像處理圖片一樣為表格加上標題並加以引用。唯一的差別是，你不用 **`figure`** 環境，而是使用 **`table`** 環境。

表格 \ref{table:data} 示範如何為表格加入標題並引用表格。

```latex
\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||} 
 \hline
 欄1 & 欄2 & 欄2 & 欄3 \\ [0.5ex] 
 \hline\hline
 1 & 6 & 87837 & 787 \\ 
 2 & 7 & 78 & 5415 \\
 3 & 545 & 778 & 7507 \\
 4 & 545 & 18744 & 7560 \\
 5 & 88 & 788 & 6344 \\ [1ex] 
 \hline
\end{tabular}
\caption{用於測試標題與標籤的表格。}
\label{table:data}
\end{table}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Table+captions+and+references\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0ATable+%5Cref%7Btable%3Adata%7D+shows+how+to+add+a+table+caption+and+reference+a+table.%0A%5Cbegin%7Btable%7D%5Bh%21%5D%0A%5Ccentering%0A%5Cbegin%7Btabular%7D%7B%7C%7Cc+c+c+c%7C%7C%7D+%0A+%5Chline%0A+Col1+%26+Col2+%26+Col2+%26+Col3+%5C%5C+%5B0.5ex%5D+%0A+%5Chline%5Chline%0A+1+%26+6+%26+87837+%26+787+%5C%5C+%0A+2+%26+7+%26+78+%26+5415+%5C%5C%0A+3+%26+545+%26+778+%26+7507+%5C%5C%0A+4+%26+545+%26+18744+%26+7560+%5C%5C%0A+5+%26+88+%26+788+%26+6344+%5C%5C+%5B1ex%5D+%0A+%5Chline%0A%5Cend%7Btabular%7D%0A%5Ccaption%7BTable+to+test+captions+and+labels.%7D%0A%5Clabel%7Btable%3Adata%7D%0A%5Cend%7Btable%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![帶有標題的 LaTeX 表格](https://images.ctfassets.net/nrgyaltdicpt/1f5w74UIaTCkpApaPdVeWf/580a87c2413ddc9d6bdef3bf462689fc/LL30Fig20-plain.svg)

## 新增目錄

建立目錄很簡單，因為命令 `\tableofcontents` 幾乎會為你完成所有工作：

```latex
\documentclass{article}
\title{章節與節}
\author{Gubert Farnsworth}
\date{2022年8月}
\begin{document}
  
\maketitle
  
\tableofcontents

\section{導言}
   
這是第一節。
      
Lorem  ipsum  dolor  sit  amet,  consectetuer  adipiscing  
elit.   Etiam  lobortisfacilisis sem.  Nullam nec mi et 
neque pharetra sollicitudin.  Praesent imperdietmi nec ante. 
Donec ullamcorper, felis non sodales...
       
\section*{無編號節}
\addcontentsline{toc}{section}{無編號節}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra 
sollicitudin.  Praesent imperdiet mi necante...

\section{第二節}
       
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra 
sollicitudin.  Praesent imperdiet mi necante...
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Generating+a+table+of+contents\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7BSections+and+Chapters%7D%0A%5Cauthor%7BGubert+Farnsworth%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A++%0A%5Cmaketitle%0A++%0A%5Ctableofcontents%0A%0A%5Csection%7BIntroduction%7D%0A+++%0AThis+is+the+first+section.%0A++++++%0ALorem++ipsum++dolor++sit++amet%2C++consectetuer++adipiscing++%0Aelit.+++Etiam++lobortisfacilisis+sem.++Nullam+nec+mi+et+%0Aneque+pharetra+sollicitudin.++Praesent+imperdietmi+nec+ante.+%0ADonec+ullamcorper%2C+felis+non+sodales...%0A+++++++%0A%5Csection%2A%7BUnnumbered+Section%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BUnnumbered+Section%7D%0A%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem.++Nullam+nec+mi+et+neque+pharetra+%0Asollicitudin.++Praesent+imperdiet+mi+necante...%0A%0A%5Csection%7BSecond+Section%7D%0A+++++++%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem.++Nullam+nec+mi+et+neque+pharetra+%0Asollicitudin.++Praesent+imperdiet+mi+necante...%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示由 LaTeX 產生的目錄的圖](https://images.ctfassets.net/nrgyaltdicpt/MxY592Q6EGsDKMA2mZw0t/fffa41a0a0d30770526f73aff72a7be1/LL30Fig21r-plain.svg)

節、子節與章節會自動納入目錄。若要手動加入項目，例如未編號節，請使用命令 `\addcontentsline` 如範例所示。

## 下載你的完成文件

以下這段簡短影片示範如何下載你專案的原始程式碼或排版後的 PDF 檔案：

更多資訊可見 Overleaf 的說明文章 [從 Overleaf 匯出你的作品](https://docs.overleaf.com/managing-projects-and-files/downloading-a-project).

## 找尋與使用 LaTeX 套件

LaTeX 不僅提供強大的排版能力，還提供一個 *可擴充性的架構* 套件 *套件*。與其試圖提供會「包山包海」的命令與功能，LaTeX 的設計宗旨是 *可擴充*，讓使用者可以載入外部程式碼（套件），以提供更專門的排版能力，或擴充 LaTeX 內建功能——例如表格排版。如同在 [加入圖片](#Adding%5Fimages)， `graphicx` 套件透過提供匯入圖形檔案的命令來擴充 LaTeX，並在（導言區）中透過撰寫以下內容來載入

```latex
\usepackage{graphicx}
```

### 載入套件

如上所述，套件會在文件導言區透過 `\usepackage` 命令載入，但由於（許多）LaTeX 套件提供一組 *選項*，可用來設定其行為，因此 `\usepackage` 命令通常如下所示：

```latex
\usepackage[options]{somepackage}
```

方括號「`[...]`」會告知 LaTeX 在載入 `somepackage`時應套用哪一組選項。於使用者要求的選項組中，各別選項或設定通常以逗號分隔；例如 [geometry 套件](https://ctan.org/pkg/geometry) 提供許多選項可在 LaTeX 中設定頁面版面，因此 `geometry` 的典型用法可能如下：

```latex
\usepackage[total={6.5in,8.75in},
top=1.2in, left=0.9in, includefoot]{geometry}
```

這個 `geometry` package 是由全球 LaTeX 社群成員撰寫並貢獻的一個套件範例，並免費提供給任何想使用它的人。

如果某個 LaTeX 套件沒有提供任何選項，或者使用者想使用套件選項的預設值，它會像這樣載入：

```latex
\usepackage{somepackage}
```

當你撰寫 `\usepackage[...]{somepackage}` LaTeX 會尋找一個相對應的檔案，稱為 `_somepackage_.sty`，它需要載入並處理該檔案——以便讓套件命令可用，並執行該套件提供的任何其他程式碼。如果 LaTeX 無法找到 `_somepackage_.sty` 它就會以錯誤結束，如下列 Overleaf 範例所示：

```latex
\documentclass[12pt, letterpaper]{article}
\usepackage{somepackage}% 一個不存在的套件
\begin{document}
這會失敗！
\end{document}
```

[開啟此 ***產生錯誤的範例*** 在 Overleaf 上](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Error+due+to+missing+package\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cusepackage%7Bsomepackage%7D%25+a+NON-EXISTENT+package%0A%5Cbegin%7Bdocument%7D%0AThis+will+fail%21%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![顯示由缺少套件所造成錯誤的圖片](https://sharelatex-wiki-cdn-671420.cdn77.org/learn-scripts/images/6/60/LL30packagefail.png)

### 尋找有關套件的資訊：CTAN

套件是透過 [綜合 TeX 檔案庫網路](https://www.ctan.org/)，通常簡稱為 CTAN；截至撰寫本文時，它收錄了來自 2881 位貢獻者的 6287 個套件。CTAN [自我描述為](https://www.ctan.org/ctan) 為

> ……一組遍布全球的網際網路網站，提供與 TEX 相關的材料供下載。

你可以瀏覽 CTAN 來尋找有用的套件；例如：

* [依主題](https://www.ctan.org/topics/cloud)
* [按字母順序](https://www.ctan.org/pkg) （如果你知道套件名稱，這很有用）

你也可以使用 [搜尋功能](https://www.ctan.org/pkg) （位於頁面頂端）。

### Overleaf 上可用的套件：介紹 TeX Live

每年一次，將（一大） *部分* CTAN 上託管的套件，再加上與 LaTeX 相關的字型與其他軟體，彙整並作為一個稱為 [TeX Live](https://tug.org/texlive/)的系統發行，可用來安裝你自己的（本機）LaTeX 環境。事實上， [Overleaf 的伺服器也使用 TeX Live](https://www.overleaf.com/learn/latex/Overleaf%5Fand%5FTeX%5FLive) ，並會在 TeX Live 發布新版本時進行更新。Overleaf 的 TeX Live 更新並非立即進行，而是在發布後數個月才會執行，讓我們有時間將新版本 TeX Live 與 [我們圖庫中的數千個範本](https://www.overleaf.com/gallery)進行相容性測試。例如，這是我們的 [TeX Live 2025 升級公告](https://www.overleaf.com/blog/tex-live-2025-is-now-available).

雖然 TeX Live 包含（一大） *部分* CTAN 套件的部分內容，但你仍可能找到一個有趣的套件，例如 [用於排版圍棋圖的 igo](https://ctan.org/pkg/igo?lang=en)，它託管於 CTAN，但未包含在 TeX Live（所發行的版本）中，因此在 Overleaf 上無法使用。CTAN 上託管的某些套件並不是 TeX Live 的一部分，原因有很多：也許某個套件已過時、存在授權問題、非常新（剛上傳），或具有平台相依性，例如可在 Windows 上運作但不能在 Linux 上運作。

新的套件以及現有套件的更新全年都會上傳到 CTAN，但 TeX Live 的更新則是每年發行一次；因此，目前版本的 TeX Live 所包含的套件，更新程度不會像 CTAN 上託管的那些那麼新。由於 Overleaf 的伺服器使用 TeX Live，安裝在我們伺服器上的套件——也就是使用者可用的套件——可能不是 CTAN 上可取得的最新版本，但一般而言，這通常不太會造成問題。


---

# 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/texlive/zh-tw/latex-ji-chu/30-fen-zhong-xue-hui-latex.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.
