基本表格
-
tabular
环境:
使用tabular
环境创建基本表格:\begin{tabular}{列格式} 内容 \end{tabular}
其中
列格式
可以是l
(左对齐)、c
(居中对齐)、r
(右对齐)、|
(竖线)等。示例:
\begin{tabular}{l|c|r} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular}
表格样式
-
booktabs
包:
使用booktabs
包可以创建更美观的表格:\usepackage{booktabs} \begin{tabular}{ccc} \toprule Item & Quantity & Price \\ \midrule Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \bottomrule \end{tabular}
-
array
包:
使用array
包可以自定义列格式:\usepackage{array} \begin{tabular}{>{\bfseries}l c >{\itshape}r} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular}
跨行和跨列
-
跨列:
使用\multicolumn
命令实现跨列:\begin{tabular}{|l|c|r|} \hline \multicolumn{2}{|c|}{Item} & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular}
-
跨行:
使用multirow
包实现跨行:\usepackage{multirow} \begin{tabular}{|l|c|r|} \hline Item & \multirow{2}{*}{Quantity} & \multirow{2}{*}{Price} \\ \cline{1-1} Apple & & \\ \hline Banana & 5 & 0.75 \\ \hline \end{tabular}
表格环境
table
环境:
使用table
环境可以使表格浮动并添加标题和标签:\begin{table}[htbp] \centering \begin{tabular}{ccc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \caption{This is a table} \label{tab:example} \end{table}
复杂表格
-
longtable
包:
使用longtable
包可以创建跨页表格:\usepackage{longtable} \begin{longtable}{ccc} \hline Item & Quantity & Price \\ \hline \endfirsthead \hline Item & Quantity & Price \\ \hline \endhead Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{longtable}
-
tabularx
包:
使用tabularx
包可以创建自动调整列宽的表格:\usepackage{tabularx} \begin{tabularx}{\textwidth}{Xcc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabularx}
-
tabularx
包tabularx
包允许自动调整列宽以适应页面宽度。\usepackage{tabularx} \begin{tabularx}{\textwidth}{Xcc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabularx}
-
makecell
包makecell
包允许在单元格中使用换行符。\usepackage{makecell} \begin{tabular}{ccc} \hline \makecell{Item \\ (Fruit)} & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular}
-
tabu
包tabu
包提供了更灵活的表格创建方式,支持多种列格式和自动调整列宽。\usepackage{tabu} \begin{tabu} to \textwidth {X[l] X[c] X[r]} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabu}
-
threeparttable
包threeparttable
包允许在表格下方添加脚注。\usepackage{threeparttable} \begin{threeparttable} \begin{tabular}{ccc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \begin{tablenotes} \small \item[*] Prices are in USD. \end{tablenotes} \end{threeparttable}
-
rotating
包rotating
包允许旋转表格。\usepackage{rotating} \begin{sidewaystable} \centering \begin{tabular}{ccc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \caption{Rotated Table} \label{tab:rotated} \end{sidewaystable}
示例代码
\documentclass{article} \usepackage{booktabs} \usepackage{array} \usepackage{multirow} \usepackage{longtable} \usepackage{tabularx} \usepackage{makecell} \usepackage{tabu} \usepackage{threeparttable} \usepackage{rotating} \begin{document} \begin{table}[htbp] \centering \begin{tabular}{l|c|r} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \caption{Basic Table} \label{tab:basic} \end{table} \begin{table}[htbp] \centering \begin{tabular}{ccc} \toprule Item & Quantity & Price \\ \midrule Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \bottomrule \end{tabular} \caption{Table with Booktabs} \label{tab:booktabs} \end{table} \begin{table}[htbp] \centering \begin{tabular}{>{\bfseries}l c >{\itshape}r} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \caption{Table with Array} \label{tab:array} \end{table} \begin{table}[htbp] \centering \begin{tabular}{|l|c|r|} \hline Item & \multirow{2}{*}{Quantity} & \multirow{2}{*}{Price} \\ \cline{1-1} Apple & & \\ \hline Banana & 5 & 0.75 \\ \hline \end{tabular} \caption{Table with Multirow} \label{tab:multirow} \end{table} \begin{longtable}{ccc} \hline Item & Quantity & Price \\ \hline \endfirsthead \hline Item & Quantity & Price \\ \hline \endhead Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{longtable} \caption{Long Table} \label{tab:longtable} \begin{tabularx}{\textwidth}{Xcc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabularx} \caption{Table with Tabularx} \label{tab:tabularx} \begin{table}[htbp] \centering \begin{tabular}{ccc} \hline \makecell{Item \\ (Fruit)} & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \caption{Table with Makecell} \label{tab:makecell} \end{table} \begin{table}[htbp] \centering \begin{tabu} to \textwidth {X[l] X[c] X[r]} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabu} \caption{Table with Tabu} \label{tab:tabu} \end{table} \begin{table}[htbp] \centering \begin{threeparttable} \begin{tabular}{ccc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \begin{tablenotes} \small \item[*] Prices are in USD. \end{tablenotes} \caption{Table with Threeparttable} \label{tab:threeparttable} \end{threeparttable} \end{table} \begin{sidewaystable} \centering \begin{tabular}{ccc} \hline Item & Quantity & Price \\ \hline Apple & 10 & 1.50 \\ Banana & 5 & 0.75 \\ \hline \end{tabular} \caption{Rotated Table} \label{tab:rotated} \end{sidewaystable} \end{document}