> 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-cn/tu-biao-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 \\\\**

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

下面你可以看到第二个示例，其中使用了各种竖线和横线（通常称为“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/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 |` 设置每一列的对齐方式：第一列为 *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/880ae8814320d513fd819cd25726933136970e53)

让我们逐一查看该命令的各个组成部分 `\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/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{长表标题。\label{long}}\\

 \hline
 \multicolumn{2}{| c |}{表格开始}\\
 \hline
 Something & something else\\
 \hline
 \endfirsthead

 \hline
 \multicolumn{2}{|c|}{表格续页 \ref{long}}\\
 \hline
 Something & something else\\
 \hline
 \endhead

 \hline
 \endfoot

 \hline
 \multicolumn{2}{| c |}{表格结束}\\
 \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!` 传递给表格环境声明后，表示该表格必须放置在 *这里*位置，并覆盖 LaTeX 默认设置。可传入的位置参数包括：

**h**

将表格大致放在这里。

**t**

将表格放在页面顶部。

**b**

将表格放在页面底部。

**p**

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

**!**

覆盖内部 LaTeX 参数。

**H**

将表格放在这个精确位置，几乎与 h! 一样。

关于表格定位的更多示例请参见 [图片和表格的位置](/latex/zh-cn/tu-biao-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{用于测试标题和标签的表格。}
\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{用于测试标题和标签的表格}**

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

**\label{table:1}**

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

**\ref{table:1}**

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

## 表格目录

要创建表格目录，请使用 `\listoftables` 命令。每个表格的标题都会用于生成此目录。对于 `babel` 包支持的语言，标题“表格目录”会相应翻译。请参见关于 [国际语言支持](/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{这是第一张表格的标题。}
\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{这是第二张表格的标题。}
\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|}{国家列表} \\
\hline
国家或地区名称& ISO ALPHA 2 代码 &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 中的 Lengths 文章。

**\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|}{国家列表} \\
\hline
国家或地区名称& ISO ALPHA 2 代码 &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|}{国家列表} \\
\hline
国家或地区名称& ISO ALPHA 2 代码 &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}}
  ```

  定义一个名为 `制` 的列类型，其对齐方式为 `p`，列宽为 `3cm` ，颜色使用 HTML 格式设置为 `AAACED`。这个新列类型用于 `tabular` 环境中。

## 参考指南

中参数的简要说明 `tabular` 环境中。

可以使用 `tabular` 环境中。

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

，其中选项可以是：

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

|            |             |
| ---------- | ----------- |
| `t`        | 顶部的线与文本基线对齐 |
| `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* 列开始并在第 *列结束的部分水平线*                |

## 进一步阅读

更多信息请参见

* [如何在 Overleaf 中插入表格](/latex/zh-cn/zhi-shi-ku/067-how-to-generate-and-insert-latex-tables-in-overleaf.md)
* [图片和表格的位置](/latex/zh-cn/tu-biao-he-biao-ge/02-positioning-images-and-tables.md)
* [表格与图形列表](/latex/zh-cn/tu-biao-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://overleaf-pro.ayaka.space/latex/zh-cn/tu-biao-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.
