目录
表格
与 LaTeX 表格相关的宏包如下:
\usepackage{booktabs} %% 三线表
\usepackage{diagbox} %% 斜线表头
\usepackage{multirow} %% 合并单元格
1、基本结构
\begin{table}[!h]
\renewcommand{\arraystretch}{1.5}
\caption{An Example of a Table}
\label{table1}
\centering
\begin{tabular}{|c|c|}
\hline
A & B \\
\hline
1 & 2\\
\hline
\end{tabular}
\end{table}
1.1框架
\begin{table}[htbp]
\end{table}
[htbp]同上
对于两栏的论文,如果表格想横跨两栏,可以将{table}
换成{table*}
,与上面图片浮动插入{figure*}
类似。
1.2行高
\renewcommand{\arraystretch}{1.5}
设置的表格的行高,上面例子设置为1.5。
1.3标题
\caption{An Example of a Table}
{ } 中是标题的具体内容,可自动编号。
1.4标签
\label{table1}
用于后面的引用,\ref
和{}中的标签名结合可实现表的引用。
1.5居中
\centering
1.6表格
\begin{tabular}{|c|c|}
\hline
A & B \\
\hline
1 & 2\\
\hline
\end{tabular}
1.6.1表格体
\begin{tabular}{|c|c|}
\end{tabular}
l代表左对齐,c代表居中,r代表右对齐,这里我们用的c(居中)。
1.6.2表格的横线
\hline
表示该边框是有实线的,如果没有\hline
,就表示这行没有边框横线。
如果我们希望部分有实线部分没有应该咋样做了?
如下面例子,我们想第二行的第 1 列、第 3 列 和第 5 列有横向,可以这样写:\cline{1-1} \cline{3-3} \cline{5-5}
;
其中\cline{a-b}中a-b表示a列至b列。
这里我们使用了{table*}
,表格可横跨两栏。
\begin{table*}[!h]
\renewcommand{\arraystretch}{1.2}
\caption{An Example of a Table}
\label{table1}
\centering
\begin{tabular}{c c c c c}
\hline
A & B & C & D & E \\
\cline{1-1} \cline{3-3} \cline{5-5}
1 & 2 & 3 & 4 & 5 \\
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}
\end{table*}
1.6.3宽度调整
-
\setlength{\tabcolsep}{输入尺寸}{}
:按页面宽度调整表格
\setlength{\tabcolsep}{输入尺寸}{
\begin{table}[htbp]
......
\end{table}
}
-
\resizebox{\textwidth}{输入尺寸}{}
:按文本内容调整表格
\resizebox{\textwidth}{输入尺寸}{
\begin{table}[htbp]
......
\end{table}
}
2、引用表格
同图片引用
As show in Table \ref{table1}
正如表 \ref{table1}
3、常见表格类型
3.1三线表
\usepackage{booktabs}
:引入三线表宏包
\toprule
:顶部线 \midrule
:中部线 \bottomrule
:底部线。最上面和最下面的横线更粗。
\begin{table}[htbp]
\renewcommand{\arraystretch}{1.5}
\centering
\caption{three-line table}
\label{table_example}
\begin{tabular}{c c c c}
\toprule % 顶部线
1&2&3&4 \\
\midrule % 中部线
one&two&three&four \\
\bottomrule % 底部线
\end{tabular}
\end{table}
3.2 斜线表头
\usepackage{diagbox}
:斜线表头所需的宏包
\diagbox{A1}{A2}
:将单元格分成两块,分别是A1和A2;\diagbox{A1}{A2}{A3}
: 将单元格分成三块,分别是A1、A2和A3。其他部分与普通表无异。
\begin{table}[!h]
\renewcommand{\arraystretch}{1.5}
\caption{An Example of a Table}
\label{table_example}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\diagbox{A1}{A2}{A3} & B & C & D & E \\
\hline
a1 & 2 & 3 & 4 & 5 \\
\hline
a2 & 2 & 3 & 4 & 5 \\
\hline
a3 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}
\end{table}
3.3合并单元格
\usepackage{multirow}
:合并单元格所需的宏包
1、\multicolumn{}{}{}
:多列合并
2、\multirow{}{}{}
:多行合并
如果是嵌套使用,那就是多行多列合并。
-
第一个大括号是要合并的数量,用到第一个命令中就是要合并多少列,用到第二个命令中就是要合并多少行。
-
第二个大括号是行中每列数据的格式及对应的边框线。合并行的格式一般都默认是遵循前面的,写*。
-
第三个大括号是当前单元格的内容。
\begin{table}[!h]
\renewcommand{\arraystretch}{1.5}
\caption{An Example of a Table}
\label{table_example}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\multicolumn{2}{|c|}{\multirow{2}{*}{A}} & \multicolumn{3}{c|}{B} \\
\cline{3-5}
\multicolumn{2}{|c|}{} & B1 & B2 & B3 \\
\hline
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}
\end{table}
4、在线表格生成工具
Create LaTeX tables online – TablesGenerator.com
Latex相关内容链接: