推荐一个在线生成各种表格命令的工具:https://tablesgenerator.com/。
1.表格参数
table 后面加*表示双栏
表格,如\begin{table*}...\end{table*}
。
表格中常用选项[htbp]
是浮动格式:
h
当前位置。将图形放置在正文文本中给出该图形环境的地方。如果本页所剩的页面不够,这一参数将不起作用。
t
顶部。将图形放置在页面的顶部。
b
底部。将图形放置在页面的底部。
p
浮动页。将图形放置在一只允许有浮动对象的页面上。
一般使用[htb]
这样的组合,只用[h]
是没有用的。这样组合的意思就是latex会尽量满足排在前面的浮动格式,就是[h-t-b]
这个顺序,让排版的效果尽量好。
[!h]
只是试图放在当前位置。如果页面剩下的部分放不下,还是会跑到下一页的。一般页言,用 [!h]
选项经常会出现不能正确放置的问题,所以常用 [ht]
、[htbp]
等。
如果你确实需要把图片放在当前位置,不容改变,可以用float
宏包的[H]
选项。不过如果这样做,出现放不下的问题时需要手工调整。
参数 | 设置 |
---|---|
\tabularnewline | 用于强制一表格行的结束,而\除了可以结束整个一行表格内容外,还可以在单个列的内容中实现换行 |
\vline | 该命令画一条竖直线,其高度等于其所在行的行高。用这种命令,可以得到那些不是贯穿整个表格的竖直线 |
2.表格格式
LaTex中标准的表格格式如下:
\begin{table}[htbp]
\centering % 显示位置为中间
\caption{standard table} % 表格标题
\label{table1} % 用于索引表格的标签
%字母的个数对应列数,|代表分割线
% l代表左对齐,c代表居中,r代表右对齐
\begin{tabular}{|c|c|c|c|}
\hline % 表格的横线
& & & \\[-6pt] %可以避免文字偏上来调整文字与上边界的距离
1&2&3&4 \\ % 表格中的内容,用&分开,\\表示下一行
\hline
& & & \\[-6pt] %可以避免文字偏上
0.1&0.2&0.3&0.4 \\
\hline
\end{tabular}
\end{table}
3.表格宽度拓宽
\begin{tabular} {|l|r|}
\hline
1 & 2 \\ \hline
Yes & No \\ \hline
\end{tabular}
\renewcommand\arraystretch{2} % 宽度拓展为原来的2倍
\begin{tabular} {|l|r|}
\hline
1 & 2 \\ \hline
Yes & No \\ \hline
\end{tabular}
\renewcommand\arraystretch{1} % 宽度恢复
\begin{tabular} {|l|r|}
\hline
1 & 2 \\ \hline
Yes & No \\ \hline
\end{tabular}
4.三线表的制作
\usepackage{booktabs} % 引入三线表宏包
\begin{table}[htbp]
\centering
\caption{three-line table}
\begin{tabular}{cccc}
\toprule % 顶部线
1&2&3&4 \\
\midrule % 中部线
0.1&0.2&0.3&0.4 \\
\bottomrule % 底部线
\end{tabular}
\end{table}
三线表伸缩:
\begin{table}[htbp]
\centering
\caption{three-line table}
\begin{tabular}{cccc}
\toprule % 顶部线
1&2&3&4 \\
\cmidrule(r){2-4}
0.1&0.2&0.3&0.4 \\
\bottomrule % 底部线
\end{tabular}
\end{table}
5.表格行列合并
\usepackage{multirow} % 多行合并的宏包
\usepackage{multicolumn} %多列合并的宏包
\begin{tabular}{|c|r|r|}
\hline
% \multirow{2}{*}{Name},2为所占的行数,此语句可以使得内容垂直居中
% \multicolumn{2}{c|}{Flag},2为所占的列数,格式由第二个{}控制
% \cline{2-3}指本行的2,3列画横线
\multirow{2}{*}{Name} & \multicolumn{2}{c|}{Flag} \\ \cline{2-3}
& Yes & NO \\ \hline
Index & 87 & 100 \\ \hline
\end{tabular}
6.表格大小设置
\resizebox{1\linewidth}{!}{
\begin{tabular}{|c|r|r|}
\hline
% \multirow{2}{*}{Name},2为所占的行数,此语句可以使得内容垂直居中
% \multicolumn{2}{c|}{Flag},2为所占的列数,格式由第二个{}控制
% \cline{2-3}指本行的2,3列画横线
\multirow{2}{*}{Name} & \multicolumn{2}{c|}{Flag} \\ \cline{2-3}
& Yes & NO \\ \hline
Index & 87 & 100 \\ \hline
\end{tabular}
}
7.表格并排
\begin{table}[t]
\begin{minipage}{0.49\linewidth}
\centering
\caption{\textbf{ImageNet classification} results of the WeightNet.}
\label{table:snetv2}
\resizebox{1\textwidth}{!}{
\begin{tabular}{lccc}
\hline
Model & \# Params & FLOPs & Top-1 err. \\ \hline
ShuffleNetV2 (0.5$\times$) & 1.4M & 41M & 39.7 \\
+ WeightNet (1$\times$) & 1.5M & 41M & \textbf{36.7} \\ \hline
\end{tabular}
}
\end{minipage}
\hfill
\begin{minipage}{0.49\linewidth}
\centering
\caption{\textbf{ImageNet classification} results of the WeightNet on ShuffleNetV2.
}
\label{table:snetv2flops}
\resizebox{1\textwidth}{!}{
\begin{tabular}{lccc}
\hline
Model & \# Params & FLOPs & Top-1 err. \\ \hline
ShuffleNetV2 (0.5$\times$) & 1.4M & 41M & 39.7 \\
+ WeightNet (8$\times$) & 2.7M & 42M & \textbf{34.0} \\ \hline
\end{tabular}
}
\end{minipage}
\end{table}
8.表格列设置
(1) 固定列宽设置(水平对齐)
设置固定列宽,超过固定宽度之后会进行自动的回行。
有三种属性可以设置分别为:
p{‘width’}
paragraph column with text vertically aligned at the top
p水平基线顶部对齐,左右默认为左对齐
m{‘width’}
paragraph column with text vertically aligned in the middle(requires array package)
m水平基线中间对齐,左右默认为左对齐
b{‘width’}
paragraph column with text vertically aligned at the bottom (requires array package)
b水平基线底部对齐,左右默认为左对齐
\usepackage{array}
\begin{table*}[t]
\begin{tabular}{|p{1cm}|p{1cm}|p{1cm}|}%导言区2
\hline
apple & apple & apple apple apple\\ \hline
4 & 5 & 6 \\ \hline
7 & 8 & 9 \\ \hline
\end{tabular}
\\
space for a line
\\
\begin{tabular}{|m{1cm}|m{1cm}|m{1cm}|}%导言区2
\hline
apple&apple&apple apple apple\\ \hline
4&5&6\\\hline
7&8&9\\\hline
\end{tabular}
\\
space for a line
\\
\begin{tabular}{|b{1cm}|b{1cm}|b{1cm}|}%导言区2
\hline
apple&apple&apple apple apple\\ \hline
4&5&6\\\hline
7&8&9\\\hline
\end{tabular}
\end{table*}
(2) 固定列宽设置(垂直左中右对齐)
设置左右中对齐(以p为例)
<{\raggedleft}
左对齐
<{\centering}
居中
<{\raggedright}
右对齐
\begin{table*}[t]
\begin{tabular}{|p{1cm}<{\raggedleft}|p{1cm}<{\centering}|p{1cm}<{\raggedright}|}
\hline
apple &apple&apple apple apple\\ \hline
4 & 5 &6\\\hline
7 & 8 &9\\\hline
\end{tabular}
\end{table*}
(3) 列设置(设置输出为手写体数字)
设置表格中的字符都是数学手写字体。语法:\>{\cmd} and <{\cmd}
。
\usepackage{array}
\newcolumntype{F}{>{$}c<{$}}
\begin{tabular}{FFF}
\alpha & \beta & \gamma \\
\delta & \epsilon & \upsilon \\
\sigma & \tau & \phi \\
\end{tabular}
9.表格行设置
(1) 添加行间距
\begin{tabular}{ll}
\hline
Mineral & Color \\[0.5cm]
Ruby & red \\
Sapphire & blue \\
\hline
\end{tabular}
(2) 调整行间距
% 方法一
\setlength{\tabcolsep}{5pt} %colums, default value is 6pt
%方法二
\renewcommand{\arraystretch}{1.5} %rows, default value is 1.0这一句代码,作用是将每一行的高度变为原来的两倍。
%方法三
\hline\noalign{\smallskip}
cline{i-j}\noalign{\smallskip}
% 另外一种方式是在`\hline`和`cline{i-j}`前面或者后添加`\noalign{\smallskip}`
experiment1\\%没有设置行距的情况
\begin{tabular}{ | l | l | r | }
\hline
\multicolumn{2}{c}{Item} \\ \cline{1-2}
Animal & Description & Price (\$) \\ \hline
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Armadillo & frozen & 8.99 \\ \hline
\end{tabular}
\\
\\
experiment2\\%更改固定行距之后的表格
\setlength{\tabcolsep}{4pt}
\begin{tabular}{ | l | l | r | }
\hline
\multicolumn{2}{c}{Item} \\ \cline{1-2}
Animal & Description & Price (\$) \\ \hline
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Armadillo & frozen & 8.99 \\ \hline
\end{tabular}
\\
\\
experiment3\\%使用renewcommand的方法进行行高设置
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{ | l | l | r | }
\hline
\multicolumn{2}{c}{Item} \\ \cline{1-2}
Animal & Description & Price (\$) \\ \hline
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Armadillo & frozen & 8.99 \\ \hline
\end{tabular}
\\
\\
experiment4\\
\setlength{\tabcolsep}{6pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{ | l | l | r | }
\hline\noalign{\smallskip}
\multicolumn{2}{c}{Item} \\ \cline{1-2}\noalign{\smallskip}
Animal & Description & Price (\$) \\ \noalign{\smallskip}\hline\noalign{\smallskip}
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Armadillo & frozen & 8.99 \\ \noalign{\smallskip}\hline
\end{tabular}
参考资源
[1] LaTeX表格Table使用解析
[2] LATEX关于表格的一些处理