> 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/tu-biao-yu-biao-ge/01-tables.md).

# 表格

在 Overleaf 中插入表格有三種方式：

1. 使用 [**插入表格** 按鈕](/latex/zh-tw/zhi-shi-ku/067-how-to-generate-and-insert-latex-tables-in-overleaf.md#inserttable) 在編輯器工具列中。
2. 在使用時，從其他文件複製並貼上表格 **視覺編輯器**.
3. 在中撰寫表格的 LaTeX 程式碼 **程式碼編輯器**.

如果你剛接觸 LaTeX，使用 [中的工具列 **視覺編輯器**](/latex/zh-tw/zhi-shi-ku/067-how-to-generate-and-insert-latex-tables-in-overleaf.md#inserttable) （選項 1）是很好的入門方式。而且你可以在 **視覺編輯器** 和 **程式碼編輯器** 之間切換，以查看表格背後的程式碼。 **本頁聚焦於選項 3。**

## 在 LaTeX 中建立簡單表格

我們可以從一個最簡單的表格範例開始：

```latex
\begin{center}
\begin{tabular}{ c c c }
 cell1 & cell2 & cell3 \\
 cell4 & cell5 & cell6 \\
 cell7 & cell8 & cell9
\end{tabular}
\end{center}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+a+basic+table\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A%5Cbegin%7Btabular%7D%7B+c+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)

![表格範例](/files/8a67049d8f66bd6477b701594ed700e955363642)

該 `tabular` 環境是 LaTeX 建立表格的預設方法。你必須為這個環境指定一個參數；這裡我們使用 `{c c c}` 這表示 LaTeX 此表格有三欄，而且每一欄中的文字都必須置中。

該 `tabular` 環境提供了額外的彈性；例如，你可以在每一欄之間加入分隔線：

```latex
\begin{center}
\begin{tabular}{ |c|c|c| }
 \hline
 cell1 & cell2 & cell3 \\
 cell4 & cell5 & cell6 \\
 cell7 & cell8 & cell9 \\
 \hline
\end{tabular}
\end{center}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+a+basic+table+with+separator+lines\&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)

![簡單表格範例](/files/6c7d90c1e625b804cb9e5733f3bcd7f0b996c9b0)

以下是上方範例中所使用結構的說明：

**{ |c|c|c| }**

這表示表格將使用三欄，欄與欄之間以垂直線分隔。每個 c 表示該欄內容將置中。你也可以使用 r 讓文字靠右對齊，使用 l 讓文字靠左對齊。

**\hline**

這會在表格頂端和底端插入水平線。你可以使用 \hline 的次數沒有限制。

**cell1 & cell2 & cell3 \\\\**

每個 & 都是儲存格分隔符號，而雙反斜線 \\\ 則設定這一列的結尾。

下面你可以看到第二個範例，它使用了各種垂直與水平線（通常稱為「rules」）：

```latex
\begin{center}
\begin{tabular}{||c c c c||}
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [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_name=Basic+table+with+horizontal+and+vertical+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)

![含 hline 的表格範例](/files/172f6afb9a77b521e0d16f30648f730d4ea3bb97)

## 固定寬度的表格

在格式化表格時，你可能需要固定寬度，無論是每一欄或整個表格。以下範例加入了 `array` 套件到文件前言：

```latex
\usepackage{array}
```

並使用它來設定固定欄寬：

```latex
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{center}
\begin{tabular}{ | m{5em} | m{1cm}| m{1cm} | }
  \hline
  cell1 dummy text dummy text dummy text& cell2 & cell3 \\
  \hline
  cell1 dummy text dummy text dummy text & cell5 & cell6 \\
  \hline
  cell7 & cell8 & cell9 \\
  \hline
\end{tabular}
\end{center}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Table+with+fixed+column+width\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Barray%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A%5Cbegin%7Btabular%7D%7B+%7C+m%7B5em%7D+%7C+m%7B1cm%7D%7C+m%7B1cm%7D+%7C+%7D+%0A++%5Chline%0A++cell1+dummy+text+dummy+text+dummy+text%26+cell2+%26+cell3+%5C%5C+%0A++%5Chline%0A++cell1+dummy+text+dummy+text+dummy+text+%26+cell5+%26+cell6+%5C%5C+%0A++%5Chline%0A++cell7+%26+cell8+%26+cell9+%5C%5C+%0A++%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D)

![固定寬度表格範例](/files/3416fdd2339df5651561796d36af4d5e84a5973d)

在 `tabular` 環境中，參數 `m{5em}` 設定寬度為 `5em` 用於第一欄（`1cm` 用於另外兩欄），並將文字置於儲存格中央。對齊選項為 `m` 表示中間， `p` 表示頂端，而 `b` 表示底端。使用這些參數時，文字會自動格式化以符合每個儲存格的大小。

如果你不需要控制每個儲存格的寬度，而是要控制整個表格的寬度並在其中平均分配空間，請使用 `tabularx` 套件。請參見下方範例：

```latex
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{0.8\textwidth} {
  | >{\raggedright\arraybackslash}X
  | >{\centering\arraybackslash}X
  | >{\raggedleft\arraybackslash}X | }
 \hline
 item 11 & item 12 & item 13 \\
 \hline
 item 21  & item 22  & item 23  \\
\hline
\end{tabularx}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Table+example+using+tabularx+package\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btabularx%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btabularx%7D%7B0.8%5Ctextwidth%7D+%7B+%0A++%7C+%3E%7B%5Craggedright%5Carraybackslash%7DX+%0A++%7C+%3E%7B%5Ccentering%5Carraybackslash%7DX+%0A++%7C+%3E%7B%5Craggedleft%5Carraybackslash%7DX+%7C+%7D%0A+%5Chline%0A+item+11+%26+item+12+%26+item+13+%5C%5C%0A+%5Chline%0A+item+21++%26+item+22++%26+item+23++%5C%5C%0A%5Chline%0A%5Cend%7Btabularx%7D%0A%5Cend%7Bdocument%7D)

![使用 tabularx 套件的表格範例](/files/4dcbedc32b99b167777dec257814a99799a842e3)

環境 `tabularx` 類似於 `tabular` 但更具彈性。要使用它，請加入以下這一行 `\usepackage{tabularx}` 到文件前言。請注意，環境的開頭語句不同，在範例中表格寬度設定為 `0.8\textwidth`，也就是文件文字寬度的 80%。你可以使用任何 [LaTeX 單位](/latex/zh-tw/ge-shi-hua/01-lengths-in-latex.md) 來設定該值。

大括號內的前綴 `| >{\raggedright\arraybackslash}X | >{\centering\arraybackslash}X | >{\raggedleft\arraybackslash}X |` 設定每欄的對齊方式：第一欄為 *left*，第二欄為 *center* 而第三欄為 *right*.

## 合併列與欄

列與欄可以合併以建立較大的儲存格。以下範例使用 `\multicolumn` 指令來合併多個欄位：

```latex
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|  }
 \hline
 \multicolumn{4}{|c|}{國家清單} \\
 \hline
 國家名稱或地區名稱& ISO ALPHA 2 代碼 &ISO ALPHA 3 代碼&ISO 數字代碼\\
 \hline
 阿富汗   & AF    &AFG&   004\\
 奧蘭群島&   AX  & ALA   &248\\
 阿爾巴尼亞 &AL & ALB&  008\\
 阿爾及利亞    &DZ & DZA&  012\\
 美屬薩摩亞&   AS  & ASM&016\\
 安道爾& AD  & AND   &020\\
 安哥拉& AO  & AGO&024\\
 \hline
\end{tabular}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+table+using+the+multicolumn+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bmultirow%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btabular%7D%7B+%7Cp%7B3cm%7D%7C%7Cp%7B3cm%7D%7Cp%7B3cm%7D%7Cp%7B3cm%7D%7C++%7D%0A+%5Chline%0A+%5Cmulticolumn%7B4%7D%7B%7Cc%7C%7D%7BCountry+List%7D+%5C%5C%0A+%5Chline%0A+Country+Name+or+Area+Name%26+ISO+ALPHA+2+Code+%26ISO+ALPHA+3+Code%26ISO+numeric+Code%5C%5C%0A+%5Chline%0A+Afghanistan+++%26+AF++++%26AFG%26+++004%5C%5C%0A+Aland+Islands%26+++AX++%26+ALA+++%26248%5C%5C%0A+Albania+%26AL+%26+ALB%26++008%5C%5C%0A+Algeria++++%26DZ+%26+DZA%26++012%5C%5C%0A+American+Samoa%26+++AS++%26+ASM%26016%5C%5C%0A+Andorra%26+AD++%26+AND+++%26020%5C%5C%0A+Angola%26+AO++%26+AGO%26024%5C%5C%0A+%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bdocument%7D)

![使用 multicolumn 指令的表格範例](/files/a31fa199c8b1e0330d6420d422952b846528ea4a)

讓我們逐一檢視該指令的每個組成部分 `\multicolumn{4}{|c|}{國家清單} \\`:

**{4}**

要合併的欄數：此例為 4。

**{|c|}**

結果儲存格的分隔符號與對齊方式：在此情況下，文字會置中，且儲存格兩側都會畫上垂直線。

**{國家清單}**

要顯示在儲存格內的文字。

若要合併列，你需要加入 `multirow` 套件到你的文件前言：

```latex
\usepackage{multirow}
```

接著你可以使用 `\multirow` 指令於文件中：

```latex
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
col1 & col2 & col3 \\
\hline
\multirow{3}{4em}{Multiple row} & cell2 & cell3 \\
& cell5 & cell6 \\
& cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Multirow+table+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bmultirow%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A%5Cbegin%7Btabular%7D%7B+%7Cc%7Cc%7Cc%7C+%7D+%0A%5Chline%0Acol1+%26+col2+%26+col3+%5C%5C%0A%5Chline%0A%5Cmultirow%7B3%7D%7B4em%7D%7BMultiple+row%7D+%26+cell2+%26+cell3+%5C%5C+%0A%26+cell5+%26+cell6+%5C%5C+%0A%26+cell8+%26+cell9+%5C%5C+%0A%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D)

![使用 multirow 指令的表格範例](/files/ceeb9c13b3841f666e27443bc6d368b65a7b8e5d)

該 `multirow` 指令需要三個參數；我們的範例使用以下設定：

1. 要合併的列數： `3`
2. 欄位寬度： `4em`
3. 儲存格內容： `多列`

## 多頁表格

如果你必須插入一個非常長的表格，而它在文件中佔用兩頁或更多頁，請使用 `longtable` 套件。首先，將下列這一行加入你的文件前言：

```latex
\usepackage{longtable}
```

接著你可以使用 `longtable` 環境，如以下範例所示：

```latex
\documentclass{article}
\usepackage{longtable}

\begin{document}

 \begin{longtable}[c]{| c | c |}
 \caption{Long table caption.\label{long}}\\

 \hline
 \multicolumn{2}{| c |}{Begin of Table}\\
 \hline
 Something & something else\\
 \hline
 \endfirsthead

 \hline
 \multicolumn{2}{|c|}{Continuation of Table \ref{long}}\\
 \hline
 Something & something else\\
 \hline
 \endhead

 \hline
 \endfoot

 \hline
 \multicolumn{2}{| c |}{End of Table}\\
 \hline\hline
 \endlastfoot

 Lots of lines & like this\\
 Lots of lines & like this\\
 Lots of lines & like this\\
 Lots of lines & like this\\
 Lots of lines & like this\\
 Lots of lines & like this\\
 Lots of lines & like this\\
 Lots of lines & like this\\
 ...
 Lots of lines & like this\\
 \end{longtable}
```

![含大量列的表格範例](/files/13c01b6df3fbf61f1c8deaac796a7b3d47686153)

[開啟一個 `longtable` 範例](https://www.overleaf.com/project/new/template/20823?id=71401569\&templateName=Longtable+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

的行為 `longtable` 類似於預設的 `tabular`，但會產生可由標準 LaTeX 分頁演算法拆分的表格。有四個 `longtable`專屬元素：

**\endfirsthead**

此指令上方的所有內容都會出現在表格開頭，也就是第一頁。

**\endhead**

在這個指令之前以及 \endfirsthead 下方所放置的任何內容，都會顯示在每一頁表格的頂端，但第一頁除外。

**\endfoot**

類似於 \endhead，在 \endhead 之後且在此指令之前所放置的內容，會出現在每一頁表格的底端，但最後一頁除外。

**\endlastfoot**

類似於 \endfirsthead。\endfoot 之後且在此指令之前的元素，會顯示在表格底端，但僅限於表格出現的最後一頁。

## 表格定位

如果表格位於浮動體中，定位表格就很容易 `table` 環境中。

```latex
\documentclass{article}
\begin{document}
下面是一個恰好定位在此處的表格：
\begin{table}[h!]
\centering
 \begin{tabular}{||c c c c||}
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [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}
\end{table}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Positioning+a+table\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0ABelow+is+a+table+positioned+exactly+here%3A%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%5Cend%7Btable%7D%0A%5Cend%7Bdocument%7D)

![表格定位範例](/files/bea6acdd7413cdf02e731d7b1819cd33fb793cad)

參數 `h!` 傳入表格環境宣告的，決定此表格必須被放置在 *這裡*，並覆寫 LaTeX 預設值。可傳入的定位參數包括：

**h**

會將表格大致放在此處。

**的區域變數中**

將表格放在頁面頂端。

**b**

將表格放在頁面底端。

**p**

將表格放在僅供表格使用的專頁中。

**!**

覆寫內部 LaTeX 參數。

**H**

將表格放在這個精確位置，幾乎就像 h! 一樣。

如需更多表格定位範例，請參見 [圖片與表格的定位](/latex/zh-tw/tu-biao-yu-biao-ge/02-positioning-images-and-tables.md) 這篇文章。

在此範例中還有幾個指令：

**\centering**

讓表格相對於浮動容器元素置中。

**\[1ex]**

這會為儲存格增加額外空間。

## 標題、標籤與參照

表格可以透過以下方式加入標題、標籤並被引用： `table` 環境中。

```latex
\documentclass{article}
\begin{document}
表格 \ref{table:1} 是一個被引用的 \LaTeX{} 元素範例。

\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [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:1}
\end{table}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Table+captions+and+labels\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0ATable+%5Cref%7Btable%3A1%7D+is+an+example+of+a+referenced+%5CLaTeX%7B%7D+element.%0A%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%3A1%7D%0A%5Cend%7Btable%7D%0A%5Cend%7Bdocument%7D)

![被引用的 LaTeX 元素範例](/files/c670b37fc9d699445f1ad306df93d084fe251d47)

這個範例中有三個重要指令：

**\caption{Table to test captions and labels}**

如你所料，這個指令會設定表格標題。如果你建立表格清單，這個標題也會用於其中。你可以將它放在表格上方或下方。

**\label{table:1}**

如果你需要在文件中引用該表格，請用這個指令設定標籤。標籤會為表格編號，並與 \ref 指令結合後，讓你能夠引用它。

**\ref{table:1}**

這段程式碼會被替換成對應於所引用表格的編號。

## 表格清單

若要建立表格清單，請使用 `\listoftables` 指令。每個表格的標題都會用來產生此清單。對於支援以下語言的 `babel` 套件，「List of tables」這個標題會相應翻譯。請參閱關於 [國際語言支援](/latex/zh-tw/yu-yan/03-international-language-support.md) 的文章以取得更多資訊。

```latex
\documentclass{article}
\begin{document}
\listoftables
\vspace{5pt}
表格 \ref{table:1} 是被引用 \LaTeX{} 元素的範例。

\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [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{This is the caption for the first table.}
\label{table:1}
\end{table}

\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
 \hline\hline
  4 & 545 & 18744 & 7560 \\
 5 & 88 & 788 & 6344 \\ [1ex]
 \hline
\end{tabular}
\caption{This is the caption for the second table.}
\label{table:2}
\end{table}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+using+listoftables\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Clistoftables%0A%5Cvspace%7B5pt%7D%0AThe+table+%5Cref%7Btable%3A1%7D+is+an+example+of+referenced+%5CLaTeX%7B%7D+elements.%0A%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%7BThis+is+the+caption+for+the+first+table.%7D%0A%5Clabel%7Btable%3A1%7D%0A%5Cend%7Btable%7D%0A%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++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%7BThis+is+the+caption+for+the+second+table.%7D%0A%5Clabel%7Btable%3A2%7D%0A%5Cend%7Btable%7D%0A%5Cend%7Bdocument%7D)

![表格清單範例](/files/91660b5f1cfc567d28f4c4ebde684a6a5ea36e0b)

## 變更表格外觀

可以修改表格中的多個元素，以符合文件需求。下面你將學到如何修改線條粗細、線條顏色以及表格儲存格的背景顏色。

### 線條寬度與儲存格內距

透過增加欄間距與列高，表格的可讀性有時可以提升。

```latex
\documentclass{article}
\setlength{\arrayrulewidth}{0.5mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.5}
\begin{document}
\begin{tabular}{ |p{3cm}|p{3cm}|p{3cm}|  }
\hline
\multicolumn{3}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 \\
\hline
阿富汗 & AF &AFG \\
奧蘭群島 & AX   & ALA \\
阿爾巴尼亞 &AL & ALB \\
阿爾及利亞    &DZ & DZA \\
美屬薩摩亞 & AS & ASM \\
安道爾 & AD & AND   \\
安哥拉 & AO & AGO \\
\hline
\end{tabular}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Changing+table+spacing\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Csetlength%7B%5Carrayrulewidth%7D%7B0.5mm%7D%0A%5Csetlength%7B%5Ctabcolsep%7D%7B18pt%7D%0A%5Crenewcommand%7B%5Carraystretch%7D%7B1.5%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btabular%7D%7B+%7Cp%7B3cm%7D%7Cp%7B3cm%7D%7Cp%7B3cm%7D%7C++%7D%0A%5Chline%0A%5Cmulticolumn%7B3%7D%7B%7Cc%7C%7D%7BCountry+List%7D+%5C%5C%0A%5Chline%0ACountry+Name+or+Area+Name%26+ISO+ALPHA+2+Code+%26ISO+ALPHA+3+%5C%5C%0A%5Chline%0AAfghanistan+%26+AF+%26AFG+%5C%5C%0AAland+Islands+%26+AX+++%26+ALA+%5C%5C%0AAlbania+%26AL+%26+ALB+%5C%5C%0AAlgeria++++%26DZ+%26+DZA+%5C%5C%0AAmerican+Samoa+%26+AS+%26+ASM+%5C%5C%0AAndorra+%26+AD+%26+AND+++%5C%5C%0AAngola+%26+AO+%26+AGO+%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bdocument%7D)

![欄間距範例](/files/f8884dafd8a03243a1027c1370f45c6d4c9507d9)

以下提供這些指令的說明：

**\setlength{\arrayrulewidth}{0.5mm}**

這會設定表格邊框的粗細。在範例中是 0.5mm，但你可以使用其他單位——完整清單請參見 LaTeX 中的長度一文。

**\setlength{\tabcolsep}{18pt}**

這個指令將文字與其所在儲存格左右邊框之間的空間設定為 18pt。同樣地，如有需要也可使用其他單位。

**\renewcommand{\arraystretch}{1.5}**

每一列的高度設定為其預設高度的 1.5 倍。

### 交替列著色

你可以使用 `xcolor` 套件搭配 `table` 選項，以下範例可加以說明：

```latex
\documentclass{article}
\usepackage[table]{xcolor}
\setlength{\arrayrulewidth}{0.5mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{2.5}
\begin{document}
{\rowcolors{3}{green!80!yellow!50}{green!70!yellow!40}
\begin{tabular}{ |p{3cm}|p{3cm}|p{3cm}|  }
\hline
\multicolumn{3}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 \\
\hline
阿富汗 & AF &AFG \\
奧蘭群島 & AX   & ALA \\
阿爾巴尼亞 &AL & ALB \\
阿爾及利亞    &DZ & DZA \\
美屬薩摩亞 & AS & ASM \\
安道爾 & AD & AND   \\
安哥拉 & AO & AGO \\
\hline
\end{tabular}}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Changing+row+colours\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Btable%5D%7Bxcolor%7D%0A%5Csetlength%7B%5Carrayrulewidth%7D%7B0.5mm%7D%0A%5Csetlength%7B%5Ctabcolsep%7D%7B18pt%7D%0A%5Crenewcommand%7B%5Carraystretch%7D%7B2.5%7D%0A%5Cbegin%7Bdocument%7D%0A%7B%5Crowcolors%7B3%7D%7Bgreen%2180%21yellow%2150%7D%7Bgreen%2170%21yellow%2140%7D%0A%5Cbegin%7Btabular%7D%7B+%7Cp%7B3cm%7D%7Cp%7B3cm%7D%7Cp%7B3cm%7D%7C++%7D%0A%5Chline%0A%5Cmulticolumn%7B3%7D%7B%7Cc%7C%7D%7BCountry+List%7D+%5C%5C%0A%5Chline%0ACountry+Name+or+Area+Name%26+ISO+ALPHA+2+Code+%26ISO+ALPHA+3+%5C%5C%0A%5Chline%0AAfghanistan+%26+AF+%26AFG+%5C%5C%0AAland+Islands+%26+AX+++%26+ALA+%5C%5C%0AAlbania+%26AL+%26+ALB+%5C%5C%0AAlgeria++++%26DZ+%26+DZA+%5C%5C%0AAmerican+Samoa+%26+AS+%26+ASM+%5C%5C%0AAndorra+%26+AD+%26+AND+++%5C%5C%0AAngola+%26+AO+%26+AGO+%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%7D%0A%5Cend%7Bdocument%7D)

![表格列著色範例](/files/dade14171de8aa5a210df7e803088b723503ccdb)

請注意指令前方的大括號

```latex
\rowcolors{3}{green!80!yellow!50}{green!70!yellow!40}
```

以及在……之後的 `tabular` 環境。指令 `\rowcolors` 需要三個參數，且各自都以大括號傳入：

* 起始列，
* 奇數列的顏色，以及
* 偶數列的顏色。

請參閱 `xcolor` 套件說明文件（在 [延伸閱讀](#further-reading) 章節）中可查看可用顏色清單以及如何建立自訂顏色。範例中將綠色與黃色以不同比例混合。

若要讓指令生效，請務必加入

```latex
\usepackage[table]{xcolor}
```

到你的 LaTeX 檔案前言。

### 為表格著色（儲存格、列、欄與線條）

表格中的所有元素都可以自訂為特定顏色。同樣地，這項功能由 **xcolor** 提供，因此你必須加入

```latex
\usepackage[table]{xcolor}
```

到前言中。下面你可以看到一個範例。

```latex
\documentclass{article}
\usepackage[table]{xcolor}
\setlength{\arrayrulewidth}{1mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{2.5}
\newcolumntype{s}{>{\columncolor[HTML]{AAACED}} p{3cm}}
\arrayrulecolor[HTML]{DB5800}
\begin{document}
\begin{tabular}{ |s|p{3cm}|p{3cm}| }
\hline
\rowcolor{lightgray} \multicolumn{3}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 \\
\hline
阿富汗 & AF &AFG \\
\rowcolor{gray}
奧蘭群島 & AX & ALA \\
阿爾巴尼亞   &AL & ALB \\
阿爾及利亞  &DZ & DZA \\
美屬薩摩亞 & AS & ASM \\
安道爾 & AD & \cellcolor[HTML]{AA0044} AND    \\
安哥拉 & AO & AGO \\
\hline
\end{tabular}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+using+colour+in+a+table\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Btable%5D%7Bxcolor%7D%0A%5Csetlength%7B%5Carrayrulewidth%7D%7B1mm%7D%0A%5Csetlength%7B%5Ctabcolsep%7D%7B18pt%7D%0A%5Crenewcommand%7B%5Carraystretch%7D%7B2.5%7D%0A%5Cnewcolumntype%7Bs%7D%7B%3E%7B%5Ccolumncolor%5BHTML%5D%7BAAACED%7D%7D+p%7B3cm%7D%7D%0A%5Carrayrulecolor%5BHTML%5D%7BDB5800%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btabular%7D%7B+%7Cs%7Cp%7B3cm%7D%7Cp%7B3cm%7D%7C+%7D%0A%5Chline%0A%5Crowcolor%7Blightgray%7D+%5Cmulticolumn%7B3%7D%7B%7Cc%7C%7D%7BCountry+List%7D+%5C%5C%0A%5Chline%0ACountry+Name+or+Area+Name%26+ISO+ALPHA+2+Code+%26ISO+ALPHA+3+%5C%5C%0A%5Chline%0AAfghanistan+%26+AF+%26AFG+%5C%5C%0A%5Crowcolor%7Bgray%7D%0AAland+Islands+%26+AX+%26+ALA+%5C%5C%0AAlbania+++%26AL+%26+ALB+%5C%5C%0AAlgeria++%26DZ+%26+DZA+%5C%5C%0AAmerican+Samoa+%26+AS+%26+ASM+%5C%5C%0AAndorra+%26+AD+%26+%5Ccellcolor%5BHTML%5D%7BAA0044%7D+AND++++%5C%5C%0AAngola+%26+AO+%26+AGO+%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bdocument%7D)

![表格中自訂顏色範例](/files/df44e07a5a9ddb3794de5229f101c3a1c0d10524)

以下說明如何變更表格中每個元素的顏色：

* **線條顏色**。指令 `\arrayrulecolor` 用於此目的。在範例中使用 HTML 格式，但也有其他格式可用，完整清單請參閱 xcolor 說明文件（連結提供於 [延伸閱讀](#further-reading) 章節）。
* **儲存格的背景顏色**。使用指令 `\cellcolor`。你可以直接在大括號內輸入顏色名稱（如 red、gray、green 等），或在方括號內傳入格式參數（範例中的 HTML），然後使用既定格式在大括號內設定所需顏色。
* **列的背景顏色**。在這種情況下 `\rowcolor` 即可達成。前兩個指令中提到的顏色選擇注意事項，對這個指令同樣適用。
* **欄的背景顏色**。這個稍微有點棘手，但最簡單的方法是定義一種新的欄型。指令

  ```latex
  \newcolumntype{s}{>{\columncolor[HTML]{AAACED}} p{3cm}}
  ```

  定義一種名為 `s` 且其對齊方式為 `p`，欄寬為 `3cm` 而顏色則以 HTML 格式設定為 `AAACED`。這個新的欄型用於 `tabular` 環境中。

## 參考指南

以下是……中參數的簡要說明 `tabular` 環境中。

可以使用以下方式建立表格 `tabular` 環境中。

```
\begin{tabular}[pos]{cols}
 表格內容
\end{tabular}
```

其中選項可以是：

* **`pos`** ：垂直位置。它可以採用以下值：

|            |              |
| ---------- | ------------ |
| `的區域變數中`   | 上方的線與文字基線對齊  |
| `b`        | 下方的線與文字基線對齊  |
| `c` 或 none | 表格以文字基線為中心對齊 |

* **`cols`** ：定義每欄的對齊方式與邊框。它可以有以下值：

|                |                                                    |
| -------------- | -------------------------------------------------- |
| `l`            | 靠左對齊欄                                              |
| `c`            | 置中欄                                                |
| `r`            | 靠右對齊欄                                              |
| `p{'width'}`   | 段落欄，文字垂直頂端對齊                                       |
| `m{'width'}`   | 段落欄，文字垂直置中對齊（需要 `array` 套件中的可用數學符號完整清單）            |
| `b{'width'}`   | 段落欄，文字垂直底端對齊（需要 `array` 套件中的可用數學符號完整清單）            |
| `\|`           | 垂直線                                                |
| `\|\|`         | 雙垂直線                                               |
| `*{num}{form}` | 格式形式會重複 *num* 次；例如 `*{3}{\|l}\|` 等同於 `\|l\|l\|l\|` |

若要分隔儲存格並插入新列，請使用以下指令：

|               |                                              |
| ------------- | -------------------------------------------- |
| `&`           | 欄分隔符號                                        |
| `\\`          | 開始新列（可在以下位置後指定額外空間： `\\` 使用方括號，例如 `\\[6pt]`) |
| `\hline`      | 列與列之間的水平線                                    |
| `\newline`    | 在儲存格內開始新行（在段落欄中）                             |
| `\cline{i-j}` | 部分水平線，起始於欄 *i* 並結束於欄 *j*                     |

## 延伸閱讀

更多資訊請參見

* [如何在 Overleaf 中插入表格](/latex/zh-tw/zhi-shi-ku/067-how-to-generate-and-insert-latex-tables-in-overleaf.md)
* [圖片與表格的定位](/latex/zh-tw/tu-biao-yu-biao-ge/02-positioning-images-and-tables.md)
* [表格與圖形清單](/latex/zh-tw/tu-biao-yu-biao-ge/03-lists-of-tables-and-figures.md)
* [LaTeX 中的長度](/latex/zh-tw/ge-shi-hua/01-lengths-in-latex.md)
* [在 LaTeX 中使用顏色](/latex/zh-tw/ge-shi-hua/13-using-colors-in-latex.md)
* [xcolor 套件手冊](http://mirrors.ctan.org/macros/latex/contrib/xcolor/xcolor.pdf)
* [tabularx 套件手冊](https://texdoc.org/pkg/tabularx)
* [longtable 套件手冊](http://mirrors.ctan.org/macros/latex/required/tools/longtable.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/tu-biao-yu-biao-ge/01-tables.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.
