表格环境
简介
tabular
环境可用于排版带有可选水平线和垂直线的表格。语法如下:\begin{tabular}{<<table spec>>} <<table content>> \end{tabular}
table spec
参数告诉 LaTeX 每列要使用的对齐方式以及要插入的垂直线。不需要指定列数,因为它是通过查看提供的参数数量推断出来的。也可以在此处在列之间添加垂直线。以下符号可用于描述表格列
l |
left-justified column |
c |
centered column |
r |
right-justified column |
S |
align numbers at decimal point |
p{'width'} |
paragraph column with text vertically aligned at the top |
m{'width'} |
paragraph column with text vertically aligned in the middle |
b{'width'} |
paragraph column with text vertically aligned at the bottom |
| |
vertical line |
|| |
double vertical line |
: |
dash vertical line |
默认情况下,如果列中的文本对于页面来说太宽,LaTeX 不会自动换行。使用
p{'width'}
,您可以定义一种特殊类型的列,它将像普通段落一样换行。在第一行中,您定义了所需的列数、它们的对齐方式以及分隔它们的垂直线。进入环境后,您必须引入所需的文本、分隔单元格并引入新行。您必须使用的命令如下:
&
- 列分隔符;\\
- 开始新行;\hline
- 水平线;\hhline
- 双水平线;\hdashline
- 水平虚线;\cline{i-j}
- 从列 i
开始到列 j
结束的部分水平线。基本示例
此示例展示了如何在 LaTeX 中创建一个简单的表格。这是一个三乘三的表格,没有任何线条。
\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
Expanding upon that by including some vertical lines:
\begin{tabular}{ | l | c | r | }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
To add horizontal lines to the very top and bottom edges of the table:
\begin{tabular}{ | l | c | r | }
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline
\end{tabular}
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
And finally, to add lines between all rows, as well as centering
\begin{center}
\begin{tabular}{ | l | c | r | }
\hline
1 & 2 & 3 \\ \hline
4 & 5 & 6 \\ \hline
7 & 8 & 9 \\
\hline
\end{tabular}
\end{center}
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
Aligning the same table to the left…
\begin{left}
\begin{tabular}{ | l | c | r | }
\hline
1 & 2 & 3 \\ \hline
4 & 5 & 6 \\ \hline
7 & 8 & 9 \\
\hline
\end{tabular}
\end{left}
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
An example of how you can align a column at a decimal point using
S
:\begin{tabular}{l|S|r|l}
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3} & \textbf{Value 4}\\
$\alpha$ & $\beta$ & $\gamma$ & $\delta$ \\
\hline
1 & 1110.1 & a & e\\
2 & 10.1 & b & f\\
3 & 23.113231 & c & g\\
\end{tabular}
Value 1 | Value 2 | Value 3 | Value 4 |
1 | 1110.1.00000 | a | e |
2 | 0010.1.00000 | b | f |
3 | 0023.113231. | c | g |
表格中的文本换行
p
属性用于指定所需的列宽度。使用
m
属性代替 p
使行与框的中间对齐,或使用 b
属性使行与框的底部对齐。这是一个简单的例子。以下代码使用相同的代码创建了两个表格;唯一的区别是第二个表格的最后一列的宽度定义为 5 厘米,而第一个表格中我们没有指定任何宽度。编译此代码:
不指定最后一列的宽度:
\begin{center}
\begin{tabular}{| l | l | l | l |}
\hline
Day & Min Temp & Max Temp & Summary \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
across most of Scotland and Northern Ireland,
but rain reaching the far northwest. \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \\
\hline
\end{tabular}
\end{center}
不指定最后一列的宽度:
Day | Min Temp | Max Temp | Summary |
Monday | 11C | 22C | A clear day with lots of sunshine. However, the strong breeze will bring down the temperatures. |
Tuesday | 9C | 19C | Cloudy with rain, across many northern regions. Clear spells across most of Scotland and Northern Ireland, but rain reaching the far northwest. |
Wednesday | 10C | 21C | Rain will still linger for the morning. Conditions will improve by early afternoon and continue throughout the evening. |
With width specified:
\begin{center}
\begin{tabular}{ | l | l | l | p{5cm} |}
\hline
Day & Min Temp & Max Temp & Summary \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
across most of Scotland and Northern Ireland,
but rain reaching the far northwest. \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \\
\hline
\end{tabular}
\end{center}
With width specified:
Day | Min Temp | Max Temp | Summary |
Monday | 11C | 22C | A clear day with lots of sunshine. However, the strong breeze will bring down the temperatures. |
Tuesday | 9C | 19C | Cloudy with rain, across many northern regions. Clear spells across most of Scotland and Northern Ireland, but rain reaching the far northwest. |
Wednesday | 10C | 21C | Rain will still linger for the morning. Conditions will improve by early afternoon and continue throughout the evening. |
跨多列的行
此命令如下所示:
\multicolumn{num_cols}{alignment}{contents}
。num_cols
是要合并的后续列数;alignment
是 l
、c
、r
。contents
只是您希望包含在该单元格内的实际数据。一个简单的例子:
\begin{tabular}{ |l|l| }
\hline
\multicolumn{2}{|c|}{Team sheet} \\
\hline
GK & Paul Robinson \\
LB & Lucas Radebe \\
DC & Michael Duberry \\
DC & Dominic Matteo \\
RB & Dider Domi \\
MC & David Batty \\
MC & Eirik Bakke \\
MC & Jody Morris \\
FW & Jamie McMaster \\
ST & Alan Smith \\
ST & Mark Viduka \\
\hline
\end{tabular}
Team sheet | |
GK | Paul Robinson |
LB | Lucas Radebe |
DC | Michael Duberry |
DC | Dominic Matteo |
RB | Dider Domi |
MC | David Batty |
MC | Eirik Bakke |
MC | Jody Morris |
FW | Jamie McMaster |
ST | Alan Smith |
ST | Mark Viduka |
跨越多行的列
然后提供跨越行所需的命令:
\multirow{''num_rows''}{''width''}{''contents''}
。参数推导起来非常简单(宽度的
*
表示内容的自然宽度)。\begin{tabular}{ |l|l|l| }
\hline
\multicolumn{3}{ |c| }{Team sheet} \\
\hline
Goalkeeper & GK & Paul Robinson \\ \hline
\multirow{4}{*}{Defenders} & LB & Lucas Radebe \\
& DC & Michael Duburry \\
& DC & Dominic Matteo \\
& RB & Didier Domi \\ \hline
\multirow{3}{*}{Midfielders} & MC & David Batty \\
& MC & Eirik Bakke \\
& MC & Jody Morris \\ \hline
Forward & FW & Jamie McMaster \\ \hline
\multirow{2}{*}{Strikers} & ST & Alan Smith \\
& ST & Mark Viduka \\
\hline
\end{tabular}
Team sheet | ||
Goalkeeper | GK | Paul Robinson |
Defenders | LB | Lucas Radebe |
DC | Michael Duburry | |
DC | Dominic Matteo | |
RB | Didier Domi | |
Midfielders | MC | David Batty |
MC | Eirik Bakke | |
MC | Jody Morris | |
Forward | FW | Jamie McMaster |
Strikers | ST | Alan Smith |
ST | Mark Viduka |
同时在两个方向上跨越
下面是一个非平凡的例子,说明如何同时在两个方向上使用跨越并正确绘制单元格的边框:
\begin{tabular}{cc|c|c|c|c|l}
\cline{3-6}
& & \multicolumn{4}{ c| }{Primes} \\ \cline{3-6}
& & 2 & 3 & 5 & 7 \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{504} & 3 & 2 & 0 & 1 & \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{540} & 2 & 3 & 1 & 0 & \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{gcd} & 2 & 2 & 0 & 0 & min \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{lcm} & 3 & 3 & 1 & 1 & max \\ \cline{1-6}
\end{tabular}
Primes | ||||||
2 | 3 | 5 | 7 | |||
Powers | 504 | 3 | 2 | 0 | 1 | |
540 | 2 | 3 | 1 | 0 | ||
Powers | gcd | 2 | 2 | 0 | 0 | min |
lcm | 3 | 3 | 1 | 1 | max |
下面是另一个利用相同思想来制作熟悉且流行的“2x2”或双重二分法的例子:
\begin{tabular}{ r|c|c| }
\multicolumn{1}{r}{}
& \multicolumn{1}{c}{noninteractive}
& \multicolumn{1}{c}{interactive} \\
\cline{2-3}
massively multiple & Library & University \\
\cline{2-3}
one-to-one & Book & Tutor \\
\cline{2-3}
\end{tabular}
noninteractive | interactive | |
massively multiple | Library | University |
one-to-one | Book | Tutor |
定位表格
如果表格位于浮动表格环境中,则定位表格很容易。
\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}
Col1 | Col2 | Col2 | Col3 |
1 | 6 | 87837 | 787 |
2 | 7 | 78 | 5415 |
3 | 545 | 778 | 7507 |
4 | 545 | 18744 | 7560 |
5 | 88 | 788 | 6344 |
\begin{table}[h!]
\begin{left}
\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{left}
\end{table}
Col1 | Col2 | Col2 | Col3 |
1 | 6 | 87837 | 787 |
2 | 7 | 78 | 5415 |
3 | 545 | 778 | 7507 |
4 | 545 | 18744 | 7560 |
5 | 88 | 788 | 6344 |
标题、标签和引用
通过将
\tabular
环境包装在 \table
环境中,可以为表格添加标题、标签和引用。必须在
\begin{table}
和 \begin{tabular}
之间或 \end{tabular}
和 \end{table}
之间添加 \caption
和 \label
命令。The table \ref{table:1} is an example of referenced \LaTeX elements.
\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}
The table 1 is an example of referenced \LaTeX elements.
Col1 | Col2 | Col2 | Col3 |
1 | 6 | 87837 | 787 |
2 | 7 | 78 | 5415 |
3 | 545 | 778 | 7507 |
4 | 545 | 18744 | 7560 |
5 | 88 | 788 | 6344 |
示例中有三个重要命令:
\caption{用于测试标题和标签的表格}
如您所料,此命令设置表格的标题,如果您创建表格列表,则此标题将在那里使用。您可以将其放在表格上方或下方。
\label{table:1}
如果您需要在文档中引用表格,请使用此命令设置标签。标签将对表格进行编号,与
\ref
命令结合使用将允许您引用它。\ref{table:1}
此代码将被引用表格对应的编号替换。
更多示例
\begin{table}[h]
\centering
\begin{tabular}{|c||c|c|} \hline
& A & B \\ \hline\hline
Foo &
\begin{tabular}{c} 1 \\ 2 \\ 3 \\ 4 \\
\end{tabular} &
\begin{tabular}{c} 2 \\ 5 \\ 9 \\ 8 \\
\end{tabular} \\ \hline
Bar &
\begin{tabular}{c} 1 \\ 2 \\ 3 \\ 4 \\
\end{tabular} &
\begin{tabular}{c} 31 \\ 23 \\ 16 \\ 42 \\
\end{tabular} \\ \hline
\end{tabular}
\caption{this is the table!}
\label{table:4}
\end{table}
A | B | |||||||||
Foo |
|
|
||||||||
Bar |
|
|
\begin{table}[h]
\centering
\begin{tabular}{ :c|c|c: }
\multicolumn{3}{|c|}{My cells2}\\
\hline
\multirow{3}{4em}{Multiple rows} & cell2 & cell3 \\\hline
& cell5 & cell6 \\ \hline
& cell8 & cell9 \\
\hhline
\end{tabular}
\caption{this is the table!}
\label{table:5}
\end{table}
My cells2 | ||
Multiple rows | cell2 | cell3 |
cell5 | cell6 | |
cell8 | cell9 |
\begin{table}[h]
\centering
\begin{tabular}{ ||c: c|| c||: }
\hhline
{\(cell^1\)} & dd$$\frac{\nabla^{2} A}{A}=-k^{2}$$ dd & {cell3} \\ \hdashline
{\(cell^4\)} & {cell5} & \(cell^6\) \\ \hline\hline
cell7 & {cell $f^f$ 8} & cell9\\\hhline
\end{tabular}
\caption{this is the table!}
\label{table:6}
\end{table}
dd |
cell3 | |
cell5 | ||
cell7 | cell |
cell9 |
\begin{table}[h]
\centering
\begin{tabular}{ |c|c|c| }
\hhline
{ formula $\frac{\nabla^{2} A}{A}=-k^{2}$} & cell2 & cell3 \\
\hline
\end{tabular}
\caption{this is the table!}
\label{table:7}
\end{table}
formula |
cell2 | cell3 |
\begin{table}[h]
\centering
\begin{tabular}{|l:l:l|}
\hline
1& \multirow{2}{5em}{\multicolumn{2}{:c:}{Day}}\\ \hline
1&2&3&4\\ \hline
1&2&3&4\\ \hline
\end{tabular}
\end{table}
1 | Day | ||
1 | 4 | ||
1 | 2 | 3 | 4 |
\begin{table}[h]
\centering
\begin{tabular} {|||c|c|c|c|c|c|c|c|:::}
\hline \hhline {alms's} & \multicolumn{4}{|c|} {economizes} & {recondition} & {bailing} & {asymptotically} \\ \hline
\multicolumn{1}{|||c|} {fiddle} & \multicolumn{5}{|c|} {kitchenettes} & {misstates} \\ \hline
\end{tabular}
\caption{this is the table!}
\label{table:7}
\end{table}
Table \ref{table:4} is an example.
alms's | economizes | recondition | bailing | asymptotically | |||
fiddle | kitchenettes | misstates |
Table 2 is an example.