表格

表格在学术写作中是常见的,通常用于总结研究结果。因此,掌握在 LaTeX 中构建表格的技巧对于制作高质量的论文非常重要,经过足够的练习,您可以打印出各种类型的漂亮表格。

请记住,LaTeX 不是一个电子表格工具,因此最好使用专门的工具来构建表格,然后将这些表格导入文档中。基本的表格不难构建,但更复杂的表格可能需要一些技巧;在这种情况下,更高级的包非常有用。不过,首先了解基础知识是非常重要的。一旦您熟练掌握了基本的 LaTeX 表格,您可以进一步了解更高级的包或您最喜欢的电子表格软件的导出选项。由于 LaTeX 的模块化特性,整个过程可以以非常便捷的方式自动化。

引言

LaTeX 内置了表格排版支持,并提供了两个环境:tabulartable。要在行和列中排版内容,需要使用 tabular 环境;而可选的 table 环境则是一个类似于图形的浮动容器,可以在其中包含 tabular 环境。

table 环境包含标题,并定义表格的浮动属性,即表格应该在文档中出现的位置,以及是否要居中显示。\caption\label 命令与图形的使用方式相同。关于 table 环境的更多息,请参见“浮动与表格”部分。

无论如何,表格的实际内容都位于 tabular 环境内。

tabular 环境

tabular 环境用于排版具有可选水平和垂直线的表格。LaTeX 会自动确定列的宽度。

环境的第一行格式如下:

\begin{tabular}[pos]{table spec}

table spec 参数告诉 LaTeX 使用哪些对齐方式,并在列之间插入哪些垂直线。

不需要明确指定列数,因为 LaTeX 会根据提供的参数数量自动推断出来。也可以在此处为列之间添加垂直线。以下符号可用于描述表格列(其中一些需要加载 array 包):

  • l:左对齐列

  • c:居中列

  • r:右对齐列

  • p{'width'}:段落列,文本垂直对齐顶部

  • m{'width'}:段落列,文本垂直居中(需要 array 包)

  • b{'width'}:段落列,文本垂直对齐底部(需要 array 包)

  • |:垂直线

  • ||:双垂直线

默认情况下,如果列中的文本宽度超出了页面,LaTeX 不会自动换行。使用 p{'width'} 可以定义一种特殊类型的列,该列会像正常段落一样换行。您可以使用 LaTeX 支持的任何单位(如 ptcm)或命令长度(如 \textwidth)来指定宽度。有关更多息,请参见“长度”章节。

可选参数 pos 可用于指定表格相对于周围文本基线的垂直位置。大多数情况下,您不需要此选项。仅当您的表格不在独立段落中时,此选项才有意义。可以使用以下字母:

  • b:底部

  • c:居中(默认)

  • t:顶部

指定字体格式

要为整列指定字体格式(如粗体、斜体等),可以在声明对齐方式之前添加 >{\format}。例如:

\begin{tabular}{ >{\bfseries}l c >{\itshape}r }

这将指示一个三列的表格,第一列左对齐并使用粗体,第二列居中并使用常规字体,第三列右对齐并使用斜体。要使其生效,您需要在前言中加载 array 包。

在第一行中,您已经指定了列数、对齐方式和分隔列的垂直线。一旦进入环境,就需要输入所需的文本,用 & 分隔单元格,并使用 \\ 引入新行。您需要使用以下命令:

  • &:列分隔符

  • \\:开始新行(可以使用方括号指定额外的空间,如 \\[6pt]

  • \hline:水平线

  • \newline:在单元格内开始新行(在段落列中)

  • \cline{i-j}:从第 i 列到第 j 列的部分水平线

请注意,命令之间插入的空格纯粹是为了个人偏好。我个人会在命令之间添加空格,以使其更容易阅读。

基本示例

以下示例展示了如何在 LaTeX 中创建一个简单的表格。它是一个三行三列的表格,但没有任何线条。

\begin{tabular}{ l c r }
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}

生成的输出如下:

1  2  3
4  5  6
7  8  9

通过加入一些垂直线,您可以扩展这个例子:

\begin{tabular}{ l | c | r }
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}

输出:

1 | 2 | 3
4 | 5 | 6
7 | 8 | 9

添加表格顶部和底部的水平线:

\begin{tabular}{ l | c | r }
  \hline			
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
  \hline  
\end{tabular}

输出:

1 | 2 | 3
4 | 5 | 6
7 | 8 | 9

最后,添加所有行之间的线条,并居中显示(注意使用 center 环境):

\begin{center}
  \begin{tabular}{ l | c | r }
    \hline
    1 & 2 & 3 \\ \hline
    4 & 5 & 6 \\ \hline
    7 & 8 & 9 \\
    \hline
  \end{tabular}
\end{center}

输出:

1 | 2 | 3
4 | 5 | 6
7 | 8 | 9

表格中的文本换行

LaTeX 排版表格时存在一些局限,其中之一是它不会自动换行,即使文本超出了页面的宽度。对于可能包含超长文本的列,建议使用 p 属性并指定列的宽度(尽管可能需要一些反复试验来获得理想效果)。对于更方便的方式,可以查看 tabularx 包或 tabulary 包。

除了使用 p 外,还可以使用 m 属性将文本垂直居中,或使用 b 属性将文本对齐到底部。

示例

以下代码创建了两个表格,唯一的区别是第二个表格的最后一列设置了 5 厘米的宽度,而第一个表格没有指定宽度。编译以下代码:

\documentclass{article}
\usepackage[english]{babel}

\begin{document}

Without specifying width for last column:
\begin{center}
    \begin{tabular}{| l | l | l | l |}
    \hline
    Day & Min Temp & Max Temp & Summary \\ \hline
    Monday & 11C & 22C & A clear day with lots of sunshine.
    However, the strong breeze will bring down the temperatures. \\ \hline
    Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells 
    across most of Scotland and Northern Ireland, 
    but rain reaching the far northwest. \\ \hline
    Wednesday & 10C & 21C & Rain will still linger for the morning. 
    Conditions will improve by early afternoon and continue 
    throughout the evening. \\
    \hline
    \end{tabular}
\end{center}

With width specified:
\begin{center}
    \begin{tabular}{ | l | l | l | p{5cm} |}
    \hline
    Day & Min Temp & Max Temp & Summary \\ \hline
    Monday & 11C & 22C & A clear day with lots of sunshine.  
    However, the strong breeze will bring down the temperatures. \\ \hline
    Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells 
    across most of Scotland and Northern Ireland, 
    but rain reaching the far northwest. \\ \hline
    Wednesday & 10C & 21C & Rain will still linger for the morning. 
    Conditions will improve by early afternoon and continue 
    throughout the evening. \\
    \hline
    \end{tabular}
\end{center}

\end{document}

您将得到以下输出:

  • 第一个表格已被裁剪,因为输出宽度超过了页面宽度。

手动换行

有时不希望依赖断行算法,特别是使用 p 指定符时,而是希望手动指定换行。在这种情况下,最简单的方法是使用 \parbox

\begin{tabular}{cc}
  boring cell content & \parbox[t]{5cm}{rather long par\\new par}
\end{tabular}

此处 t 参数控制文本在框中的放置方式。其他允许的值为 c(居中)和 b(底部)。

列间距

如果需要调整列间的间距(默认情况下 LaTeX 会选择非常紧凑的列),可以更改列分隔:\setlength{\tabcolsep}{5pt}。默认值是 6pt

还可以使用 hspace 插入水平空间,例如:

\begin{tabular}{r@{\hspace{1in}}rr}

hspace长度有多种选择。

行间距

通过重新定义 \arraystretch 命令来设置行间距:

\renewcommand{\arraystretch}{1.5}

默认值是 1.0

调整规则间距的另一种方法是在 \hline\cline{i-j} 命令之前或之后添加 \noalign{\smallskip}

\begin{tabular}{ | l | l | r | }
  \hline\noalign{\smallskip}
  \multicolumn{2}{c}{Item} \\
  \cline{1-2}\noalign{\smallskip}
  Animal & Description & Price (\$) \\
  \noalign{\smallskip}\hline\noalign{\smallskip}
  Gnat  & per gram & 13.65 \\
        & each     &  0.01 \\
  Gnu   & stuffed  & 92.50 \\
  Emu   & stuffed  & 33.33 \\
  Armadillo & frozen & 8.99 \\
  \noalign{\smallskip}\hline
\end{tabular}

您还可以使用粘合剂在行终止符后显式指定跳过的空间:

\begin{tabular}{ll}
\hline
Mineral & Color \\[1cm]
Ruby & red \\
Sapphire & blue \\
\hline
\end{tabular}

表格中的其他环境

如果您在表格单元格内使用 LaTeX 环境,如 verbatimenumerate

\begin{tabular}{c c}
	\hline
	\begin{verbatim}
	code
	\end{verbatim}
	& description
 	\\ \hline
\end{tabular}

您可能会遇到类似于 ! LaTeX Error: Something's wrong--perhaps a missing \item. 的错误。

要解决此问题,请将列指定符更改为“段落”类型(pmb):

\begin{tabular}{m{5cm} c}

定义多个列

您可以使用 *{num}{str} 语法一次定义多个对齐方式相同的列。当表格列较多时,这非常有用。

以下是一个六列居中对齐的表格,两侧各有一列:

\begin{tabular}{l*{6}{c}r}
Team              & P & W & D & L & F  & A & Pts \\
\hline
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular}

使用 >{\cmd}<{\cmd} 的列指定

列的规格可以通过 array 包来更改。这是在 tabular 环境的参数中完成的,>{\command} 用于在每个列元素前执行命令,而 <{\command} 用于在每个列元素后执行命令。示例:要使某一列处于数学模式,使用 \begin{tabular}{>{$}c<{$}}。另一个例子是改变字体\begin{tabular}{>{\small}c} 用于将列中的文本打印为小字体

>< 指定符的参数必须正确匹配 {</code> 和 <code inline="">} 字符。这意味着 >{\bfseries} 是有效的,而 >{\textbf} 将无法工作,>{\textbf{}} 也不是有效的。如果需要将表格的文本作为参数(例如,使用 \textbf 来生成粗体文本),应该使用 \bgroup\egroup 命令:>{\textbf\bgroup}c<{\egroup} 会产生预期的效果。对于其他命令(例如使用 \underline 为文本加下划线),需要暂时将列文本存储在一个盒子中,使用 lrbox

首先,必须使用 \newsavebox{\boxname} 定义这样的盒子,然后可以定义:

>{\begin{lrbox}{\boxname} }%
l%
<{\end{lrbox}%
  \underline{\unhbox\boxname} }%

这将文本存储在一个盒子中,并使用 \unhbox盒子中取出文本(如果需要再次使用盒子,可以使用 \unhcopy 代替)。然后,将文本传递给 \underline。对于 LaTeX2e,您可能需要使用 \usebox{\boxname} 替代 \unhbox\boxname

如果使用 \raisebox 而不是 \underline,可以强制表格中的所有行具有相同的高度,而不是自然的变化高度(例如,数学术语或上标出现时)。

示例:使用 p{...}>{\centering}

\begin{tabular}{>{\centering}p{3.5cm}>{\centering}p{3.5cm} }
Geometry  & Algebra
\tabularnewline
\hline
 Points & Addition 
\tabularnewline
 Spheres & Multiplication 
\end{tabular}

注意使用 \tabularnewline 代替 \\,以避免出现错误 Misplaced \noalign

@! 表达式

列分隔符可以使用 @{...}!{...} 语法指定。

通常,它需要一些文本作为参数,当将其附加到列时,它会自动将文本插入到该列的每个单元格中,在实际数据之前。@{...} 命令去掉列间的空间,并用大括号之间的内容替换它。为了保持初始空间,使用 !{...}。要添加空间,可以使用 @{\hspace{''width''}}

以下是一个有用的示例,用于对齐小数点:

\begin{tabular}{r@{.}l}
  3   & 14159 \\
  16  & 2     \\
  123 & 456   \\
\end{tabular}

输出:

3.14159
16.2
123.456

@ 表达式的去空间特性使其在操控列间水平间距时非常有用。例如,基本表格中的列描述如下:

\begin{tabular}{ |l|l| }
  \hline
  stuff & stuff \\ \hline
  stuff & stuff \\
  \hline
\end{tabular}

使用 dcolumn 对齐小数点

可以使用 dcolumn 包代替 @ 表达式对小数点对齐的列进行构建,dcolumn 提供了一个新的列指定符,用于浮动数据。要使用 dcolumn,首先在导言区加载该包并定义列类型:

\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{\cdot}{#1} }
\begin{tabular}{l r c d{1} }
Left&Right&Center&\mathrm{Decimal}\\
1&2&3&4\\
11&22&33&44\\
1.1&2.2&3.3&4.4\\
\end{tabular}

使用 dcolumn 加粗文本

为了突出表格中的某些条目,可以使用粗体文本。由于 dcolumn 需要看到小数点,这使得使用粗体文本变得更加困难。为了避免数字对齐问题,可以通过以下方式定义新的粗体列类型:

\usepackage{dcolumn}
\DeclareMathVersion{nxbold} 
\SetSymbolFont{operators}{nxbold}{OT1}{cmr} {b}{n}
\SetSymbolFont{letters}  {nxbold}{OML}{cmm} {b}{it}
\SetSymbolFont{symbols}  {nxbold}{OMS}{cmsy}{b}{n}

\makeatletter
\newcolumntype{d}{D{.}{.}{-1} } % decimal column
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3} }c<{\DC@end} } % wide bold decimal column
\newcolumntype{Z}[3]{>{\mathversion{nxbold}\DC@{#1}{#2}{#3} }c<{\DC@end} } % normal width bold decimal column
\makeatother

行规格

将相同的命令应用到每一行的每一个单元格可能会很方便,就像列的应用一样。不幸的是,tabular 环境默认无法做到这一点。我们需要使用 tabu 包,它提供了 \rowfont 选项:

\begin{tabu}{XX}
\rowfont{\bfseries\itshape\large} Header1 & Header2 \\
\hline
Cell2 & Cell2
\end{tabu}

跨越单元格

为了创建稍微复杂一些的表格,我们需要使用表格数据本身嵌入必要的命令。

行跨越多个列

命令格式如下:\multicolumn{num_cols}{alignment}{contents}num_cols 是要合并的列数;alignment 可以是 l(左对齐)、c(居中对齐)、r(右对齐),或者指定一个宽度,例如 p{5.0cm} 来让文本换行;contents 是单元格的实际数据。一个简单的例子:

\begin{tabular}{ |l|l| }
  \hline
  \multicolumn{2}{|c|}{Team sheet} \\
  \hline
  GK & Paul Robinson \\
  LB & Lucas Radebe \\
  DC & Michael Duberry \\
  DC & Dominic Matteo \\
  RB & Dider Domi \\
  MC & David Batty \\
  MC & Eirik Bakke \\
  MC & Jody Morris \\
  FW & Jamie McMaster \\
  ST & Alan Smith \\
  ST & Mark Viduka \\
  \hline
\end{tabular}

列跨越多行

首先需要在导言区添加 \usepackage{multirow}。然后使用以下命令来实现跨越行:\multirow{num_rows}{width}{contents}。其中,num_rows 是要合并的行数,width 是该列的宽度(* 表示内容的自然宽度),contents 是要放入该单元格的实际数据。

\usepackage{multirow}

\begin{tabular}{ |l|l|l| }
\hline
\multicolumn{3}{ |c| }{Team sheet} \\
\hline
Goalkeeper & GK & Paul Robinson \\ \hline
\multirow{4}{*}{Defenders} & LB & Lucas Radebe \\
 & DC & Michael Duburry \\
 & DC & Dominic Matteo \\
 & RB & Didier Domi \\ \hline
\multirow{3}{*}{Midfielders} & MC & David Batty \\
 & MC & Eirik Bakke \\
 & MC & Jody Morris \\ \hline
Forward & FW & Jamie McMaster \\ \hline
\multirow{2}{*}{Strikers} & ST & Alan Smith \\
 & ST & Mark Viduka \\
\hline
\end{tabular}

使用 \multirow 时,注意需要在后续行的每个单元格中插入空白条目。若没有数据,可以保持空白,但仍需使用 & 来分隔与下一列的数据。

同时跨越行和列

这是一个稍微复杂一些的示例,展示了如何同时跨越行和列,并确保单元格边框正确绘制:

\usepackage{multirow}

\begin{tabular}{cc|c|c|c|c|l}
\cline{3-6}
& & \multicolumn{4}{ c| }{Primes} \\ \cline{3-6}
& & 2 & 3 & 5 & 7 \\ \cline{1-6}
\multicolumn{1}{ |c  }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{504} & 3 & 2 & 0 & 1 &     \\ \cline{2-6}
\multicolumn{1}{ |c  }{}                        &
\multicolumn{1}{ |c| }{540} & 2 & 3 & 1 & 0 &     \\ \cline{1-6}
\multicolumn{1}{ |c  }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{gcd} & 2 & 2 & 0 & 0 & min \\ \cline{2-6}
\multicolumn{1}{ |c  }{}                        &
\multicolumn{1}{ |c| }{lcm} & 3 & 3 & 1 & 1 & max \\ \cline{1-6}
\end{tabular}

在这个例子中, \multicolumn{1}{ |c }{</code> 被用来在每个跨行的单元格两侧绘制垂直边框。<code inline=""> \multirow{2}{*}{...} 用来跨越两行并合并内容。

控制表格大小

调整表格大小

graphicx 包提供了 \resizebox{width}{height}{object} 命令,可以用来调整表格的大小。以下示例展示了如何将表格宽度调整为 8 厘米,同时保持原始的宽高比例:

\usepackage{graphicx}
% ...

\resizebox{8cm}{!} {
  \begin{tabular}...
  \end{tabular}
}

包括标题的调整

\begin{table}[h]
\resizebox{1.4\textwidth}{!}{\begin{minipage}{\textwidth}
\begin{tabular}{r|c|c|}
& \multicolumn{1}{c}{noninteractive}
& \multicolumn{1}{c}{interactive} \\
\cline{2-3}
massively multiple & Library & University \\
\cline{2-3}
one-to-one & Book & Tutor \\
\cline{2-3}
\end{tabular}
\caption[Table caption text]{Table taken from \cite[p.10]{refid} }
\label{table:name}
\end{minipage} }
\end{table}

使用比例调整大小

\usepackage{graphicx}
% ...

\scalebox{0.7}{
  \begin{tabular}...
  \end{tabular}
}

改变字体大小

可以通过在表格范围内添加所需的字体大小命令来全局更改表格的字体大小,表格范围可以在 \begin{table} 语句之后或需要加上分组定界符:

{\footnotesize
  \begin{tabular}{| r | r || c | c | c |}
      % ...
  \end{tabular}
}

或者,您可以在导言区添加以下代码来更改文档中所有表格的默认字体

\let\oldtabular\tabular 
\renewcommand{\tabular}{\footnotesize\oldtabular}

控制行间距

要调整行间距,可以重新定义 \arraystretch 命令:

\renewcommand{\arraystretch}{1.5}

默认值是 1.0。

表格中的颜色

交替行颜色

xcolor 包提供了在表格中使用交替行颜色的必要命令,当与 table 选项一起加载时。命令 \rowcolors{<starting row>}{<odd color>}{<even color>} 必须在 tabular 环境开始之前指定。

\documentclass{article}
\usepackage[table]{xcolor}

\begin{document}

\begin{center}
\rowcolors{1}{green}{pink}
\begin{tabular}{lll}
odd 	& odd 	& odd \\
even 	& even 	& even\\
odd 	& odd 	& odd \\
even 	& even 	& even\\
\end{tabular}
\end{center}

\end{document}

如果需要停用特定行的高亮显示,可以使用 \hiderowcolors 命令,直到表格的结尾。可以通过 \showrowcolors 命令重新启用高亮。

单个单元格的颜色

如上所述,使用 xcolor 包:

\usepackage[table]{xcolor}
\cellcolor[gray]{0.9}  % 为单元格设置灰色背景
\cellcolor{red}        % 设置单元格为红色

表格的宽度和伸缩

tabular* 环境

tabular*tabular 环境的一个扩展,它需要额外的参数(在列描述符之前)来指定表格的首选宽度。

\begin{tabular*}{0.75\textwidth}{ | c | c | c | r | }
  \hline
  label 1 & label 2 & label 3 & label 4 \\
  \hline 
  item 1  & item 2  & item 3  & item 4  \\
  \hline
\end{tabular*}

然而,表格的列可能会显示为其自然宽度。为了确保列均匀分布,可以使用如下方法:

\begin{tabular*}{0.75\textwidth}{@{\extracolsep{\fill} } | c | c | c | r | }
  \hline
  label 1 & label 2 & label 3 & label 4 \\
  \hline 
  item 1  & item 2  & item 3  & item 4  \\
  \hline
\end{tabular*}

tabularx

tabularx 提供了一种新型的表格环境,它与 tabular* 类似,但新增了 X 列描述符,能够自动调整列宽,使得表格宽度符合指定的总宽度,简化了表格的创建。

\usepackage{tabularx}
% ...

\begin{tabularx}{\textwidth}{ |X|X|X|X| }
  \hline
  label 1 & label 2 & label 3 & label 4 \\
  \hline 
  item 1  & item 2  & item 3  & item 4  \\
  \hline
\end{tabularx}

使用 \newcolumntype 格式化选定列

可以使用 \newcolumntype 定义新的列描述符,并以不同方式格式化选定的列。

\usepackage{tabularx}
% ...

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}%
\begin{tabularx}{\textwidth}{ |l|R|l|R| }
  \hline
  label 1 & label 2 & label 3 & label 4 \\
  \hline 
  item 1  & item 2  & item 3  & item 4  \\
  \hline
\end{tabularx}

跨列使用 \multicolumn

在使用 \multicolumn 时,可以结合 tabularx 环境实现跨列的效果。需要注意,multicolumn 的宽度应等于合并的列宽。

\usepackage{tabularx}
% ...

\begin{tabularx}{1\textwidth}{ |>{\setlength\hsize{1\hsize}\centering}X|>{\setlength\hsize{1\hsize}\raggedleft}X@{} >{\setlength\hsize{1\hsize}\raggedright}X|>{\setlength\hsize{1\hsize}\centering}X| }
  \hline
  Label 1 & \multicolumn{2}{>{\centering\setlength\hsize{2\hsize} }X|}{Label 2} & Label 3 \\
  \hline 
  123  & 123  & 456  & 123  \\
  \hline
  123  & 123  & 456  & 123  \\
  \hline
\end{tabularx}

创建带有参数的列类型

你可以创建带有参数的列类型,使用 \newcolumntype 来格式化列:

\usepackage{tabularx}
\usepackage[table]{xcolor}  % 用于为最后一列着色
% ...

\newcolumntype{L}[1]{>{\hsize=#1\hsize\raggedright\arraybackslash}X}%
\newcolumntype{R}[1]{>{\hsize=#1\hsize\raggedleft\arraybackslash}X}%
\newcolumntype{C}[2]{>{\hsize=#1\hsize\columncolor{#2}\centering\arraybackslash}X}%

\begin{tabularx}{\textwidth}{ | L{1} | R{0.5} | R{0.5} | C{2}{gray} | }
  \hline
  label 1 & label 2 & label 3 & label 4 \\
  \hline
  item 1  & item 2  & item 3  & item 4  \\
  \hline
\end{tabularx}

以上代码展示了如何创建不同类型的列,包括对齐、颜色和大小设置。

tabulary

tabulary 是一个修改过的 tabular* 环境,它允许为具有相同高度的列设置宽度。tabulary 使得写平衡良好的表格更加容易和方便。

tabularx 的问题在于,如果单元格几乎为空,它会留下许多空白。此外,它不容易实现不同的列宽。

tabulary 尝试平衡列宽,使每列至少具有其自然宽度,而不会超过最大长度

\usepackage{tabulary}
...

\begin{center}
  \begin{tabulary}{0.7\textwidth}{LCL}
    Short sentences      & \#  & Long sentences                                                 \\
    \hline
    This is short.       & 173 & This is much loooooooonger, because there are many more words.  \\
    This is not shorter. & 317 & This is still loooooooonger, because there are many more words. \\
  \end{tabulary}  
\end{center}

第一个参数是最大宽度。tabulary 会尽量不超过此宽度,但如果内容不足,它不会将列拉伸到最大宽度,这与 tabularx 不同。

第二个参数是列的布局。可能的值包括 tabular 环境中的那些,外加:

  • L:左对齐的平衡列

  • C:居中对齐的平衡列

  • R:右对齐的平衡列

  • J:左右对齐的平衡列

这些都是大写字母。

tabu 环境

tabu 环境的工作方式与 tabularx 类似。

注意:tabu 当前已损坏且不再维护

\begin{tabu} to \linewidth {llX[2]lllXl}
% ...
\end{tabu}

to \linewidth 指定目标宽度。X 参数可以有一个可选的跨度因子。

跨页的表格

长表格由 LaTeX 的 longtable 环境原生支持,它替代了 tabulartable 环境,或者将它们合并为一个环境。不幸的是,这个环境不支持拉伸(即 X 列)。

tabu 包(目前已不再维护)提供了 longtabu 环境。它具有 tabu 的大部分功能,并且能够跨越多页。

LaTeX 可以很好地处理长表格:您可以指定一个将在每页重复的表头、仅出现在第一页的表头,以及相同的页脚。

\begin{longtabu} to \linewidth {lX[2]lXl}
\rowfont\bfseries H1 & H2 & H3 & H4 & H5 \\ \hline 
\endhead
\\ \hline
\multicolumn{5}{r}{There is more to come} \\
\endfoot
\\ \hline
\endlastfoot
% Content ...

它使用与 longtable 相似的语法,如果您想了解更多,建议查看其文档。

您也可以尝试以下包之一:supertabularxtab,后者是 supertabular 的扩展和改进版本。

部分垂直线

为单个单元格添加部分垂直线:

\begin{tabular}{ l c r }
  \hline
  1 & 2 & 3 \\ \hline
  4 & 5 & \multicolumn{1}{r|}{6}  \\ \hline
  7 & 8 & 9 \\ \hline
\end{tabular}

在特定单元格中删除部分垂直线:

\begin{tabular}{ | l | c | r | }
  \hline
  1 & 2 & 3 \\ \hline
  4 & 5 & \multicolumn{1}{r}{6} \\ \hline
  7 & 8 & 9 \\ \hline
\end{tabular}

垂直居中的图像

插入图像到表格行时,它会对齐到单元格的顶部。通过使用 array 包,可以解决这个问题。定义新的列类型可以使图像垂直居中。

\newcolumntype{V}{>{\centering\arraybackslash} m{.4\linewidth} }

或者使用 parbox 来居中图像。

\parbox[c]{1em}{\includegraphics{image.png} }

raisebox 也可以工作,并允许您使用第一个参数手动微调对齐。

\raisebox{-.5\height}{\includegraphics{image.png} }

表格中的脚注

tabular 环境无法正确处理脚注。longtable 可以修复这个问题。

专业表格

许多专业排版的书籍和期刊都具有简单的表格,上下有适当的间距,并且几乎从不使用垂直线。许多 LaTeX 表格示例(包括本书)展示了垂直线(使用 "|")和双线(使用 \hline\hline 或 "||"),而这些在专业出版物中通常被认为是不必要且分散注意力的。

booktabs 包对于在 LaTeX 表格中轻松提供这种专业感非常有用,其文档也提供了关于什么构成 "良好" 表格的指导。

简而言之,包使用 \toprule 为最上面的线,\midrule 为表格中间的线(如标题下方),bottomrule 为最下面的线。这样可以确保线条的粗细和间距符合要求。此外,\cmidrule 可以用于跨越指定列的中间线。以下示例对比了使用 booktabs 和两个等效的标准 LaTeX 实现(第二个示例需要在导言区使用 \usepackage{array}\usepackage{dcolumn},第三个示例需要在导言区使用 \usepackage{booktabs})。

常规 LaTeX 表格

\begin{tabular}{llr}
\hline
\multicolumn{2}{c}{Item} \\
\cline{1-2}
Animal    & Description & Price (\$) \\
\hline
Gnat      & per gram    & 13.65      \\
          & each        & 0.01       \\
Gnu       & stuffed     & 92.50      \\
Emu       & stuffed     & 33.33      \\
Armadillo & frozen      & 8.99       \\
\hline
\end{tabular}

使用 array 包

\usepackage{array} 
% 或 \usepackage{dcolumn}
...
\begin{tabular}{llr}
\firsthline
\multicolumn{2}{c}{Item} \\
\cline{1-2}
Animal    & Description & Price (\$) \\
\hline
Gnat      & per gram    & 13.65      \\
          & each        & 0.01       \\
Gnu       & stuffed     & 92.50      \\
Emu       & stuffed     & 33.33      \\
Armadillo & frozen      & 8.99       \\
\lasthline
\end{tabular}

使用 booktabs 包

\usepackage{booktabs}
\begin{tabular}{lllr}  
\toprule
\multicolumn{2}{c}{Item} \\
\cmidrule(r){1-2}
Animal    & Description & Price (\$) & 2\\
\midrule
Gnu       & stuffed     & 92.50     & 2 \\
Emu       & stuffed     & 33.33    & 2  \\
Armadillo & frozen      & 8.99     & 2  \\
\bottomrule
\end{tabular}

表格脚注

通常需要在表格下方而不是页面底部添加脚注,且表格上方需要适当的标题。ctable 包可以解决这个问题。它提供了一个短标题选项,插入到表格列表中,而不是实际的标题(后者可能非常长且不适合出现在表格列表中)。ctable 使用 booktabs 包。

旋转表格

表格也可以在文档中旋转,使用 rotatingrotfloat 包。请参阅《旋转章节》。

表格带图例

要在表格中添加图例,可以使用 caption 包。使用该包,除了常规的 \caption{...} 外,还可以添加 \caption*{...}。示例如下:

\begin{table}
  \begin{tabular}{| r | r || c | c | c |}
      ...
  \end{tabular}
  \caption{A normal caption}
  \caption*{
    A legend, even a table can be used
    \begin{tabular}{l l}
      item 1 & explanation 1 \\
    \end{tabular}
  }
\end{table}

常规标题用于标签和引用。

eqparbox 包

在某些情况下,可能需要将表格中的每一行拉伸到其最长行的自然宽度。例如,当有两种语言的相同文本并希望将它们并排展示且行对齐时,可以使用 eqparbox 包提供的命令 \eqmakebox,其行为类似于 \makebox,但是它使用标签而不是宽度参数。

\newsavebox{\tstretchbox}
\newcolumntype{S}[1]{%
  >{\begin{lrbox}{\tstretchbox} }%
  l%
  <{\end{lrbox}%
  \eqmakebox[#1][s]{\unhbox\tstretchbox} }%
}

更多细节,请参见 eqparbox 包的文档。

paracol 包

LaTeX 提供的各种表格环境功能丰富,但它们缺乏自动分页的能力,特别是在长行的情况下。paracol 包可以在行之间自动分页,并在某些情况下可以替代 tabular 环境。这类情况常见于需要翻译和定义的文档,可能还包括列表。

使用 table 环境进行浮动

在 WYSIWYG 文档处理器中,通常会将表格放置在文本中间。这就是我们到目前为止一直在做的事。然而,专业文档通常会将表格放在专用页面上,以免打乱文本流。LaTeX 可以通过抽象化表格等对象来自动执行此任务,帮助我们决定它们在哪里最合适地放置。这种抽象被称为浮动。

要告诉 LaTeX 使用表格作为浮动体,需要将 table 环境放在 tabular 环境周围,这样它就能浮动并添加标签和标题。

\begin{table}[position specifier]
  \centering
  \begin{tabular}{|l|}
    ... your table ...
  \end{tabular}
  \caption{This table shows some data}
  \label{tab:myfirsttable}
\end{table}

table 环境中使用标签后,可以像引用其他对象一样引用它:

\ref{tab:myfirsttable}

表格环境

table 环境在你希望在文档的开始或结尾有一个表格列表时非常有用,可以使用命令:

\listoftables

此时,标题将显示在表格列表中(如果显示的话)。

你可以设置可选的参数位置说明符来定义表格的放置位置。以下是所有可能的放置方式。使用这些字符的组合可以向 LaTeX 定义你的“愿望清单”:

  • h:在声明表格的位置(这里)

  • t:在页面顶部

  • b:在页面底部

  • p:在专门的浮动页上

  • !:覆盖默认的浮动限制。例如,b 浮动的最大大小通常比较小;如果你需要一个较大的浮动体,就需要这个 ! 参数。

默认值是 tbp,这意味着它默认会被放置在页面顶部。如果不能放在顶部,它会尽量放在底部,或者最终放在一个单独的页面上,与其他浮动环境一起。

你可以强制 LaTeX 使用一个特定的放置位置。例如,[!h] 强制 LaTeX 将其放置在你指定的位置(除非真的不可能,例如你将表格放在这里,而这个地方恰好是页面的最后一行)。再次理解这一点:它强迫 LaTeX 将表格放置在一个特定的位置,但如果 LaTeX 认为那样放不合适,它不会放在那里。如果你真的想手动放置表格,不要使用 table 环境。

水平居中表格

与其他元素一样,水平居中表格也可以使用 \centering 命令,在打开 table 环境后使用,或者将其放入 center 环境中。

使用电子表格和数据分析工具

对于复杂或动态的表格,你可能希望使用电子表格。通过使用专门的软件来构建表格并将其导出为 LaTeX 格式,可以节省大量时间。以下是一些流行软件的插件和库:

  • calc2latex:适用于 LibreOffice 和 OpenOffice.org Calc 电子表格

  • excel2latex:适用于 Microsoft Office Excel

  • matrix2latex:适用于 Python 和 MATLAB

  • pandas:pandas DataFrame 提供一个方法将其包含的数据转换为 LaTeX

  • latex-tools:一个 Ruby 库

  • xtable:适用于 R 的库

  • org-mode:适用于 Emacs 用户,org-mode 表格可以直接嵌入到 LaTeX 文档中

  • Emacs 对齐命令:可以清理凌乱的 LaTeX 表格

  • LATeX 在线表格生成器:一个在线工具,可以在浏览器中创建简单的表格,并直接生成 LaTeX 格式。

  • 在线创建 LaTeX 表格:在线工具。

然而,将生成的源代码复制到文档中并不方便。为了最大灵活性,生成源代码到单独的文件,并通过 \input 命令从主文档中引入该文件。如果你的电子表格支持命令行操作,你可以通过一个 Makefile 在一个命令中生成完整的文档(包括表格)。

更多功能

查看以下包以获得更多功能:

  • hhline:自定义水平线

  • array:提供更多自定义列定义的自由

  • colortbl:让表格更有色彩

  • threeparttable:支持表格内和标题下的脚注

  • arydshln:创建虚线水平和垂直线

  • ctable:允许在表格下方添加脚注,并在上方提供适当的标题间距(集成了 booktabs 包)

  • slashbox:创建二维表格,第一格包含两个轴的描述(Tex Live 2011 或之后版本中不可用)

  • diagbox:与 slashbox 兼容,适用于 Tex Live 2011 或之后版本

  • dcolumn:对数字单元格进行小数点对齐

  • rccol:具有四舍五入的高级小数点对齐功能

  • numprint:打印数字,支持当前模式(文本或数学),自动使用正确的字体、分隔符、指数和/或四舍五入到指定的数字。

  • spreadtab:支持公式的电子表格

  • siunitx:对表格条目进行对齐

  • pgfplotstable:加载、四舍五入、格式化和后处理数值表格,例如通过直接从 .csv 文件中导入数据,而不是手动编写整个 LaTeX 表格。


Last modified: Sunday, 20 April 2025, 11:27 AM