写给自己的歌

Panda loves Bunny

十月 12th, 2013

LaTeX常用表格绘制入门

使用心得, 网文转载, by tumaotm_122.

LaTeX 中经常会碰到绘制表格.

下面通过一个例子来体会 LaTeX 的表格功能.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass{article}
\usepackage{multirow}
\begin{document}
LaTeX table example\\
\verb= http:\\www.chinatex.org=\\
\begin{table}[!hbp]
\begin{tabular}{|c|c|c|c|c|}
\hline
\hline
label 1-1 & label 1-2 & label 1-3 & label 1 -4 & label 1-5 \\
\hline
label 2-1 & label 2-2 & label 3-3 & label 4-4 & label 5-5 \\
\hline
\multirow{2}{*}{Multi-Row} & \multicolumn{2}{|c|}{Multi-Column} & \multicolumn{2}{|c|}{\multirow{2}{*}{Multi-Row and Col}} \\
\cline{2-3}
& column-1 & column-2 & \multicolumn{2}{|c|}{}\\
\hline
\end{tabular}
\caption{My first table}
\end{table}
\end{document}

 

保存,编译,看看是什么样子,下面来解释:

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
\documentclass{article}%开始文档
\usepackage{multirow}%使用多栏宏包
\begin{document}%开始文档
LaTeX table example\\
\verb= http:\\www.chinatex.org =\\
\begin{table}[!hbp]%开始表格
%其中参数[!hbp] 的意思是:
%!表示尽可能的尝试 h(here) 当前位置显示表格,
%如果实在不行显示在 b(bottom) 底部,
\begin{tabular}{|c|c|c|c|c|}%开始绘制表格
%{|c|c|c|c|c|} 表示会有5列, 每个的方式未居中(c),
%也可以改成靠左(l)和靠右(r) 其中 | 表示绘制列线
\hline %绘制一条水平的线
\hline %再绘制一条水平的线
label 1-1 & label 1-2 & label 1-3 & label 1 -4 & label 1-5
%这事表格的第一行, 其中5个元素, 用 &隔开.
\hline
label 2-1 & label 2-2 & label 3-3 & label 4-4 & label 5-5 \\
%这事表格的第二行, 其中5个元素, 用 & 隔开.
\hline
%下面这一段有点复杂,参加后面的解释,可以自己修改慢慢体会.
\multirow{2}{*}{Multi-Row} & \multicolumn{2}{|c|}{Multi-Column} & \multicolumn{2}{|c|}{\multirow{2}{*}{Multi-Row and Col}} \\
%上面开始两行合并, 然后又是正常的两列合并, 接下来是两行两列合并
\cline{2-3} %绘制第2列和第3列的横线
& column-1 & column-2 & \multicolumn{2}{|c|}{}\\
%补偿上面的两列合并的那一行
\hline
\end{tabular}
\caption{My first table} %表格的名称
\end{table}
\end{document}

其中,multirow{2}{*}{text}的第一个参数表示行的数目,*表示由系统自动调整文字,text表示要写入的文字

multicolumnmulticolumn类似,功能是跨多列, \multicolumn{2}{|c|}{text}表示跨2行,文字采用中心对齐的方式,text是要写入的文字。

multicolumnmultirow可以组合使用,跨多行多列,只需要将multirow作为multicolumntext即可。

最后,\cline用于画横线 \cline{i-j}表示从第i列画到第j列.

自定义命令:

让表格中的单元格换行

1
2
3
4
5
6
7
8
9
10
11
\newcommand{\tabincell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
%然后使用&\tabincell{c}{}&就可以在表格中自动换行
 
%比如这么用
\begin{tabular}{|c|c|}
\hline
1 & the first line \\
\hline
2 & \tabincell{c}{haha\\ heihei\\zeze} \\
\hline
\end{tabular}

FAQ:

The table is created, there is no error message and the footnote numbering appears in the table but there is no footnote text at the bottom of the table.
Can somebody explain me how?
How can I solve the problem?

Move the \footnotetext commands outside the table environment; this however will cause the footnotes to appear at the bottom of the page (as the first table in my example below shows); if you want the footnotes to appear right after the table, then you could use the threeparttable package (have a look at the second table in my example code).

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
39
40
41
42
43
44
45
46
47
48
49
\documentclass{article}
\usepackage{threeparttable}
 
 
\begin{document}
 
%A table with footnotes appearing at the bottom of the page:
\begin{table}
  \centering
  \begin{tabular}{llll}
  \hline
  column 1 & column 2 & column 3\footnotemark[1] & column 4\footnotemark[2] \\
  \hline
  row 1 & data 1 & data 2 & data 3 \\
  row 2 & data 1 & data 2 & data 3 \\
  row 3 & data 1 & data 2 & data 3 \\
  \hline
  \end{tabular}
  \caption{Table with footnotes at the bottom of the page}
  \label{tab:test1}
\end{table}
\footnotetext[1]{table footnote 1}
\footnotetext[2]{table footnote 2}
 
\clearpage
 
%A table with footnotes appearing at the bottom of the table:
\begin{table}
  \centering
  \begin{threeparttable}[b]
  \caption{Table with footnotes after the table}
  \label{tab:test2}
  \begin{tabular}{llll}
  \hline
  column 1 & column 2 & column 3\tnote{1} & column 4\tnote{2} \\
  \hline
  row 1 & data 1 & data 2 & data 3 \\
  row 2 & data 1 & data 2 & data 3 \\
  row 3 & data 1 & data 2 & data 3 \\
  \hline
  \end{tabular}
  \begin{tablenotes}
    \item[1] tablefootnote 1
    \item[2] tablefootnote 2
  \end{tablenotes}
 \end{threeparttable}
\end{table}
 
\end{document}

We can get the tools bundle including the tabularx package, which are also support show the footnotes in the table.

In windows environment, we can using console to compile this bundle.

1
Latex tools.ins
1
\usepackage {tabularx}
0

相关博文

Back Top

发表评论

电子邮件地址不会被公开。 必填项已用*标注