latex 表格相关语法(全集)


基本表格

  1. 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}
    

表格样式

  1. booktabs
    使用 booktabs 包可以创建更美观的表格:

    \usepackage{booktabs}
    
    \begin{tabular}{ccc}
    \toprule
    Item & Quantity & Price \\
    \midrule
    Apple & 10 & 1.50 \\
    Banana & 5 & 0.75 \\
    \bottomrule
    \end{tabular}
    
  2. 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}
    

跨行和跨列

  1. 跨列
    使用 \multicolumn 命令实现跨列:

    \begin{tabular}{|l|c|r|}
    \hline
    \multicolumn{2}{|c|}{Item} & Price \\
    \hline
    Apple & 10 & 1.50 \\
    Banana & 5 & 0.75 \\
    \hline
    \end{tabular}
    
  2. 跨行
    使用 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}
    

表格环境

  1. 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}
    

复杂表格

  1. 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}
    
  2. tabularx
    使用 tabularx 包可以创建自动调整列宽的表格:

    \usepackage{tabularx}
    
    \begin{tabularx}{\textwidth}{Xcc}
    \hline
    Item & Quantity & Price \\
    \hline
    Apple & 10 & 1.50 \\
    Banana & 5 & 0.75 \\
    \hline
    \end{tabularx}
    

  1. tabularx

    tabularx 包允许自动调整列宽以适应页面宽度。

    \usepackage{tabularx}
    
    \begin{tabularx}{\textwidth}{Xcc}
    \hline
    Item & Quantity & Price \\
    \hline
    Apple & 10 & 1.50 \\
    Banana & 5 & 0.75 \\
    \hline
    \end{tabularx}
    
  2. 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}
    
  3. 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}
    
  4. 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}
    
  5. 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}
    

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司南锤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值