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

运行效果如下:

在这里插入图片描述

### 创建和编辑表格Overleaf 中创建和编辑表格主要依赖于 `tabular` 环境。此环境允许定义列的对齐方式以及单元格的内容。 #### 使用 Tabular 环境构建基本表格结构 为了创建一个带有边框的三列表格,可以使用如下代码: ```latex \begin{table}[h] \centering \begin{tabular}{|c|c|c|} \hline 列1 & 列2 & 列3 \\ \hline 单元格1 & 单元格2 & 单元格3 \\ 单元格4 & 单元格5 & 单元格6 \\ \hline \end{tabular} \caption{表格的标题} \label{tab:my_label} \end{table} ``` 上述代码中的 `\begin{tabular}` 和 `\end{tabular}` 定义了一个表格范围,在大括号内指定每列的格式;竖线表示垂直分隔符,字母 c 表示居中对齐[^1]。 #### 添加表头并调整样式 如果希望给定特定样式的表头,可以在首行前加上额外命令来改变字体大小或加粗文字效果: ```latex \usepackage{makecell, booktabs} ... \renewcommand{\arraystretch}{1.5}% 增加行间距 \begin{table}[htbp!] \centering \begin{tabular}{>{\bfseries}l *{2}{p{8em}} } \toprule {\thead[l]{Header\\Row 1}} & Column Two Head & Third Col Header \\ \midrule Data Row One & Value & Another value \\ Second data row & More values & Even more \\ \bottomrule \end{tabular} \caption{改进后的表格布局} \label{tab:better_table_layout} \end{table} ``` 这里引入了两个新包——`booktabs` 提供更美观的水平线条,而 `makecell` 支持多行表头设置。 #### 合并与拆分单元格 对于复杂的数据展示需求,可能还需要合并相邻的多个单元格形成更大的区域。这可以通过附加参数到相应位置实现: ```latex \multicolumn{number of columns to span}{alignment specifiers}{content} ``` 例如要跨越两列显示某个标题,则写成这样: ```latex \begin{tabular}{|c|c|c|} \hline \multicolumn{2}{|c|}{Combined Cells Spanning Two Columns}\\ \cline{1-2} Cell A & Cell B & Cell C \\ \hline \end{tabular} ``` 同样地,也可以通过类似的语法来进行跨行操作:`\multirow{nrows}{width}{text}` 需要注意的是,该功能通常需要加载 multirow 包才能正常使用
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胡 亥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值