summaryrefslogtreecommitdiff
path: root/info/latex-notes-zh-cn/src/tables.tex
blob: 84e2a94c52e91d7e966b8bef5555d7594a30ebde (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
\chapter{表格}
\label{sec:tables}

\section{简单表格}
\verb|tabular|~环境提供了最简单的表格功能。它用~\verb|\hline|~命令代表横线,\verb+|+~代表竖线,用~\verb|&|~来分栏。每个栏位的对齐方式可以用~l、c、r(左中右)来控制。
\begin{code}
\begin{tabular}{|l|c|r|}
    \hline 
    操作系统 & 发行版 & 编辑器 \\
    \hline 
    Windows & MikTeX & TeXnicCenter \\
    \hline 
    Unix/Linux & TeX Live & Emacs \\
    \hline 
    Mac OS & MacTeX & TeXShop \\
    \hline 
\end{tabular}
\end{code}

\begin{tabular}{|l|c|r|}
    \hline 
    操作系统 & 发行版 & 编辑器 \\
    \hline 
    Windows & MikTeX & TeXnicCenter \\
    \hline 
    Unix/Linux & TeX Live & Emacs \\
    \hline 
    Mac OS & MacTeX & TeXShop \\
    \hline 
\end{tabular}
\ \\

和针对插图的~\verb|figure|~环境类似,\LaTeX~还有另一个针对表格的浮动环境~\verb|table|。我们可以用它给上面的示例穿件马甲,顺便把表格简化为科技文献中常用的三线表。

\begin{code}
\begin{table}[htbp]
\caption{浮动环境中的三线表}
\label{tab:threesome}
\centering
\begin{tabular}{lll}
    \hline 
    操作系统 & 发行版 & 编辑器 \\
    \hline 
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \hline 
\end{tabular}
\end{table}
\end{code}

\begin{table}[htbp]
\caption{浮动环境中的三线表}
\label{tab:threesome}
\centering
\begin{tabular}{lll}
    \hline 
    操作系统 & 发行版 & 编辑器 \\
    \hline 
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \hline 
\end{tabular}
\end{table}

完美主义者可能觉得上面示例中的三条线一样粗不够美观,这时可以使用~\verb|booktabs|~宏包\citep{Fear_2005}的几个命令。

\begin{code}
\begin{table}[htbp]
\caption{浮动环境中的三线表}
\centering
\begin{tabular}{lll}
    \toprule
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
\end{code}

\begin{code}
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}
\end{code}

\begin{table}[htbp]
\caption{\texttt{booktabs}~宏包的效果}
\centering
\begin{tabular}{lll}
    \toprule
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}

\section{表格宽度}
有时我们需要控制某栏位宽度,可以将其对齐方式参数从~\verb|l、c、r|~改为~\verb|p{宽度}|~。
\begin{code}
\begin{table}[htbp]
\caption{控制栏位宽度}
\centering
\begin{tabular}{p{100pt}p{100pt}p{100pt}}
    \toprule
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}
\end{code}

\begin{table}[htbp]
\caption{控制栏位宽度}
\centering
\begin{tabular}{p{100pt}p{100pt}p{100pt}}
    \toprule
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}

若想控制整个表格的宽度可以使用~\verb|tabularx|~宏包,\verb|X|~参数表示某栏可以折行。

\begin{code}
\begin{table}[htbp]
\caption{控制表格宽度}
\centering
\begin{tabularx}{350pt}{lXlX}
    \toprule
    李白 & 平林漠漠烟如织,寒山一带伤心碧。暝色入高楼,有人楼上愁。玉梯空伫立,宿鸟归飞急。何处是归程,长亭更短亭。& 
    泰戈尔 & 夏天的飞鸟,飞到我的窗前唱歌,又飞去了。秋天的黄叶,它们没有什么可唱,只叹息一声,飞落在那里。\\
    \bottomrule
\end{tabularx}
\end{table}
\end{code}

\begin{table}[htbp]
\caption{控制表格宽度}
\centering
\begin{tabularx}{350pt}{lXlX}
    \toprule
    李白 & 平林漠漠烟如织,寒山一带伤心碧。暝色入高楼,有人楼上愁。玉阶空伫立,宿鸟归飞急。何处是归程,长亭更短亭。& 
    泰戈尔 & 夏天的飞鸟,飞到我的窗前唱歌,又飞去了。秋天的黄叶,它们没有什么可唱,只叹息一声,飞落在那里。\\
    \bottomrule
\end{tabularx}
\end{table}

\section{跨行、跨列表格}
有时某栏需要横跨几列,我们可以使用~\verb|\multicolumn|~命令。它的前两个参数指定横跨列数和对齐方式。\verb|booktabs|~宏包的~\verb|\cmidrule|~命令用于横跨几列的横线。
\begin{code}
\begin{table}[htbp]
\caption{跨栏表格}
\centering
\begin{tabular}{lll}
    \toprule
    & \multicolumn{2}{c}{常用工具} \\
    \cmidrule{2-3}
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}
\end{code}

\begin{table}[htbp]
\caption{跨栏表格}
\centering
\begin{tabular}{lll}
    \toprule
    & \multicolumn{2}{c}{常用工具} \\
    \cmidrule{2-3}
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}

跨行表格需要使用~\verb|multirow|~宏包,\verb|\multirow|~命令的前两个参数是竖跨的行数和宽度。
\begin{code}
\usepackage{multirow}
...
\begin{table}[htbp]
\caption{跨行表格}
\centering
\begin{tabular}{lllc}
\end{code}
\begin{code}
    \toprule
    操作系统 & 发行版 & 编辑器 & 用户体验\\
    \midrule
    Windows & MikTeX & TeXnicCenter & 
    \multirow{3}{*}{\centering 爽} \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}
\end{code}

\begin{table}[htbp]
\caption{跨行表格}
\centering
\begin{tabular}{lllc}
    \toprule
    操作系统 & 发行版 & 编辑器 & 用户体验 \\
    \midrule
    Windows & MikTeX & TeXnicCenter & 
    \multirow{3}{*}{\centering 爽} \\
    Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}

\section{彩色表格}
彩色表格需要使用~\verb|colortbl|~宏包\citep{Carlisle_2001}提供的一些命令:\verb|\columncolor|、~\verb|\rowcolor|、\verb|\cellcolor|~等。
\begin{code}
\usepackage{colortbl}
...
\begin{table}[htbp]
\caption{彩色表格}
\centering
\begin{tabular}{lll}
    \toprule
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
\end{code}
\begin{code}
    \rowcolor[gray]{.8} Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}
\end{code}

\begin{table}[htbp]
\caption{彩色表格}
\centering
\begin{tabular}{lll}
    \toprule
    操作系统 & 发行版 & 编辑器 \\
    \midrule
    Windows & MikTeX & TeXnicCenter \\
    \rowcolor[gray]{.8} Unix/Linux & TeX Live & Emacs \\
    Mac OS & MacTeX & TeXShop \\
    \bottomrule
\end{tabular}
\end{table}

\section{长表格}
有时表格太长要跨页,可以使用~\verb|longtable|~宏包\citep{Carlisle_2004}。\verb|\endfirsthead|、~\verb|\endhead|~命令用来定义首页表头和通用表头,\verb|\endfoot|、\verb|\endlastfoot|~命令用来定义通用表尾和末页表尾。
\begin{code}
\usepackage{longtable}
...
\begin{longtable}{ll}
\caption{长表格} \\
    \toprule
    作者 & 作品 \\
    \midrule
    \endfirsthead
    \midrule
    作者 & 作品 \\
    \midrule
    \endhead
    \midrule
    \multicolumn{2}{r}{接下页\dots} \\
\end{code}
\begin{code}
    \endfoot
    \bottomrule
    \endlastfoot
    白居易 & 汉皇重色思倾国,\\
    & 御宇多年求不得。\\
    & 杨家有女初长成,\\ 
    & 养在深闺人未识。\\
    & 天生丽质难自弃,\\ 
    & 一朝选在君王侧。\\
    & 回眸一笑百媚生,\\ 
    & 六宫粉黛无颜色。\\
    & 春寒赐浴华清池,\\ 
    & 温泉水滑洗凝脂。\\
    & 侍儿扶起娇无力,\\ 
    & 始是新承恩泽时。\\
    & 云鬓花颜金步摇,\\ 
    & 芙蓉帐暖度春宵。\\
    & 春宵苦短日高起,\\ 
    & 从此君王不早朝。\\
\end{longtable}
\end{code}

\begin{longtable}{ll}
\caption{长表格} \\
    \toprule
    作者 & 作品 \\
    \midrule
    \endfirsthead
    \midrule
    作者 & 作品 \\
    \midrule
    \endhead
    \midrule
    \multicolumn{2}{r}{接下页\dots} \\
    \endfoot
    \bottomrule
    \endlastfoot
    白居易 & 汉皇重色思倾国,\\
    & 御宇多年求不得。\\
    & 杨家有女初长成,\\
    & 养在深闺人未识。\\
    & 天生丽质难自弃,\\
    & 一朝选在君王侧。\\
    & 回眸一笑百媚生,\\
    & 六宫粉黛无颜色。\\
    & 春寒赐浴华清池,\\
    & 温泉水滑洗凝脂。\\
    & 侍儿扶起娇无力,\\
    & 始是新承恩泽时。\\
    & 云鬓花颜金步摇,\\
    & 芙蓉帐暖度春宵。\\
    & 春宵苦短日高起,\\
    & 从此君王不早朝。\\
\end{longtable}

\bibliographystyle{unsrtnat}
\bibliography{reading}
\newpage