1. 简介

在本教程中,我们将介绍如何在 LaTeX 中绘制横向纵向表格。

LaTeX 提供了强大的表格排版能力,尤其适合学术论文、技术文档等对格式要求较高的场景。掌握其基本语法和一些高级技巧,能显著提升文档的可读性和专业性。


2. 基础 LaTeX 表格

LaTeX 中使用 tabular 环境来创建表格:

\begin{tabular}{c c c}
 cell1 & cell2 & cell3 \\ 
 cell4 & cell5 & cell6 \\
 cell7 & cell8 & cell9 \\
\end{tabular}

Rendered by QuickLaTeX.com

  • {c c c} 表示有三列,内容居中对齐
  • & 分隔单元格
  • \\ 表示换行(即新一行)

提示:可以将 c 替换为 l(左对齐)或 r(右对齐)

添加边框

\begin{tabular}{|c|c|c|}
 \hline 
 cell1 & cell2 & cell3 \\ 
 \hline
 cell4 & cell5 & cell6 \\
 \hline
 cell7 & cell8 & cell9 \\
 \hline
\end{tabular}

Rendered by QuickLaTeX.com

  • | 表示垂直边框
  • \hline 插入水平边框线

3. 表格的定位与标题

使用 table 环境可以控制表格的位置并添加标题:

\begin{table}[h]
\centering
\caption{LaTeX 表格标题}
\begin{tabular}{|c|c|c|}
 \hline 
 cell1 & cell2 & cell3 \\ 
 \hline
 cell4 & cell5 & cell6 \\
 \hline
 cell7 & cell8 & cell9 \\
 \hline
\end{tabular}
\end{table}

Rendered by QuickLaTeX.com

  • [h]:尽量放在当前位置(here)
  • [t]:放在页面顶部(top)
  • [b]:放在页面底部(bottom)
  • \centering:居中显示
  • \caption{}:添加标题

4. 使用 booktabs 美化表格

专业风格的表格通常使用三条加粗的水平线(顶部、中部、底部),这可以通过 booktabs 包实现:

\usepackage{booktabs}

\begin{table}[h]
\centering
\begin{tabular}{ccc}
\toprule
\textbf{Topic 1} & \textbf{Topic 2} & \textbf{Topic 3} \\
\midrule
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\bottomrule
\end{tabular}
\end{table}

Rendered by QuickLaTeX.com

  • \toprule:顶部加粗线
  • \midrule:中间分隔线
  • \bottomrule:底部加粗线

跨列线(cmidrule)

\usepackage{booktabs}

\begin{table}[h]
\centering
\begin{tabular}{cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
row1 & A.1 & B.1 & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
row3 & A.3 & B.3 & C.3 & D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}

Rendered by QuickLaTeX.com

  • \cmidrule(rl){2-4}:从第2到第4列画一条线,左右留白

5. 表格颜色设置

使用 xcolorcolortbl 可以给表格添加颜色:

\usepackage{booktabs}
\usepackage{xcolor,colortbl}
\definecolor{lavender}{rgb}{0.9, 0.9, 0.98}

\begin{table}[h]
\centering
\begin{tabular}{cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
\rowcolor{lavender}
row1 & A.1 & B.1 & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
\rowcolor{lavender}
row3 & A.3 & B.3 & C.3 & D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
\rowcolor{lavender}
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}

Rendered by QuickLaTeX.com

  • \definecolor{lavender}{rgb}{0.9, 0.9, 0.98}:定义颜色
  • \rowcolor{}:整行背景色
  • \columncolor{}:整列背景色
  • \cellcolor{}:单个单元格背景色
  • \textcolor{}:文字颜色

6. 纵向表格(Landscape)

当表格列数较多时,使用纵向排版更合适。可以通过 lscape 包实现:

\usepackage{lscape}

\begin{landscape}
\begin{table}[h]
\centering
\begin{tabular}{cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
\rowcolor{lavender}
row1 & A.1 & B.1 & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
\rowcolor{lavender}
row3 & A.3 & B.3 & C.3 & D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
\rowcolor{lavender}
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}
\end{landscape}

Rendered by QuickLaTeX.com

  • \begin{landscape} ... \end{landscape}:将表格区域旋转为横向
  • 适用于列多行少的表格

7. 小结

本文介绍了在 LaTeX 中绘制表格的基本方法:

  • 使用 tabular 创建基础表格
  • 使用 table 环境设置位置与标题
  • 使用 booktabs 实现专业风格的三线表
  • 使用 xcolorcolortbl 设置颜色
  • 使用 lscape 实现纵向表格排版

掌握了这些技巧,你就可以在撰写技术文档、论文或报告时,灵活地控制表格的样式与布局,提升文档的专业度和可读性 ✅


作者邮箱latex@example.com
最后更新:2025-04-05


原始标题:Vertical and Horizontal Tables in LaTeX