1. 简介

在编写 LaTeX 文档时,表格是展示结构化数据的重要工具。但在处理复杂数据时,标准的表格结构往往无法满足需求,这时就需要对单元格进行合并或拆分。本文将介绍几种在 LaTeX 中拆分表格单元格的方法,帮助你构建更复杂的表格结构。

2. 使用 \multicolumn 命令拆分列

\multicolumn 是 LaTeX 中用于合并列的标准命令。其语法如下:

\multicolumn{numcols}{format}{text}

其中:

  • numcols:要合并的列数
  • format:对齐方式(c 居中、l 左对齐、r 右对齐),可加竖线 | 控制边框
  • text:合并后单元格中的内容

举个例子,我们想将“白猫”这一列进一步拆分为“波斯猫”和“暹罗猫”两列,可以这样写:

\begin{tabular}{|c|c|c|c|}\hline\hline
   \multicolumn{4}{|c|}{A Classification of Cats}\\\hline\hline
   \multicolumn{2}{|c|}{White Cats} & Black Cats & Pink Cats\\\hline
      Persian & Siamese & Halloween & Cartoon \\\cline{1-2}
      Round head & Slim body & Superstition & Panther\\\hline
\end{tabular}

这样就能在“White Cats”下拆分出两个子类,效果如下:

Table of Classification of cats using multicolumns

注意:使用 \multicolumn 需要预先知道表格的最大列数,并合理使用 \cline 来控制行线的显示范围。

3. 使用嵌套 tabular 环境实现更灵活拆分

对于更复杂的结构,可以使用嵌套的 tabular 环境。例如将“白猫”和“黑猫、粉猫”分别放在两个子表格中:

\begin{tabular}{|@{}c@{}|@{}c@{}|}\hline\hline
\multicolumn{2}{|c|}{A Classification of Cats}\\\hline\hline
    \begin{tabular}{c|c}
       \multicolumn{2}{c}{White Cats}\\\hline
       Persian & Siamese \\\hline
       Round Head & Slim Body \\\hline
    \end{tabular} &
    \begin{tabular}{c|c}
       Black Cats   & Pink Cats\\\hline
       Halloween    & Cartoon\\
       Superstition & Panther\\\hline
    \end{tabular}
\end{tabular}

效果如下:

Table of Classification of Cats using nested tabulars

⚠️ 注意@{} 用于去除嵌套表格中默认的左右间距,使得边框对齐更自然。

4. 对角线拆分单元格

有时候我们希望在一个单元格内使用对角线分割内容,比如绘制一个数学表格:

Illustration of a diagonal split in a cell of a table

可以使用 slashbox 宏包提供的 \backslashbox{LowerText}{UpperText} 命令实现:

\usepackage{slashbox}

\begin{tabular}{|c|c|c|c|c|c|}
  \hline
  \multicolumn{6}{|c|}{\rule{0pt}{10pt}}\\\hline
  \backslashbox{}{} & 0 & 1 & 2 & 3 & 4 \\
  \hline
  0 &  0 &  1 &  4 &  9 & 16 \\
  1 &  1 &  2 &  5 & 10 & 17 \\
  2 &  4 &  5 &  8 & 13 & 20 \\
  3 &  9 & 10 & 13 & 18 & 25 \\
  4 & 16 & 17 & 20 & 25 & 32 \\
  \hline
\end{tabular}

使用 \rule{0pt}{10pt} 是为了给对角线留出足够的空间。

⚠️ 注意slashbox 已被弃用,建议使用 diagbox 宏包替代,功能更强大。

5. 使用 \multirow 合并多行

当需要在垂直方向合并多个单元格时,可以使用 multirow 宏包中的 \multirow 命令。其语法如下:

\multirow[vert-pos]{num-rows}{width}[vert-adjust]{text}

参数说明:

  • vert-pos:垂直对齐方式(c, t, b
  • num-rows:跨行数
  • width:文本宽度,* 表示自动宽度
  • vert-adjust:垂直微调
  • text:显示的文本

举个复杂例子,展示动物分类:

\usepackage{multirow}
\usepackage{graphicx}
\usepackage{arydshln}

\begin{tabular}{|l:l|l|l|l|l:l|}
  \hline 
  \multicolumn{7}{|c|}{The Animal Kingdom}\\\hline
  \hline
  \multirow[c]{5}{*}[0in]{\rotatebox{90}{Arthropods}} &
  Archanids &
  \multirow[c]{5}{*}[0in]{\rotatebox{90}{Annelids}}&
  \multirow[c]{5}{*}[0in]{\rotatebox{90}{Molluscs}}&
  \multirow[c]{5}{*}[0in]{\rotatebox{90}{Nematodes}}&
  \multirow[c]{5}{*}[0in]{\rotatebox{90}{Vertebrates}}&
  Amphibians\\
  & Crustaceans & & & & &Birds\\
  & Insects     & & & & &Fish\\
  & Myriapods   & & & & &Mammals\\
  &             & & & & &Reptiles\\
  \hline 
\end{tabular}

效果如下:

Using multirow and multicolumn to split a table

技巧:结合 \rotatebox 可以节省水平空间,特别适合长字段。

6. 总结

本文介绍了几种在 LaTeX 中拆分表格单元格的方法:

方法 用途 特点
\multicolumn 横向合并列 简单易用,适合简单结构
嵌套 tabular 构建复杂结构 灵活但需注意格式对齐
\backslashbox / \diagbox 对角线分割 适用于二维索引表
\multirow 纵向合并行 适合长字段垂直居中

建议

  • 简单拆分优先使用 \multicolumn
  • 复杂结构使用嵌套 tabular
  • 对角线用 \diagbox
  • 多行合并用 \multirow

掌握这些方法后,你就能在 LaTeX 中自如地构建各种复杂表格了。


原始标题:Dividing Table Cells in LaTeX