\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
