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

猜你喜欢

转载自blog.csdn.net/zhaohongfei_358/article/details/121555191