跨页表格制作 longtable和 supertabular 示例 您所在的位置:网站首页 word表格跨页设置表头和表尾 跨页表格制作 longtable和 supertabular 示例

跨页表格制作 longtable和 supertabular 示例

2024-05-21 03:09| 来源: 网络整理| 查看: 265

一篇paper中难免会遇到各种各样的问题(包括表格、图片、公式等等)需要处理,最终要想达到我们要的效果还得经历多次的调整。在这个系列的博客中,我会将我遇到的一些比较棘手或是比较常用到的问题整合起来,分享给大家。

1 问题描述

有时候由于页面长度有限,所以表格不一定能完全在同一个页面中。在word中遇到这种情况时,word能够自动对表格进行跨页断行的操作(如下图所示)。image.png

但在latex中,一个超过页面长度的长表格,使用平常的begin{table}环境的话,就会出现如下情况,表格会一直延申到整个页面的底部,并且显示不全(如下图所示)。

image.png

2 解决办法2.1 longtable

首先需要加上宏包\usepackage{longtable}

2.1.1 简单例子

第一种写法是直接将表格内容放置在longtable环境中,代码及效果图如下所示:

\begin{longtable}{cccc} \caption{example}\label{tab1}\\ % 表格内容 \end{longtable}

image.png

可以很明显的看到,用longtable的方法,表格会自动换页,不会出现文章一开始的那种情况。虽然满足了自动换页这一点要求,但是通常一张跨页的表格需要在每一页的开头重复表头,所以我们来做一下改进。

2.1.2 加表头表尾

第二种写法也很简单,只是在第一种写法的基础上,在表格内容的前面加上一些表头的修饰操作。代码及效果图如下所示。

\begin{longtable}{cccccccc} \caption{example}\label{tab1}\\ % 表格“首页”显示内容 \toprule n1 & n2 & n3 & n4 & n1 & n2 & n3 & n4 \\ \midrule \endfirsthead % “后续页面”表头显示内容 \multicolumn{8}{r}{Continued}\\ \toprule n1 & n2 & n3 & n4 & n1 & n2 & n3 & n4 \\ %\midrule \endhead % 表格“尾页前”,表格最后显示内容 %\bottomrule \multicolumn{8}{c}{Continued on next page}\\ \endfoot % 表格“尾页”,表格最后显示内容 \bottomrule \endlastfoot \multicolumn{3}{c}{a} &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \hline 1 & 2 & 3 &4 & 1 & 2 & 3 &4\\ \hline a & b & c &d & a & b & c &d\\ \end{longtable}

image.png

\toprule、\midrule和\bottomrule为三线表booktabs的横线控制命令;\endfirsthead前为表格首页的表头;\endfirsthead和\endhead的中间部分为后续页面的表头内容;\endhead和\endfoot的中间部分为除表格尾页外的其它页的表尾;\endfoot到\endlastfoot部分是最后一页的表尾。2.1.3 使用longtable的注意事项不能用 twocolumn 双栏页面格式中longtable 环境不能放在 table 的浮动体内,否则只会在一页显示。\begin{table}[!htp] \caption{example}\label{tab1} \begin{longtable}{cccc} %表格内容 \end{longtable} \end{table}

如果我们如上面代码一样,把longtable环境不能放在table的浮动体内,看下面这张效果图你会发现,表格内容只显示一页,并且显示不完全。

image.png

2.2 supertabular(适用范围较广)2.2.1 英文说明书

这份说明书对supertabular环境做了非常详细的介绍,包括每个关键字的说明,和需要注意的地方,语言简练易懂。

需要的可以点下面这个连接下载下来:http://mirrors.ctan.org/macros/latex/contrib/supertabular/supertabular.pdf

2.2.2 基本框架

我们先按照说明书中的例子来讲解一下基本框架首先要有宏包\usepackage{supertabular}

\begin{center} \tablefirsthead{% %首页表头 \hline \multicolumn{1}{|c}{\tbsp Number} & \multicolumn{1}{c}{Number$^2$} & Number$^4$ & \multicolumn{1}{c|}{Number!} \\ \hline} \tablehead{% %后续页表头 \hline \multicolumn{4}{|l|}{\small\sl continued from previous page}\\ \hline \multicolumn{1}{|c}{\tbsp Number} & \multicolumn{1}{c}{Number$^2$} & Number$^4$ & \multicolumn{1}{c|}{Number!} \\ \hline} \tabletail{%除最后一页外的表尾 \hline \multicolumn{4}{|r|}{\small\sl continued on next page}\\ \hline} \tablelasttail{\hline} %最后一页的表尾 \bottomcaption{This table is split across pages} \begin{supertabular}{|r@{\hspace{6.5mm}}|r@{\hspace{5.5mm}}|r|r|} %表格内容 \end{supertabular} \end{center}

其实supertabular与longtable是大同小异的,同样由表头表尾及中间的内容来构成一张表格。其中:

\tablefirsthead为表格首页的表头;\tablehead为后续页面的表头内容;\tabletail为除表格尾页外的其它页的表尾;\tablelasttail为最后一页的表尾。

其他部分如果有看不懂的地方,可以参考一下这篇博客十秒完成你要的latex表格:超强工具分享+表格创建+合并行列+布局调整

2.2.3 简单例子

清楚了基本框架后,我们可以来看看说明书的例子,学会了简单例子之后,我会放上我的一个实例来一起学习一下。

image.png

表尾也是同样的道理,这里就不再赘述。

2.2.3 我的一个实例\begin{center} \tablefirsthead{% \multicolumn{2}{l}{\textbf{Table 6}}\\ \multicolumn{2}{l}{Passengers demand in small case}\\ \hline} \tablehead{% \hline \multicolumn{2}{l}{\small\sl continued from previous page}\\ \hline} \tabletail{% \hline \multicolumn{2}{r}{\small\sl continued on next page}\\ \hline} \tablelasttail{\hline} \begin{supertabular}{l|l} %表格内容 \end{supertabular} \end{center}

改动部分:其实可以发现,我们在制作长表格的时候,表头部分是可以让我们随机应变的,其他表尾的部分还是没有动。

image.png

如果说后续页面的表头你不想加continued from previous page这句话,那么代码中你只需要像最后一页的表尾那样,写个横线的命令就行。

\tablehead{\hline}

在做这个长表格的过程中,我发现{\tabincell{c}{内容 \\ 内容}}在我的表格中不适用,用了tabincell后有各种各样的问题。但是用不了tabincell,仍然需要换行的操作的话,我想到的办法是直接新建一行。(但是后来我用那个简单例子试验了一下,tabincell 是适用的,大概是因为我那个表格太多页,也太大了)————————————————

版权声明:本文为CSDN博主「妙妙喵mmm」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_42772652/article/details/122932889



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有