LaTex表格--长表格和长宽调整

跨页的长表格

1. 长表格使用案例

写论文时当表格的行数很多,出现跨页时候,采用longtable环境。
最简单的longtable使用跟table一样,下面给出一个简单的实例。

\documentclass[twoside,12pt]{article}
\usepackage{longtable}
 
\begin{document}
 
% An illustration of longtable
\begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
\caption{caption}
\label{table:label} \\ % add \\ command to tell LaTeX to start a new line
\hline
line1 & line2 & t1


(\%)&Station\\
\hline
% data begins here
10 & 2 & 0:22:00 & 9:46:00 & 2:00:00 & 80.49 & 159.18 & 302.25 & 89.88 & Cours Dillon \\
204 & 205 & 2:01:00 & 2:57:00 & 1:11:00 & 47.97 & 95.21 & 138.43 & 45.38 & Ayguevives Collège \\
% more data here
\hline
\end{longtable}
 
\end{document}

注意事项:

  • 如果把表标题\caption{}或者标签\label{}放在前面,要在其后添加换行\,否则会报“! Misplaced \noalign.”错误。

  • 如果是表头单元的宽度与其他行不一致,多编译几次就行了,这是因为longtable为了节省内存和避免溢出采取分块处理表格带来的副作用,详情见TeX – LaTeX Stack Exchange: Bad width of head of longtable。

2. 细节设置

重复表头、表尾

长表格有时会跨越很多页,为了便于阅读,在每一页重复表头或者表尾,这涉及到4个命令,如下:

\endhead, specify rows (比如表头) to appear at the top of every page (under the headline, but before the other lines of the table)
\endfoot, specify rows (比如水平线\hline) to appear at the bottom of each page.
\endfirsthead,只作用于表格的第一页。
\endlastfoot,只作用于表格的第一页。

值得注意的是,这些命令需要放在表格开始处(at the start of the table)。以下是一个实例,每一页头部重复表头,每一页尾部重复水平线(\hline)。

\documentclass[twoside,12pt]{article}
\usepackage{longtable}
 
\begin{document}
 
\begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
\caption{caption}
\label{table:label} \\ % add \\ command to tell LaTeX to start a new line
\hline
line1 & line2 & t1& $t_{12}$ & t2 & r(\%)& D(GB)& $D_{nc}(GB)$&Gt(\%)&Station\\
\hline
\endhead
 
% Appear \hline at the bottom of every page
\hline
\endfoot
 
% data begins here
10 & 2 & 0:22:00 & 9:46:00 & 2:00:00 & 80.49 & 159.18 & 302.25 & 89.88 & Cours Dillon \\
204 & 205 & 2:01:00 & 2:57:00 & 1:11:00 & 47.97 & 95.21 & 138.43 & 45.38 & Ayguevives Collège \\
% more data here
\hline
\end{longtable}
 
\end{document}

3. 适应页面

上面的方法解决了表格纵向显示问题。对于横向,如果一行有太多数据,默认情况下表格会截断超出的部分。解决方法无非是改变字体大小,缩小列间的间距,调整表格边缘,多行显示,纵向显示。

改变字体大小

在表格开始前声明字体大小,比如\small或者\tiny,为了不影响表格后面的字体大小,用{}括起来,如下:

\begin{document}
 
% Temporarily change the font size
{
\small
\tiny
 
\begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
...
\end{longtable}
 
} % End of changing the font size
 
\end{document}

或者用\begin{footnotesize}…\end{footnotesize}括起来,

\begin{document}
 
\begin{footnotesize}
\begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
...
\end{longtable}
\end{footnotesize}
 
\end{document}

缩小列间的间距

默认情况下,表格单元左侧和右侧会有填充(padding),被定义为\tabcolsep,默认值为6pt。使用命令\setlength{\tabcolsep}{6pt}调整列间的间距。

\begin{document}
 
% Change the intercolumn space
\setlength{\tabcolsep}{2pt}
 
\begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
...
\end{longtable}
 
\end{document}

未使用\setlength{\tabcolsep},编译结果如图:在这里插入图片描述

以\setlength{\tabcolsep}{25pt}为例,编译结果如图:
在这里插入图片描述

调整表格边缘

减少表格的边缘(margins),这样就可以放入更多的内容。

\begin{document}
 
% Adjust margins
\setlength\LTleft{-1in}
\setlength\LTright{-1in plus 1 fill}
 
\begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
...
\end{longtable}
 
\end{document}

多行显示

使用longtabu(tabularx + longtable)将长文本在单元格多行显示。

\documentclass[twoside,12pt]{article}
\usepackage{longtable}
\usepackage{tabu}
 
\begin{document}
 
% Use longtabu
\begin{longtabu} to \textwidth {|X|X|X|X|X|X|X|X|X|X|}
...
\end{longtabu}
 
\end{document}

纵向显示

使用\begin{landscape}…\end{landscape}将表格纵向显示。

\documentclass[twoside,12pt]{article}
\usepackage{longtable}
\usepackage{lscape} % for landscape
 
\begin{document}
 
\begin{landscape}
\begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
...
\end{longtable}
\end{landscape}
 
\end{document}

以上这些方法,有时候需要组合使用。

  • 9
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值