【Overleaf】Latex表格基本方法的使用

基本用法

基本用法如下:%后面代表注释

\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环境

{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}

运行效果如下:

在这里插入图片描述

  • 36
    点赞
  • 213
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡 亥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值