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

# 表格

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

1. 使用 [**插入表格** 按钮](/latex/zh-cn/zhi-shi-ku/067-how-to-generate-and-insert-latex-tables-in-overleaf.md#inserttable) 在编辑器工具栏中。
2. 在使用……时，从另一个文档复制并粘贴表格 **可视化编辑器**.
3. 在……中编写该表格的 LaTeX 代码 **代码编辑器**.

如果你刚接触 LaTeX，使用 [……中的工具栏 **可视化编辑器**](/latex/zh-cn/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/eac41a24f448b16511667aafee6ae86e44f4639a)

该 `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/76bf47db673e1805d3e29e9901645e27ba767d7d)

下面是上面示例中所使用结构的说明：

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

这表示表格中将使用三列，并且列与列之间由竖线分隔。每个 c 表示该列内容居中。你也可以使用 r 让文本右对齐，使用 l 让文本左对齐。

**\hline**

这会在表格顶部和底部添加一条水平线。\hline 的使用次数没有限制。

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

每个 & 都是一个单元格分隔符，而双反斜杠 \\\ 表示这一行结束。

下面你可以看到第二个示例，它使用了各种竖线和横线（通常称为“规则线”）：

```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/fb6b367ab938dfe0e3d7fd0fdc43938aaf79769e)

## 固定宽度的表格

在格式化表格时，你可能需要固定宽度，无论是每一列还是整个表格。下面的示例添加了 `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/02b3bf892faca338b2f97495a51f99d884227169)

在 `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/c750a7864d3a2bb08af077adb2d39f30c7c49574)

环境 `tabularx` 类似于 `tabular` ，但更灵活。要使用它，请添加以下行 `\usepackage{tabularx}` 到文档导言区。请注意，该环境的起始声明不同，在示例中表格宽度设置为 `0.8\textwidth`，也就是文档正文宽度的 80%。你可以使用任意一种 [LaTeX 单位](/latex/zh-cn/ge-shi-hua/01-lengths-in-latex.md) 来设置该值。

花括号中的前缀 `| >{\raggedright\arraybackslash}X | >{\centering\arraybackslash}X | >{\raggedleft\arraybackslash}X |` 设置每列的对齐方式：第一列为 *左侧*，第二列为 *center* ，第三列为 *右侧*.

## 合并行和列

行和列可以合并以创建更大的表格单元格。下面的示例使用 `\multicolumn` 命令来合并多个列：

```latex
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|  }
 \hline
 \multicolumn{4}{|c|}{Country List} \\
 \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/880ae8814320d513fd819cd25726933136970e53)

让我们逐一查看该命令的各个组成部分 `\multicolumn{4}{|c|}{Country List} \\`:

**{4}**

要合并的列数：在此示例中为 4。

**{|c|}**

结果单元格的分隔符和对齐方式：在此情况下文本将居中，并且在单元格两侧都会画竖线。

**{Country List}**

显示在单元格中的文本。

要合并行，你需要添加 `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/fef353228ae0f00b7282232c1cfa5aa41b5a3688)

该 `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/74ee6d1ce39ce6bd90ce17f6e940b7832d76eb4d)

[打开一个 `longtable` Overleaf 中的示例](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/ce8272aec4c39dc5de8e8c79b29ada83b0282485)

参数 `h!` 传递给 table 环境声明的参数指定该表格必须放置在 *此处*，并覆盖 LaTeX 默认设置。可传入的定位参数包括：

**h**

大致将表格放在这里。

**t**

将表格放在页面顶部。

**b**

将表格放在页面底部。

**p**

将表格放在专门的页面中，仅用于表格。

**!**

覆盖 LaTeX 内部参数。

**H**

将表格放在这个精确位置，和 h! 差不多。

有关表格定位的更多示例，请参见 [图片和表格的定位](/latex/zh-cn/tu-xing-he-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{Table to test captions and labels.}
\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/a7c60b1375cb360b212d2cd14bd37a4117aa376b)

示例中有三个重要命令：

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

正如你所预期的，这个命令为表格设置标题。如果你创建表格目录，这个标题会用于那里。你可以把它放在表格上方或下方。

**\label{table:1}**

如果你需要在文档中引用该表格，请使用此命令设置标签。标签会为表格编号，并与 \ref 命令结合后即可引用它。

**\ref{table:1}**

这段代码将被替换为对应被引用表格的编号。

## 表格目录

要创建表格目录，请使用 `\listoftables` 命令。每个表格的标题将用于生成此列表。对于……支持的语言， `babel` 宏包，标题“List of tables”会自动翻译为相应语言。请参见关于 [国际语言支持](/latex/zh-cn/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/525c769a4876c1f503386fc4d9814aad3a409931)

## 更改表格外观

可以根据文档需要修改表格的若干元素。下面你将学习如何修改表格中线条的粗细、线条颜色以及单元格背景色。

### 线宽和单元格内边距

通过增加列间距和行距，表格的可读性有时可以得到改善。

```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
国家名称或地区名称& 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/15c4badb97a1299f9654374a3c80f6e2e5030d5f)

下面给出这些命令的说明：

**\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
国家名称或地区名称& 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/2c6fe260052ceaf83bdf75a711848e8ba47a9f38)

请注意，在命令

```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
国家名称或地区名称& 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/5a470f6c56002554d2563d93cb82edd3fde50d7b)

下面将说明如何更改表格中每个元素的颜色：

* **线条颜色**。命令 `\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`** ：垂直位置。它可以取以下值：

|        |             |
| ------ | ----------- |
| `t`    | 顶部的线与文本基线对齐 |
| `b`    | 底部的线与文本基线对齐 |
| `c` 或无 | 表格相对于文本基线居中 |

* **`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-cn/zhi-shi-ku/067-how-to-generate-and-insert-latex-tables-in-overleaf.md)
* [图片和表格的定位](/latex/zh-cn/tu-xing-he-biao-ge/02-positioning-images-and-tables.md)
* [表格和图形列表](/latex/zh-cn/tu-xing-he-biao-ge/03-lists-of-tables-and-figures.md)
* [LaTeX 中的长度](/latex/zh-cn/ge-shi-hua/01-lengths-in-latex.md)
* [在 LaTeX 中使用颜色](/latex/zh-cn/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://ayakaleaf-pro.ayaka.space/latex/zh-cn/tu-xing-he-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.
