LaTex基础-Table

1
2
3
4
5
\begin{tabular}[pos]{cols}

table content

\end{tabular}

where options can be:

  • pos : Vertical position. It can assume the following values:
tthe line at the top is aligned with the text baseline (可以理解为一横文字的底部基线, 所有文字在这个基线上排布)
bthe line at the bottom is aligned with the text baseline
c or nonethe table is centred to the text baseline
  • cols : Defines the alignment and the borders of each column. It can have the following values:
lleft-justified column
ccentred column
rright-justified column
p{'width'}paragraph column with text vertically aligned at the top
m{'width'}paragraph column with text vertically aligned in the middle (requires array
package)
b{'width'}paragraph column with text vertically aligned at the bottom (requires array
package)
|vertical line
||double vertical line
*{num}{form}the format form is repeated num times; for example *{3}{|l}|
is equal to |l|l|l|

To separate between cells and introducing new lines use the following commands:

&column separator
\\\\start new row (additional space may be specified after \\
using square brackets, such as \\[6pt]
)
\\hlinehorizontal line between rows
\\newlinestart a new line within a cell (in a paragraph column)
\\cline{i-j}partial horizontal line beginning in column i and ending in column j

1. 用table制作封面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
% 用于制作更复杂的封面
\title{\makebox{
\begin{tabular}{c}
\includegraphics[width=\textwidth]{img/nankai.jpg} \\
\textbf{课程报告} \\
xxxxxx
\end{tabular}
}
}
\author{\makebox{
\begin{tabular}{cc}
姓名: & xxxx \\
学号: & xxxx
\end{tabular}
}}
\date{\today}

2. 将表格进行整体缩放(并不推荐, 除非迫不得已)

1
2
3
4
5
6
7
8
9
10
\resizebox{\textwidth}{15mm}{
\begin{tabular}{|c|c|}
\hline
\ & 平均运行时间(单位: s) \\
\hline
训练阶段 & 2333.5593 \\
测试阶段 & \ \\
\hline
\end{tabular}
} % 只能包含tabular 不能包含文本 如 \caption

3. 整体调整每一列宽度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\setlength{\tabcolsep}{7mm}{
\begin{tabular}{cccccc}
\toprule
Models & $\hat c$ & $\hat\alpha$ & $\hat\beta_0$ & $\hat\beta_1$ & $\hat\beta_2$ \\ \hline
model & 30.6302 & 0.4127 & 9.4257 & - & - \\
model & 12.4089 & 0.5169 & 18.6986 & -6.6157 & - \\
model & 14.8586 & 0.4991 & 19.5421 & -7.0717 & 0.2183 \\
model & 3.06302 & 0.41266 & 0.11725 & - & - \\
model & 1.24089 & 0.51691 & 0.83605 & -0.66157 & - \\
model & 1.48586 & 0.49906 & 0.95609 & -0.70717 & 0.02183 \\
\bottomrule
\caption{哈哈哈}
\end{tabular}
} % 这个可以包含很多元素

4. 设置列高

\rule{线的宽度}{线撑起的高度}

线设置为0pt宽度, 就可以起到撑起单元格的作用了

1
2
3
4
5
6
7
8
\begin{tabular}{|c|c|}
\hline
\ & 平均运行时间(单位: s) \\
\hline
\rule{0pt}{8pt}训练阶段 & 2333.5593 \\
测试阶段 & \ \\
\hline
\end{tabular}

5. 三线表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass{article} 
\usepackage{float}%提供float浮动环境
\usepackage{booktabs}%提供命令\toprule、\midrule、\bottomrule

\begin{document}

%经典三线表
\begin{table}[H]
\caption{\textbf{Example 1}}%标题
\centering%把表居中
\begin{tabular}{c{80pt}c{80pt}c{80pt}c{80pt}}%四个c代表该表一共四列,内容全部居中
\toprule%第一道横线
Item 1&Item 2&Item 3&Item 4 \\
\midrule%第二道横线
Data1&Data2&Data3&Data4 \\
Data5&Data6&Data7&Data8 \\
\bottomrule%第三道横线
\end{tabular}
\end{table}

\end{document}

6. 复杂表格

多列表格中用\cline 控制哪几列有横线: \cline{3-5} 表示第3列和第5列有横线.

1
2
3
4
5
% 如果要间隔一列加横线只要两个指令就行
\begin{tabular}{llrr}
a &b &c &d \\ \cline{1-1} \cline{3-4}
e &f &g &h
\end{tabular}

如果用了booktabs, 想要\cline类似的多列横线, 则可以使用\cmidrule, 这个比\midrule更细

解释

解释2


\usepackage{multirow}

\multirow{nrows}[bigstruct]{width}[fixup]{text}

  • nrows: 占用的行数
  • bigstructs: 可选, 在使用了bigstruct宏包时用
  • width: 该栏宽度, 如果为*则由LaTex自行决定
  • fixup: 可选项, 调整文本的垂直位置
  • text: 所要排版的文本, 可以用 \ 强制换行
1
2
3
4
5
6
7
8
9
10
\begin{tabular}{|c|c|c|c|c|}
\hline
%跨行(将两行合并为一行) 第一个参数指明跨几行 记得下一行对应位置空出来 不然会重叠, 第二个参数是宽度
\multirow{2}{*}{1} & 11 & 111 & 1111 & 11111 \\
\cline{2-5}
& 22 & 222 & 2222 & 22222 \\
\hline
3 & 33 & 333 & 3333 & 33333 \\
\hline
\end{tabular}

\multicolumn{ncols}{指明对齐方式以及方格线}{单元格内容}

1
2
3
4
5
6
7
8
9
10
11
\begin{tabular}{|c|c|c|c|c|}
\hline
% after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
%跨列(将两列合并为一列)第一个参数指明跨几列 第二个参数指明内容居中并在左右两边画上直线 最后一个参数是表格内容
\multicolumn{2}{|c|}{1} & 111 & 1111 & 11111 \\
\hline
2 & 22 & 222 & 2222 & 22222 \\
\hline
3 & 33 & 333 & 3333 & 33333 \\
\hline
\end{tabular}

7. 表格的列方向上分散对齐

\makebox[width][position]{text}

\makebox[4em][s]{content}

  • l - left
  • r - right
  • s - interword space adjusted so text fills box exactly
1
2
3
4
5
6
\begin{tabular}{rl}
\makebox[4em][s]{论文题目}: & \@title\\
\makebox[4em][s]{专\hspace{\fill}业}:& \@major\\
\makebox[4em][s]{博\hspace{\fill}士\hspace{\fill}生}: & \@author\\
\makebox[4em][s]{指导教师}: & \@advisorname \,\, \@advisortitle
\end{tabular}
  1. 表格内换行

\makecell

解释

1
2
3
4
5
6
7
\usepackage{makecell}

% ...

\begin{tabular}{cc}
\makecell[l]{a \\ b \\ c } & 1 \\ % 用于单元格内换行
\end{tabular}

除了使用\makecell外, 还可以用嵌套tabular的方式

1
2
3
4
5
6
7
8
9
10
11
\begin{tabular}{cccc}
\toprule
\begin{tabular}{c}
物理机\\资源类型
\end{tabular}
& CPU资源(单位:核) & 内存资源(单位:G) & 机器数量 \\
\hline
类型一 & 32 & 64 & 6000 \\
类型二 & 92 & 288 & 3000 \\
\bottomrule
\end{tabular}
  1. 设置合理列宽

需要导入makecell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
% p{xpt} 可以指定每一列的宽度, 但是这样就没有t, c, b 对齐的作用了, 但是可以自动换行!

\begin{table }[ htbp]
\centering
\begin{tabular}{p{80 pt}p{80 pt}p{80 pt}}
\toprule
操作系统 & 发行版 & 编辑器 \\
\midrule
Windows & MikTeX & TexMakerX \\
Unix/Linux & teTeX & Kile \\
Mac OS & MacTeX & TeXShop \\
通用 & TeX Live & TeXworks \\
\bottomrule
\end{tabular}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
% p{80pt}<{\centering}

\begin{table }[ htbp]
\centering
\begin{tabular }{p{80 pt}<{\centering}p{80 pt}<{\raggedleft\arraybackslash }p{80 pt}}
\toprule
操作系统 & 发行版 & 编辑器 \\
\midrule
Windows & MikTeX & TexMakerX \\
Unix/Linux & teTeX & Kile \\
Mac OS & MacTeX & TeXShop \\
通用 & TeX Live & TeXworks \\
\bottomrule
\end{tabular}
\end{table}

9. 将表旋转90度显示

使用宏包rotating

table环境改为sidewaytable

  • \usepackage[figuresleft]{rotating}: 顺时针90度
  • \usepackage[figuresright]{rotating}: 逆时针90度
1
2
3
4
5
6
7
8
9
10
11
12
\usepackage[figuresright]{rotating}

%...

\begin{sidewaystable}[htp]
\caption{This is a caption}
\centering %表格整体居中
\footnotesize %设置字体
\begin{tabular}{l*{7}{L{3cm}}} %共7列,每一列列宽为3cm
%...
\end{tabular}
\end{sidewaystable}

multicolumn 小结

  • m{5cm}: 居中
  • p{5cm}: 顶部
  • b{5cm}: 底部

multicolumn的位置设置

1. 垂直居中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
Day & Min Temp & Max Temp & Summary \\
\hline
% 注意, multicolumn中 | 会覆盖全局的边框设置, 不加的话, 对应单元格也没边框
Monday & 11C & 22C & \multicolumn{1}{|p{5cm}|}{A clear day with lots of sunshine. However, the strong breeze will bring down the temperatures.} \\
\hline
Tuesday & 9C & 19C & \multicolumn{1}{|m{5cm}|}{Cloudy with rain, across many northern regions. Clear spells across most of Scotland and Northern Ireland, but rain reaching the far northwest.} \
\hline
Wednesday & 10C & 21C & \multicolumn{1}{|b{5cm}|}{Rain will still linger for the morning. Conditions will improve by early afternoon and continue throughout the evening.} \\
\hline

\end{tabular}

\end{center}

2. 垂直且水平居中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
\documentclass[a4paper,twoside,12pt]{article}
\usepackage{amsbsy}%
\usepackage{graphicx}
\usepackage{array}
\usepackage{calc}
\newcolumntype{N}{>{\centering\arraybackslash}m{.5in}} % \arraybackslash 就是那个 \\
\newcolumntype{G}{>{\bfseries\centering\arraybackslash}m{2in+6\tabcolsep}}

\begin{document}

\begin{table}[H]
\centering
\begin{tabular}{*{9}{N|}}
\cline{2-9}
\multicolumn{1}{N|}{} & \multicolumn{4}{G|}{Emissions (Pre Catalytic Converter)} & \multicolumn{4}{G|}{Emissions (Post Catalytic Converter)} \tabularnewline \hline
\multicolumn{1}{|N|}{\textbf{Dyno Load (\%)}} & \textbf{CO (\%)} & \textbf{HC (ppm)} & \textbf{Nox (ppm)} & \textbf{$\boldsymbol{\mathrm{CO_2}}$ (\%)} & \textbf{CO (\%)} & \textbf{HC (ppm)} & \textbf{Nox (ppm)} & \textbf{$\boldsymbol{\mathrm{CO_2}}$ (\%)} \\ \hline
\multicolumn{1}{|N|}{10} & 0.77 & 23 & 523 & 14.0 & 0.09 & 18 & 130 & 13.8 \\ \hline
\multicolumn{1}{|c|}{30} & 0.78 & 13 & 555 & 14.2 & - & - & - & - \\ \hline
\multicolumn{1}{|c|}{70} & 0.70 & 14 & 580 & 14.4 & - & - & - & - \\ \hline
\multicolumn{1}{|c|}{90} & 0.74 & 15 & 630 & 14.5 & 0.70 & 15 & 618 & 14.6 \\ \hline
\multicolumn{1}{|c|}{Neutral} & 0.06 & 7 & 21 & 15.6 & 0.06 & 5 & 27 & 15.7 \\ \hline
\end{tabular}
\caption{\textbf{}}
\label{2000Emissions}
\end{table}

\end{document}

3. newcolumntype是如何工作的呢?

详情可见: https://tex.stackexchange.com/questions/257128/how-does-the-newcolumntype-command-work

\arraybackslash

As for \arraybackslash, the \raggedright, \raggedleft, and \centering declarations refine \ in a way that conflicts with its use in a tabular or array environments. The command \arraybackslash (implemented in array and tabularx) restoresthe meaning of \ for use in array and tabular (you would only need \arraybackslash for the last column).


LaTex基础-Table
https://www.torch-fan.site/2022/08/02/LaTex基础-Table/
作者
Torch-Fan
发布于
2022年8月2日
更新于
2022年11月15日
许可协议