现在的位置: 首页 > 综合 > 正文

LaTeX 表格的处理 [转]

2012年10月16日 ⁄ 综合 ⁄ 共 3752字 ⁄ 字号 评论关闭
文章目录

[转] 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}

 

跨页表格

普通的表格不能跨页。如果需要跨页表格,需要使用 longtablesupertabular 等宏包。此处以 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 表格。点击此处下载。

抱歉!评论已关闭.