赞
踩
基本用法如下:%
后面代表注释
\begin{tabular}{cccc}%一个c表示有一列,格式为居中显示(center),此处是4列(4个c)
1 & 2 & 3 & 4 \\%第一行各元素,中间用&连接
5 & 6 & 7 & 8 \\%第二行各元素,中间用&连接
\end{tabular}
运行效果如下:
竖线的添加方式是在表头添加|
\begin{tabular}{|c|c|c|c|}%每列之间的|代表整列都要绘制|
1 & 2 & 3 & 4 \\%第一行第一列和第二列 中间用&连接
5 & 6 & 7 & 8 \\%第二行第一列和第二列 中间用&连接
\end{tabular}
运行效果如下:
横线的语法是\hline
\begin{tabular}{cccc}
\hline % 一条横线
1 & 2 & 3 & 4 \\
\hline % 一条横线
5 & 6 & 7 & 8 \\
\hline % 一条横线
\end{tabular}
运行效果如下:
单元格格式分为居左、居中、居右显示,上面的c
就是居中(center)显示
\begin{tabular}{|l|c|r|}%l:left c:center r:right
\hline
1 & 2 & 3 \\
\hline
44444444 & 55555555 & 66666666 \\
\hline
\end{tabular}
运行效果如下:
有时我们希望绘制第一行和最后一行粗,中间行细的三线表。这个时候,需要引用宏包booktabs
,引用方法为\usepackage{booktabs}
。
\usepackage{booktabs}
\begin{tabular}{ccc}
\toprule %表格顶部粗横线
title1 & title2 & title3 \\
\midrule % 表格中间细横线
1 & 2 & 3 \\
4 & 5 & 6 \\
\bottomrule % 表格底部粗横线
\end{tabular}
运行效果如下:
{table}有若干可选参数 [!htbp]
h
代表here,将表格排在当前文字位置
t
表示将表格放在下一页的 top (页首)
b
表示将表格放在当前页的 bottom (底部)
!
表示忽略美观因素,尽可能按照参数指定的方式来处理表格浮动位置。
表格将会按照所给参数,依次尝试按照每个参数进行排版,当无法排版时,将会按照下一个参数
\begin{table}[!htbp]
\centering
\caption{This is a table} % 添加标题
\label{tab:tableTab} % 设置标签
\begin{tabular}{ccc}
\toprule
title1 & title2 & title3 \\
\midrule
1 & 2 & 3 \\
4 & 5 & 6 \\
\bottomrule
\end{tabular}
% 标题和标签写在这里也可以
\end{table}
运行效果如下:
横向合并表格的关键字是\multicolumn{3}{|c|}{title}
\begin{table}[!htbp]
\centering
\begin{tabular}{|c|c|c|}
\hline
\multicolumn{3}{|c|}{title}\\ % 用\multicolumn{3}表示横向合并三列
% |c|表示居中并且单元格两侧添加竖线 最后是文本
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
\hline
\end{tabular}
\end{table}
运行效果如下:
纵向合并需要宏包\usepackage{multirow}
,
\usepackage{multirow}
\begin{table}[!htbp]
\centering
\begin{tabular}{|c|c|c|}
\hline
\multirow{4}*{title test}& 1 & 2 \\ % 纵向合并4个单元格,&后面是第一行需要显示的数据
\cline{2-3} % 为第二列到第三列添加横线
& 3 & 4 \\ % 第二行需要显示的数据
\cline{2-3} % 为第二列到第三列添加横线
& 5 & 6 \\ % 第三行需要显示的数据
\cline{2-3} % 为第二列到第三列添加横线
& 7 & 8 \\ % 第四行需要显示的数据
\hline
\end{tabular}
\end{table}
运行效果如下:
\begin{table}[!htbp]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
\multicolumn{2}{|c|}{ \multirow{2}*{title 1} }& \multicolumn{4}{c|}{title 2} &\multirow{2}*{title 3}\\
\cline{3-6}
\multicolumn{2}{|c|}{}& 1 & 2 & 3 & 4 &\\
\hline
\multirow{4}*{title 4}& 5 & 6 & 7 & 8 & 9 & 10 \\
\cline{2-7}
& 11 &12 & 13 & 14 & 15 & 16 \\
\cline{2-7}
& 17 & 18 & 19 & 20 & 21 & 22 \\
\cline{2-7}
& 23 & 24 & 25 & 26 & 27 & 28 \\
\hline
\end{tabular}
\end{table}
运行效果如下:
斜线表头的实现需要引入宏包\usepackage{diagbox}
,使用diagbox
关键字添加
\usepackage{diagbox}
\begin{table}[!htbp]
\centering
\begin{tabular}{|c|c|c|}
\hline
\diagbox{a}{b}{c}& bc & cb\\ %添加斜线表头
\hline
ab & 2 & 3 \\
ba & 5 & 6 \\
\hline
\end{tabular}
\end{table}
运行效果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。