墘青

LaTeX, Gaussian, ORCA and More...


  • 首页

  • 关于

  • 归档

  • 搜索

关于LaTeX表格的高阶技巧

发表于 2019-08-10 更新于 2019-08-25 分类于 LaTeX

本文的主要内容是:

  • 用工具快速生成表格
  • 三线表的画法
  • 合并单元格
  • 添加斜线表头(Slash header)
  • 给表格添加脚注

快速生成LaTeX表格

手动输入LaTeX的表格实在是太累了!大家一定也曾想尽办法让自己已经写好的Microsoft Word或者Microsoft Excel内的表格变成LaTeX的表格环境。在这里我介绍两种办法让大家方便地将word和excel表格变为LaTeX命令。

在线工具

在线工具的优点在于不需要下载任何文件,打开浏览器就可以用。在这里我推荐Tables Generator这个网站。将我们已经制作好的表格黏贴进去后,立即就可以得到LaTeX命令。同时访问这个网站不需要科学上网。最令人意外的是,这个网站不仅仅支持将表格转换成LaTeX命令,也可以将表格转换成HTML表格、纯文本表格、markdown表格和MediaWiki表格。

classes of lipids polar portion nonpolar portion
triacyglycerol glycerol fatty acid
glycerophospholipid phosphorate group and alcohol fatty acid
sphingolipid phosphorate group and Choline Sphingosine and fatty acid
ganglioside oilgosaccharide Sphingosine and fatty acid
cholesterol the hydroxyl group at C-3 the steroid nucleus and the hydrocarbon side chain at C-17

我们将上面的表格输入这个网站,就可以得到以下的结果:

LaTeX表格

1
2
3
4
5
6
7
8
9
10
11
\begin{table}[]
\begin{tabular}{|l|l|l|}
\hline
\begin{tabular}[c]{@{}l@{}}classes of\\ lipids\end{tabular} & polar portion & nonpolar portion \\ \hline
triacyglycerol & glycerol & fatty acid \\ \hline
glycerophospholipid & phosphorate group and alcohol & fatty acid \\ \hline
sphingolipid & phosphorate group and Choline & Sphingosine and fatty acid \\ \hline
ganglioside & oilgosaccharide & Sphingosine and fatty acid \\ \hline
cholesterol & the hydroxyl group at C-3 & the steroid nucleus and the hydrocarbon side chain at C-17 \\ \hline
\end{tabular}
\end{table}

HTML表格

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
28
29
30
31
32
33
34
35
36
37
38
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-0pky">classes of<br>&nbsp;&nbsp;lipids</th>
<th class="tg-0pky">polar portion</th>
<th class="tg-0pky">nonpolar portion</th>
</tr>
<tr>
<td class="tg-0pky">triacyglycerol</td>
<td class="tg-0pky">glycerol</td>
<td class="tg-0pky">fatty acid</td>
</tr>
<tr>
<td class="tg-0pky">glycerophospholipid</td>
<td class="tg-0pky">phosphorate group and alcohol</td>
<td class="tg-0pky">fatty acid</td>
</tr>
<tr>
<td class="tg-0pky">sphingolipid</td>
<td class="tg-0pky">phosphorate group and Choline</td>
<td class="tg-0pky">Sphingosine and fatty acid</td>
</tr>
<tr>
<td class="tg-0pky">ganglioside</td>
<td class="tg-0pky">oilgosaccharide</td>
<td class="tg-0pky">Sphingosine and fatty acid</td>
</tr>
<tr>
<td class="tg-0pky">cholesterol</td>
<td class="tg-0pky">the hydroxyl group at C-3</td>
<td class="tg-0pky">the steroid nucleus and the hydrocarbon side chain at C-17</td>
</tr>
</table>

纯文本表格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
+---------------------+-------------------------------+------------------------------------------------------------+
| classes of | polar portion | nonpolar portion |
| lipids | | |
+---------------------+-------------------------------+------------------------------------------------------------+
| triacyglycerol | glycerol | fatty acid |
+---------------------+-------------------------------+------------------------------------------------------------+
| glycerophospholipid | phosphorate group and alcohol | fatty acid |
+---------------------+-------------------------------+------------------------------------------------------------+
| sphingolipid | phosphorate group and Choline | Sphingosine and fatty acid |
+---------------------+-------------------------------+------------------------------------------------------------+
| ganglioside | oilgosaccharide | Sphingosine and fatty acid |
+---------------------+-------------------------------+------------------------------------------------------------+
| cholesterol | the hydroxyl group at C-3 | the steroid nucleus and the hydrocarbon side chain at C-17 |
+---------------------+-------------------------------+------------------------------------------------------------+

Markdown表格

1
2
3
4
5
6
7
| classes of   lipids | polar portion                 | nonpolar portion                                           |
|---------------------|-------------------------------|------------------------------------------------------------|
| triacyglycerol | glycerol | fatty acid |
| glycerophospholipid | phosphorate group and alcohol | fatty acid |
| sphingolipid | phosphorate group and Choline | Sphingosine and fatty acid |
| ganglioside | oilgosaccharide | Sphingosine and fatty acid |
| cholesterol | the hydroxyl group at C-3 | the steroid nucleus and the hydrocarbon side chain at C-17 |

本地插件

有很多朋友不喜欢在线工具,因为它们更新速度快,消失的速度也快,同时完全离不开网络。我们也有着可以下载到本地的插件,能够将excel表格转化成LaTeX代码。我要介绍的插件是Excel2LaTeX,这个插件是一个可以应用于Windows平台下的Microsoft Excel 2000-2019的excel插件,是一个标准的.xla文件。

它能支持的Excel表格特性有:

  • 粗体、斜体
  • 精确到每一单元格的左右对齐
  • 每一个单元格的边框样式
  • 字体颜色(使用xcolor宏包)
  • 单元格背景色(使用colorbl宏包)
  • 内容旋转(使用rotating宏包)
  • 合并单元格
  • 能将单元格中的特殊符号转换为LaTeX命令
  • 支持booktabs宏包

其实它还有更多优良的特性,在此不再赘述。

画三线表

三线表是学术论文写作中的必备。三线表通常只有三条线,即顶线、底线和栏目线,这三条线都是横线,表格中没有任何竖线。其中顶线和底线为粗线,栏目线为细线。要制作三线表,我们必须使用booktabs宏包。进入tabular环境后,在对应的位置加入相应的横线:顶线是\toprule,栏目线是\midrule,底线是\bottomrule。

首先在你的导言区加上:

1
\usepackage{booktabs}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\begin{tabular}{ccccc}
\toprule
Year & NME & New BLA & Total& Rate/\% \\
\midrule
2008 & 21 & 3 & 95 & 25.3 \\
2009 & 20 & 6 & 98 & 26.5 \\
2010 & 15 & 6 & 93 & 22.6 \\
2011 & 24 & 6 & 100 & 30 \\
2012 & 33 & 6 & 101 & 38.6 \\
2013 & 25 & 4 & 102 & 28.4 \\
2014 & 30 & 11 & 119 & 34.4 \\
2015 & 33 & 12 & 127 & 35.4 \\
2016 & 15 & 7 & 102 & 21.6 \\
2017 & 34 & 12 & 150 & 30.7 \\
2018 & 42 & 17 & 151 & 39.1 \\
\bottomrule
\end{tabular}

m2jVw8.png

合并单元格

合并单元格在LaTeX中看起来是个非常非常困难的事情。从完成这件事上所用的宏包的数量也可以看出着并不简单。为了达到这个目的,我们需要使用两个宏包array和multirow。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\documentclass{article}
\usepackage{array}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{metalogo}
\renewcommand{\arraystretch}{1.6}
\begin{document}
\begin{tabular}{@{}p{1.5cm}p{1.6cm}>{\raggedleft}p{1cm}
>{\raggedright}p{1.6cm}r@{}}
Compiler & \multicolumn{2}{c}{Input}
& \multicolumn{2}{c@{}}{Output} \\
\cmidrule(r){1-1}\cmidrule(lr){2-3}\cmidrule(l){4-5}
& Encoding & Images & Fonts & Format \\
\cmidrule(lr){2-2}\cmidrule(lr){3-3}
\cmidrule(lr){4-4}\cmidrule(l){5-5}
\LaTeX & utf8, ascii, applemac, latin1, \ldots
& EPS & Type 1, Type 3 & DVI \\
pdf\LaTeX & utf8, ascii, applemac, latin1, \ldots
& PDF PNG JPG & Type 1, Type 3 & PDF \\
\XeLaTeX, \LuaLaTeX & utf8 & PDF PNG JPG & Type 1,
Type 3, OpenType, Graphite, TrueType & PDF \\
\end{tabular}
\end{document}

m2jEef.png

让我们来看看tabular环境的参数的意义:

  • @{code}: 这将插入给定代码而不是列分隔空间。代码可以是诸如符号或空间宏之类的命令。在这里,我们将其设置为空,从而实现与线的左对齐。
  • p{width}: 这指定了包含文本的特定宽度的单元格。字母p代表段落。
  • >{code}: 这会在列的单元格之前插入代码,而<{code}之后会执行此操作。这来自array宏包,并在其手册中有所记录。在这里,我们使用它来插入\raggedleft命令以避免默认的完全对齐,这在表格单元格中不太好。
  • r,l,c: 这些分别代表右对齐,左对齐,居中对齐。

现在,让我们再将着眼点放在Input这个单元格。

1
\multicolumn{number of columns}{formatting}{text}

在这里,我们将列数设置为2,并设置为居中格式,最后输入单元格文本。

添加斜线表头

斜线表头是设计表格时常用的元素。在LaTeX中实现这一点非常容易,只需要使用slashbox宏包即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass{article}
\usepackage{slashbox}
\begin{document}
\renewcommand{\arraystretch}{1.8}
\begin{tabular}{|l|c|c|c|c|c|}
\hline
\slashbox{Time}{Weekday} & Monday & Tuesday
& Wednesday & Thursday & Friday \\
\hline
8--10 & & & & & \\
10--12 & & & & & \\
12--14 & & & & & \\
14--16 & & & & & \\
\hline
\end{tabular}
\end{document}

slashbox宏包提供了两种命令,分别是:

1
2
\slashbox[width][trim]{left top text}{right bottom text}
\backslashbox[width][trim]{left bottom text}{right top text}

可选参数用于在自动计算不适合的情况下进行调整。你可以指定斜线列的宽度;可以分别通过声明l,r或lr来选择左侧修剪,右侧修剪或两侧切割,从而选择默认左侧和右侧柱分隔空间的修剪。

需要注意的是slashbox宏包并不被TeX Live发行版包括。事实上,它是一个非常古老的宏包,并不建议大家使用。

slashbox的替代品diagbox宏包兼容了slashbox宏包提供的所有命令。

1
\usepackage{diagbox}

在导言区加载完之后,我们就可以开始排版表头了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\documentclass{article}
\usepackage{diagbox}
\begin{document}
\begin{tabular}{|l|ccc|}
\hline
\diagbox{Time}{Day} & Mon & Tue & Wed \\
\hline
Morning & used & used & \\
Afternoon & & used & used \\
\hline
\end{tabular}
\begin{tabular}{|l|ccc|}
\hline
\diagbox{Time}{Room}{Day} & Mon & Tue & Wed \\
\hline
Morning & used & used & \\
Afternoon & & used & used \\
\hline
\end{tabular}
\end{document}

m2jiQI.png

同时,\diagbox命令还有非常多的参数,比如可以指定斜线方向的dir,它可以取 NW(西北)、NE(东北)、SW(西南)、SE(东南)四种方向。在只有一条斜线的表头中,NE 与 SW、SE 与 NW 是等价的。斜线方向的默认值是 NW。

m2jPSA.png

m2jFyt.png

\diagbox的更多参数以及diagbox宏包的更多功能详见其文档。

添加脚注

表格中的条目(例如行标题文本)应该很短,否则会出现问题。例如,长标题可能会让我们的眼睛更难以跟随短行条目但是跳过更宽的行。在保持表本身简洁明快的同时添加必要细节的一种方法是使用脚注。

比起将笔记放在页面底部,直接在表格底部添加注记更好。我们也可称它们为表注。

为了做到这一点,我们需要使用threeparttable宏包。

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
28
29
30
31
32
33
34
35
36
37
38
\documentclass{article}
\usepackage{booktabs}
\usepackage{bbding}
\usepackage{threeparttable}
\begin{document}
\begin{table}
\centering
\renewcommand{\arraystretch}{1.6}
\begin{threeparttable}
\begin{tabular}{lccccc}
\toprule
Class & Part page & Chapters & Abstract\tnote{1}
& Front-/Backmatter\tnote{2}
& Appendix name\tnote{3} \\
\cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(lr){3-3}
\cmidrule(lr){4-4}\cmidrule(lr){5-5}\cmidrule(l){6-6}
article & & & \Checkmark & \\
book & \Checkmark & \Checkmark & &
\Checkmark & \Checkmark \\
report & \Checkmark & \Checkmark & \Checkmark &
& \Checkmark \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\item[1] An environment: \verb|\begin{abstract}|
\ldots \verb|\end{abstract}|
\item[2] Commands: \verb|\frontmatter|,
\verb|\mainmatter|, \verb|\backmatter|
\item[3] The command \verb|\appendix| exists in
Article too, but there's no prefix
``Appendix''.
\end{tablenotes}
\end{threeparttable}
\caption{Structuring differences between standard
\LaTeX\ classes}
\label{comparison}
\end{table}
\end{document}

m2jkOP.png

其中,threeparttable是用于取代table环境的新环境,在threeparttable环境的最后可以添加另一种环境tablenotes来为你的表格提供注释。

在表格中使用注释的方法是使用命令\tnote{reference number},而引用参数来自于环境tablenotes。

tablenotes和itemize非常相像。使用的语法也是一模一样的。

1
2
3
4
5
6
7
8
9
\begin{tablenotes}
\item[1] An environment: \verb|\begin{abstract}|
\ldots \verb|\end{abstract}|
\item[2] Commands: \verb|\frontmatter|,
\verb|\mainmatter|, \verb|\backmatter|
\item[3] The command \verb|\appendix| exists in
Article too, but there's no prefix
``Appendix''.
\end{tablenotes}
任之 wechat
订阅公众号以获取最新内容!
用LaTeX制作幻灯片
充分利用PDF格式
  • 文章目录
  • 站点概览
任之

任之

Personal blog featuring tutorials for LaTeX, Gaussian and ORCA in Chinese.
32 日志
4 分类
4 标签
GitHub E-Mail
  1. 1. 快速生成LaTeX表格
    1. 1.1. 在线工具
      1. 1.1.1. LaTeX表格
      2. 1.1.2. HTML表格
      3. 1.1.3. 纯文本表格
      4. 1.1.4. Markdown表格
    2. 1.2. 本地插件
  2. 2. 画三线表
  3. 3. 合并单元格
  4. 4. 添加斜线表头
  5. 5. 添加脚注
© 2020 任之
Alexander Qi 专属
|
闲言碎语