接上一篇 LaTeX 系列(3) —— Figure,这篇文章介绍Latex中的各种Table样式和排版的实现。
1. 导入所需的包
全部导入就完事了:
\usepackage{booktabs} % toprule, midrule, bottomrule
\usepackage{multirow} % multirow, multicolumn
\usepackage{graphicx} % resizebox
\usepackage{threeparttable} % footnote
\usepackage{makecell} % makecell
2. 不同表格样式
Latex代码:
% Table 1
\begin{table}[htbp]
\centering
\caption{Caption}
\label{tab:my_label}
\begin{tabular}{|c | ccccc|}
\hline
Letter & A & B & C & D & E\\
\hline
Number & 1 & 2 & 3 & 4 & 5\\
\hline
\end{tabular}
\end{table}
% Table 2
\begin{table}[htbp]
\centering
\caption{Caption}
\label{tab:my_label}
\begin{tabular}{c | ccccc}
\hline\hline
Letter & A & B & C & D & E\\
\hline
Number & 1 & 2 & 3 & 4 & 5\\
\hline\hline
\end{tabular}
\end{table}
% Table 3
\begin{table}[htbp]
\centering
\caption{Caption}
\label{tab:my_label}
\begin{tabular}{c ccccc}
\toprule
Letter & A & B & C & D & E\\
\midrule
Number & 1 & 2 & 3 & 4 & 5\\
\bottomrule
\end{tabular}
\end{table}
效果:
3. 表格尺寸调整
Latex代码:
\begin{table}[htbp]
\centering
\caption{Caption}
\label{tab:my_label}
\setlength{\tabcolsep}{5pt} % 列间隔
\renewcommand{\arraystretch}{1.0} % 行间隔
\resizebox{\textwidth}{!}{ % 调整整个表格的尺寸
\begin{tabular}{|c ccccc|}
\hline
Letter & A & B & C & D & E\\
\hline
Number & 1 & 2 & 3 & 4 & 5\\
\hline
\end{tabular}
}
\end{table}
效果:
4. 多行合并,多列合并
Latex代码:
\begin{table}[htbp]
\centering
\caption{Caption}
\label{tab:my_label}
\begin{tabular}{|c | ccccc|}
\hline
\multirow{2}{*}{Letter} & A & B & C & D & E\\
~ & F & G & H & I & J\\
\hline
\end{tabular}
\end{table}
\begin{table}[htbp]
\centering
\caption{Caption}
\label{tab:my_label}
\begin{tabular}{|ccccc | ccccc|}
\hline
\multicolumn{5}{|c|}{Letter} & \multicolumn{5}{c|}{Number}\\
\hline
A & B & C & D & E & 1 & 2 & 3 & 4 & 5\\
\hline
\end{tabular}
\end{table}
效果:
5. 表格并排
Latex代码:
\begin{minipage}{0.5\textwidth}
\centering
\captionsetup{type=table}
\caption{Caption}
\begin{tabular}{|c | ccccc|}
\hline
Letter & A & B & C & D & E\\
\hline
Number & 1 & 2 & 3 & 4 & 5\\
\hline
\end{tabular}
~
\end{minipage}
\begin{minipage}{0.5\textwidth}
\centering
\captionsetup{type=table}
\caption{Caption}
\begin{tabular}{|c | ccccc|}
\hline
Letter & A & B & C & D & E\\
\hline
Number & 1 & 2 & 3 & 4 & 5\\
\hline
\end{tabular}
\end{minipage}
效果:
6. 特殊操作
Latex代码:
% 脚注
\begin{table}
\setlength{\belowcaptionskip}{2mm}
% \setlength{\abovecaptionskip}{2mm}
\centering
\caption{Table with footnotes after the table}
\begin{threeparttable}
\begin{tabular}{|c | ccccc|}
\hline
Letter\tnote{$\dagger$} & A & B & C & D & E\\
\hline
Number & 1 & 2 & 3 & 4 & 5\\
\hline
\end{tabular}
\begin{tablenotes}
\item[$\dagger$] The letters are capital.
\end{tablenotes}
\end{threeparttable}
\end{table}
% 单元格换行
\begin{table}[htbp]
\centering
\caption{Caption}
\label{tab:my_label}
\begin{tabular}{|c | ccccc|}
\hline
\multirow{5}{*}{\makecell[c]{Capital\\Letters}} & A & B & C & D & E\\
~ & A & B & C & D & E\\
~ & A & B & C & D & E\\
~ & A & B & C & D & E\\
~ & A & B & C & D & E\\
\hline
\end{tabular}
\end{table}
效果: