Latex中使用Table(表格)和 常用参数解释

Table和Tabular的区别

\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||} 
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [0.5ex] 
 \hline\hline
 1 & 6 & 87837 & 787 \\ 
 2 & 7 & 78 & 5415 \\
 3 & 545 & 778 & 7507 \\
 4 & 545 & 18744 & 7560 \\
 5 & 88 & 788 & 6344 \\ [1ex] 
 \hline
\end{tabular}
\caption{Table to test captions and labels.}
\label{table:1}
\end{table}

在这里插入图片描述

  • Tabular:仅指表格的那部分,不包括表格的文字说明等
  • Table:表格的所有元素

简单的Table实例

tabular是创建一个Table的基础环境。我们可以把Table看作是由许多单元(Cell)的数组,我们需要做的就是定义每一个Cell。下面是一个最基本的表格:

\documentclass{article}
\begin{document}
\begin{tabular}{l|c}
Name&Salary\\\hline
Mark&$\$250,000$\\
Carly&$\$80,000$\\
Carter&$\$25,000$\\
Sam&$\$50,000$
\end{tabular}
\end{document}

在这里插入图片描述

  • {l|c} :改参数表示列数和其对应的排版。l表示左对齐c表示居中对齐r表示右对齐。| 表示两列之间的分隔符,当不加|时,NameSalary 这两列之间就不会有竖线。
  • & :该符号表示一行中两个单元格之间的分隔符。比如,这个有两列,则每行有一个&符。如果有n列,则每行应该有 n-1 个 &
  • \\:换行符,表示该行结束,换下一行
  • hlinehorizontal line,表示在该行下面应该增加一条水平线。比如上图中,第一行下面有横线,后面都没有

单元格内数据量大的处理(指定列宽)

LaTex 并不会自动换行,当文本太大时,它只会一直往后写,甚至会超出页面

要解决这个问题,需要手动的进行一些设置,例如:

\documentclass{article}
\begin{document}
\begin{table}
\begin{tabular}{c|lll}
Name&Salary&Likes&Children\\\hline
Mark&$\$250,000$&windsurfing and jumping on trampolines&Amy, John, and Ray\\
Carly&$\$80,000$&heavy metal music, Paris, and dancing in the rain&Tyra\\
Carter&$\$25,000$&candy, fast cars that he cannot afford and Ramen&None\\
Sam&$\$50,000$&painting, motorcycles, and Reddit&Kyle and Sam Jr.
\end{tabular}
\end{table}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{table}
\begin{tabular}{c|lp{2in}p{1in}}
Name&Salary&Likes&Children\\\hline
Mark&$\$250,000$&windsurfing and jumping on trampolines&Amy, John, and Ray\\
Carly&$\$80,000$&heavy metal music, Paris, and dancing in the rain&Tyra\\
Carter&$\$25,000$&candy, fast cars that he cannot afford and Ramen&None\\
Sam&$\$50,000$&painting, motorcycles, and Reddit&Kyle and Sam Jr.
\end{tabular}
\end{table}
\end{document}

在这里插入图片描述
在输出的两个表中,上面的是没有格式化的,下面的是格式化之后的。

  • p{width} :可以使用 p{width}来指定列宽。例如,第一个表示没有指定列宽(c|lll,即第1列居中,第2-4列左对齐)。第二个表指定了列宽(c|lp{2in}p{1in},即第1列居中,第2列左对齐,第3列2英尺,第4列1英尺)。其中宽度可以使用的单位有cmptinem

多行和多列

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
\begin{tabular}{c|lp{1.4in}lp{1.1in}}
Name&Salary&Likes&Children&Desires\\\hline
\multirow{3}{*}{Mark}&\multirow{3}{*}{$\$250,000$}&windsurfing&Amy&\multirow{3}{*}{own
	a cheese shop}\\
~&~&jumping on&John&~\\
~&~&a trampoline&Ray&~\\\hline
\multirow{3}{*}{Carly}&$\multirow{3}{*}{\$80,000}$&heavy metal music&\multirow{3}{*}{Tyra}
&tattoo a president\\
~&~&Paris&~&\multirow{2}{*}{of the united states}\\
~&~&dancing in the rain&~&~\\\hline
\multirow{3}{*}{Carter}&$\multirow{3}{*}{\$25,000}$&candy&\multirow{3}{*}{None}&play the
	Ukelele\\
~&~&fast cars that he cannot afford&~&\multirow{2}{*}{ travel the world}\\
~&~&Ramen&~&~\\\hline
\multirow{3}{*}{Sam}&$\multirow{3}{*}{\$50,000}$&paintingit&Kyle&\multirow{3}{*}{be in the
	circus}\\
~&~&motorcycles&\multirow{2}{*}{Sam Jr.}&~\\
~&~&Reddit&~&~
\end{tabular}
\end{table}
\end{document}

在这里插入图片描述

  • \multirow{行数}{*}{文本}:当某个单元格需要占多行时,就使用该方式即可。例如,Mark需要占三行,则使用 \multirow{3}{*}{Mark}。对于第二行和第三行的第一个单元格,则填写~即可。

使用多行特性时,需要导包 \usepackage{multirow}

Table位置的调整

\begin{table}[h!] 中的[h!]是表格的参数,用来调整表格位置,具体有参数有

参数位置官方解释
h以浮动的方式放置在当前位置,大概实在原文本出现的位置,但不完全是Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot)
t放置在页面顶部Position at the top of the page.
b放置在页面底部Position at the bottom of the page.
p只为浮动设置一个特殊页面Put on a special page for floats only.
!覆盖LaTex用于确认“良好”浮动的内部参数Override internal parameters LaTeX uses for determining “good” float positions.
H将浮动精确的放置在Latex代码中的位置,需要导入float包,与h!等价Places the float at precisely the location in the LATEX code. Requires the float package. This is somewhat equivalent to h!

使用Excel自动生成表格

WPS将Excel表格转换为LaTex表格

若你使用的是微软Excel,可自行百度,原理差不多。







参考资料

LaTex Tutorial - Tables: https://www1.cmc.edu/pages/faculty/aaksoy/latex/latexfour.html

Overleaf 文档-Tables: https://www.overleaf.com/learn/latex/Tables

Positioning images and tables: https://www.overleaf.com/learn/latex/Positioning_images_and_tables

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

iioSnail

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

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

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

打赏作者

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

抵扣说明:

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

余额充值