[轉] http://space.uibe.edu.cn/u1/ryang/latex-table.html
LaTeX 表格的處理
- LaTeX 表格處理概述
- 一般三線表的處理
- 帶表格注釋的三線表
- 固定列寬和自動伸縮列寬
-
- 固定列寬與對齊方式
- 自動伸縮列寬
- 跨頁表格
- 表格旋轉和后置
-
- 表格旋轉
- 表格后置
- 輔助轉換工具
LaTeX 表格處理概述
與 word 不同,LaTeX 通過一定的語法規則將表格寫成純文本形式。基本規則包括:表格從上到下,每一行從左到右,單元格內容使用 &
分隔,用 \\
換行。最基本的表格環境是 tabular
環境。下面是一個簡單的表格代碼和實際效果:
\begin{tabular}[t]{l|c} \hline 姓名 & 年齡 \\ \hline 張三 & 32 \\ 李四 & 12 \\ 王五 & 24 \\ \hline \end{tabular}
一般三線表的處理
學術論文普遍使用三線表。三線表的特點主要是:整個表格通常只有三條橫線,首尾兩條橫線較粗,中間一條較細,一般不使用豎線。LaTeX 處理三線表相當簡單方便。用到的宏包主要是 booktabs
。下面是普通三線表的代碼和效果:
\begin{table}[htbp] \caption{\label{tab:test}示例表格} \begin{tabular}{lcl} \toprule 姓名 & 年齡 & 地址\\ \midrule 張三 & 32 & 中華人民共和國\\ 李四 & 12 & 中華人民共和國\\ 王五 & 24 & 中華人民共和國\\ \bottomrule \end{tabular} \end{table}
帶表格注釋的三線表
三線表有時候還需要加上注釋以便給出表格的資料來源等信息。解決這一問題可以使用下面三個辦法之一:
- 使用
ctable
宏包。該宏包用法簡單,下面是典型代碼和效果:
\ctable[% caption=The Skewing Angles, label=tab:nowidth, ]{lcc} {\tnote{for the abstraction reaction, $Mu+HX \rightarrow MuH+X$.} \tnote[b]{1 degree${} = \pi/180$ radians.} \tnote[c]{this is a particularly long note, showing that footnotes are set in raggedright mode as we don't like hyphenation in table footnotes.} } {\FL & $H(Mu)+F_2$ & $H(Mu)+Cl_2$ \ML $\beta$(H) & $80.9$\tmark[b] & $83.2$ \NN $\beta$(Mu) & $86.7$ & $87.7$ \LL }
- 使用
threeparttable
宏包。下面是典型代碼和效果:
\begin{table}[htbp] \centering\small \begin{threeparttable} \caption{\label{tab:results}Effect of Trade Openness on Environment (Air Pollution)} \begin{tabular}{lccc} \toprule & NO$_2$ & SO$_2$ & PM \\ \midrule $\ln(y/pop)$ & 408.74* & 287.25* & 566.65 \\ & (121.79) & (118.81) & (336.19) \\ $\ln(y/pop)^2$ & $-$22.85* & $-$16.58* & $-$35.57** \\ & (6.90) & (6.78) & (19.06) \\ $(X+M)/Y$ & $-$.29** & $-$.31* & $-$.37 \\ & (.17) & (.08) & (.34) \\ $Polity$ & $-$3.20* & $-$6.58* & $-$6.70** \\ & (1.47) & (2.05) & (3.42) \\ $\ln(LandArea/pop)$ & $-$5.94 & $-$2.92* & $-$13.02* \\ & (5.93) & (1.39) & (6.29) \\ Obs. & 36 & 41 & 38 \\ $R^2$ & 0.16 & 0.68 & 0.62 \\ \bottomrule \end{tabular} \small Note: Robust standard errors in parentheses. Intercept included but not reported. \begin{tablenotes} \item[*] significant at 5\% level \item[**] significant at 10\% level \end{tablenotes} \end{threeparttable} \end{table}
固定列寬和自動伸縮列寬
有時三線表需要固定某列的列寬,或者指定整個表格的總寬度,指定某幾列自動伸縮。
固定列寬與對齊方式
固定列寬可以使用 array
宏包的 p{2cm}
系列命令,如果需要指定水平對齊方式,可以使用下面的形式 >{\centering}p{2cm}
實現,但如果使用這種方式,缺省情況下不能使用 \\
換行,需要使用 \tabularnewline
代替。為了仍然使用 \\
換行,需要在導言區加上下面的代碼:
\usepackage{array} \newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp} \newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}} \newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}} \newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}
使用 C{3cm}
命令即可指定該列寬度為 3cm,並且文字居中對齊,左對齊和右對齊命令分別是 L{2cm}
和 R{2cm}
。
下面是一個的例子:
\begin{table}[htbp] \centering\caption{\label{tab:test}2000 和~2004 年中國制造業產品的出口份額} \begin{tabular}{l*{2}{R{2cm}}} \toprule & 2000 & 2004 \\ \midrule 鋼鐵 & 3.1 & 5.2 \\ 化學制品 & 2.1 & 2.7 \\ 辦公設備及電信設備 & 4.5 & 15.2 \\ 汽車產品 & 0.3 & 0.7 \\ 紡織品 & 10.4 & 17.2 \\ 服裝 & 18.3 & 24\\ \bottomrule \end{tabular} \end{table}
自動伸縮列寬
使用 tabularx
宏包可以實現自動伸縮列寬。下面是一個簡單的例子。與普通的 tabular
環境不同之處在於:(1)需要指定整個表格的總寬度;(2)需要用 X
指定至少一列為自動伸縮列。
\begin{table}[htbp] \centering\caption{\label{tab:test}2000 和~2004 年中國制造業產品的出口份額} \begin{tabularx}{10cm}{Xrr} \toprule & 2000 & 2004 \\ \midrule 鋼鐵 & 3.1 & 5.2 \\ 化學制品 & 2.1 & 2.7 \\ 辦公設備及電信設備 & 4.5 & 15.2 \\ 汽車產品 & 0.3 & 0.7 \\ 紡織品 & 10.4 & 17.2 \\ 服裝 & 18.3 & 24\\ \bottomrule \end{tabularx} \end{table}
跨頁表格
普通的表格不能跨頁。如果需要跨頁表格,需要使用 longtable
或 supertabular
等宏包。此處以 longtable
為主介紹。
下面是一個例子。
\begin{longtable}{p{1.2cm}p{8cm}p{5cm}} \caption{\label{tab:test}WTO 英語縮寫}\\ \toprule 縮寫 & 原\hspace{1em}文 & 解\hspace{1em}釋\\ \midrule \endfirsthead {\bf 續表~\ref{tab:test}}\\ \toprule 縮寫 & 原\hspace{1em}文 & 解\hspace{1em}釋\\ \midrule \endhead \endfoot \bottomrule \endlastfoot WTO & World Trade Organization & 世界貿易組織\\ TRIMs & Trade-Related Investment Measures & 與貿易有關的投資措施\\ TPR & Trade Policy Review & 貿易政策審議\\ .... \end{longtable}
表格旋轉和后置
表格旋轉
如果表格過寬,可以將表格旋轉 90 度橫放。使用 rotating
宏包即可實現此功能。與普通表格的不同之處是:需要將 table
環境替換成 sidewaystable
環境。
表格后置
使用 endfloat
宏包可以將文章中的所有圖表置於文章末尾,以滿足某些雜志的排版要求。
輔助轉換工具
- calc2latex 或 excel2latex 可以將電子表格文件數據轉換為 latex 表格。
- dat2latex.pl 這是 perl 腳本,可將 gnuplot 的文本數據文件轉換為 latex 表格。點擊此處下載。