介绍
本文介绍如何使用 LaTeX 创建和自定义表格:更改大小/间距、组合单元格、将颜色应用于行或单元格等。
我们可以从最简单的表格示例之一开始:
\begin{center}
\begin{tabular}{ c c c }
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9
\end{tabular}
\end{center}
环境tabular
是LATEX创建表的默认方法。您必须为此环境指定一个参数;这里我们使用{c c c}
告诉 LaTeX 有三列并且每一列中的文本必须居中。
在LATEX中创建一个简单的表
环境tabular
提供了额外的灵活性;例如,您可以在每列之间放置分隔线:
\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
以下是上面示例中使用的结构的描述:
{ |c|c|c| }
这声明将在表中使用由垂直线分隔的三列。每个都c意味着该列的内容将居中。您还可以使用r
将文本右对齐和l
左对齐。
\hline
这将在表格顶部和底部插入一条水平线。您可以使用\hline
的次数没有限制。
cell1 & cell2 & cell3 \\
每个&
都是一个单元格分隔符,双反斜杠\\
设置该行的结尾。
下面你可以看到第二个例子,它使用了各种垂直和水平线(通常称为“规则”):
\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}
固定长度的表格
格式化表格时,您可能需要每列或整个表格的固定长度。以下示例将array
包添加到文档序言:
\usepackage{array}
并使用它来设置固定的列宽:
\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}
在tabular
环境中,该参数m{5em}
设置第一列的长度为5em(其他两列1cm)并将文本居中在单元格的中间。对齐选项用于中间、顶部和底部。使用这些参数时,文本会自动格式化以适合每个单元格。
如果您不需要控制每个单元格的宽度,而是整个表格的宽度,然后在其中均匀分配空间,请使用tabularx
包。请参见下面的示例:
\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}
环境tabularx
类似于tabular
但更灵活,要使用它,请在文档序言中添加该行\usepackage{tabularx}
。请注意,环境打开语句是不同的,在示例中,表格宽度设置为0.8\textwidth
,即文档文本宽度的 80%。您可以使用任何LATEX单位来设置此长度。
大括号内的前缀
| >{\raggedright\arraybackslash}X
| >{\centering\arraybackslash}X
| >{\raggedleft\arraybackslash}X |
设置每列的对齐方式:第一个到left,第二个到center,第三个到right。
组合行和列
\multicolumn
可以合并行和列以创建更大的表格单元格。以下示例使用该命令合并多个列:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}| }
\hline
\multicolumn{4}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 Code&ISO numeric Code\\
\hline
Afghanistan & AF &AFG& 004\\
Aland Islands& AX & ALA &248\\
Albania &AL & ALB& 008\\
Algeria &DZ & DZA& 012\\
American Samoa& AS & ASM&016\\
Andorra& AD & AND &020\\
Angola& AO & AGO&024\\
\hline
\end{tabular}
\end{document}
让我们回顾一下命令\multicolumn{4}{|c|}{Country List} \\
的每个组件
{4}
要组合的列数:在这种情况下为 4。
{|c|}
结果单元格的分隔符和对齐方式:在这种情况下,文本将居中,并且将在单元格的每一侧绘制一条垂直线。
{Country List}
要在单元格内显示的文本。
要合并行,您需要将multirow
包添加到文档序言中:
\usepackage{multirow}
然后,您可以在文档中使用该命令\multirow
:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{center}
\begin{tabular}{ |c|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}
该multirow
命令采用三个参数;我们的示例使用以下设置:
- 要组合的行数:3
- 列的宽度:4em
- 单元格的内容:Multiple row
多页表
如果您必须插入一个非常长的表格,该表格在文档中占据两页或更多页,请使用该longtable
包。首先,将以下行添加到您的文档序言中:
\usepackage{longtable}
然后您可以使用该longtable
环境,如以下示例所示:
\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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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\\
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}
\end{document}
longtable
的行为类似于tabular
默认值,但生成的表可以被标准的LATEX分页算法打破。有四个longtable
特定的元素:
\endfirsthead
此命令之上的所有内容都将出现在表格的开头,在第一页。
\endhead
您在此命令之前及\endfirsthead
以下放置的任何内容都将显示在除第一页之外的每一页的表格顶部。
\endfoot
与\endhead
类似,您在\endhead
命令之后和\endfoot
命令之前放置的内容将出现在除最后一页之外的每一页的表格底部。
\endlastfoot
类似于\endfirsthead
。\endfoot
命令之后和\endlastfoot
之前的元素将显示在表格的底部,但仅显示在表格出现的最后一页中。
定位表
如果表位于浮动table
环境中,则放置表很容易。
\documentclass{article}
\begin{document}
Below is a table positioned exactly here:
\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}
传递给表环境声明的参数h!
确定此表必须放在此处,并覆盖L A T E X默认值。可以传入的定位参数包括:
h
大约将表放在这里。
t
将表格放在页面顶部。
b
将表格放在页面底部。
p
将表格放在特殊页面中,仅用于表格。
!
覆盖内部L A T E X参数。
H
把桌子放在这个精确的位置,很像 h!
。
有关表格定位的更多示例,请参阅定位图像和表格文章。
在此示例中,还有一些命令:
\centering
相对于浮动容器元素使表格居中。
\[1ex]
这为单元格增加了额外的空间。
标题、标签和参考
可以通过table
环境对表格进行标题、标记和引用。
\documentclass{article}
\begin{document}
Table \ref{table:1} is an example of a referenced \LaTeX{} element.
\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}
示例中有三个重要的命令:
\caption{Table to test captions and labels}
如您所料,此命令设置表格的标题。如果您创建表格列表,则将在此处使用此标题。您可以将它放在表上方或下方。
\label{table:1}
如果您需要在文档中引用表格,请使用此命令设置标签。标签将为表格编号,并在与\ref
命令结合使用时,将允许您引用它。
\ref{table:1}
此代码将替换为与引用表对应的数字。
表列表
要创建表列表,请使用该\listoftables
命令。每个表的标题将用于生成此列表。对于babel
包支持的语言,标题“表格列表”将相应翻译。有关更多信息,请参阅有关国际语言支持的文章。
更改表格的外观
可以修改几个表格元素以满足文档的需要。下面您将学习如何修改表格中单元格的线条粗细、线条颜色和背景颜色。
线宽和单元格填充
有时可以通过增加列间距和行拉伸来提高表格的易读性。
\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
Afghanistan & AF &AFG \\
Aland Islands & AX & ALA \\
Albania &AL & ALB \\
Algeria &DZ & DZA \\
American Samoa & AS & ASM \\
Andorra & AD & AND \\
Angola & AO & AGO \\
\hline
\end{tabular}
\end{document}
下面提供了命令的描述:
\setlength{\arrayrulewidth}{0.5mm}
这将设置表格边框的厚度。在示例中为 0.5 毫米,但您可以使用其他单位 - 请参阅LaTeX中的长度以获取完整列表。
\setlength{\tabcolsep}{18pt}
使用此命令将文本与其包含单元格的左/右边框之间的间距设置为 18pt。同样,如果需要,您可以使用其他单位。
\renewcommand{\arraystretch}{1.5}
每行的高度相对于其默认高度设置为 1.5。
颜色交替行
您可以使用带有table
选项的xcolor
包将交替颜色应用于表格的行,如以下示例所示:
\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
Afghanistan & AF &AFG \\
Aland Islands & AX & ALA \\
Albania &AL & ALB \\
Algeria &DZ & DZA \\
American Samoa & AS & ASM \\
Andorra & AD & AND \\
Angola & AO & AGO \\
\hline
\end{tabular}
\end{document}
注意命令前的大括号
\rowcolors { 3 }{ green!80!yellow!50 }
{ green!70!yellow!40 }
和tabular
环境之后。该\rowcolors
命令采用三个参数,每个参数都在大括号内传递:
- 要开始的行,
- 奇数行的颜色和
- 偶数行的颜色。
有关可用颜色的列表以及如何创建自己的颜色,请参阅xcolor包文档(在进一步阅读部分)。在示例中,绿色和黄色以不同的比例混合。
为了使命令正常工作,请确保添加\usepackage[table]{xcolor}
到您的L A T E X文件 的序言。
为表格着色(单元格、行、列和行)
可以自定义表格中的所有元素以使用特定颜色。同样,此功能由xcolor
提供,因此您必须添加\usepackage[table]{xcolor}
到序言。下面你可以看到一个例子。
\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
Afghanistan & AF &AFG \\
\rowcolor{gray}
Aland Islands & AX & ALA \\
Albania &AL & ALB \\
Algeria &DZ & DZA \\
American Samoa & AS & ASM \\
Andorra & AD & \cellcolor[HTML]{AA0044} AND \\
Angola & AO & AGO \\
\hline
\end{tabular}
\end{document}
下面是关于如何更改表格中每个元素的颜色的说明:
- 线条的颜色。该命令
\arrayrulecolor
用于此。在示例中使用了 HTML 格式,但也可以使用其他格式,请参阅 xcolor 文档以获取完整列表。 - 单元格的背景颜色。使用命令
\cellcolor
。您可以直接在大括号内输入名称(红色、灰色、绿色等)或在括号内传递格式参数(示例中为 HTML),然后使用已建立的格式在大括号内设置所需的颜色。 - 一行的背景颜色。在这种情况下
\rowcolor
将实现这一点。前面两个命令中提到的关于颜色选择的相同观察结果对这个命令有效。 - 列的背景颜色。这个有点棘手,但最简单的方法是定义一个新的列类型。命令
\newcolumntype { s }{ > { \columncolor [HTML] { AAACED }} p { 3cm }}
定义一个名为的列类型s
,其对齐方式为p
,列宽为3cm
,颜色设置为HTML
格式AAACED
。这种新的列类型在tabular
环境中使用。
参考指南
tabular
环境 中参数的简要说明。
可以使用tabular
环境创建表。
\begin{tabular}[pos]{cols}
table content
\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列结束的部分水平线