LaTeX使用笔记:长表格longtable(附实例)


转载于此:http://sparkandshine.net/latex-use-notes-longtable-with-examples/#1

本文记录我在插入长表格遇到的一些问题及解决方法,包括重复表头、表尾(纵向显示),调整表格适应页面宽度(横向显示)。

 

1. 一个简单实例

最简单的longtable使用跟table一样,下面给出一个简单的实例。

  1. \documentclass[twoside,12pt]{article}
  2. \usepackage{longtable}
  3.  
  4. \begin{document}
  5.  
  6. % An illustration of longtable
  7. \begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
  8. \caption{caption}
  9. \label{table:label} \\ % add \\ command to tell LaTeX to start a new line
  10. \hline
  11. line1 & line2 & $t_1$ & $t_{12}$ & $t_2$ & $r$(\%)& $D$(GB)& $D_{nc}(GB)$&$G_t$(\%)&Station\\
  12. \hline
  13. % data begins here
  14. 10 & 2 & 0:22:00 & 9:46:00 & 2:00:00 & 80.49 & 159.18 & 302.25 & 89.88 & Cours Dillon \\
  15. 204 & 205 & 2:01:00 & 2:57:00 & 1:11:00 & 47.97 & 95.21 & 138.43 & 45.38 & Ayguevives Collège \\
  16. % more data here
  17. \hline
  18. \end{longtable}
  19.  
  20. \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)。

  1. \documentclass[twoside,12pt]{article}
  2. \usepackage{longtable}
  3.  
  4. \begin{document}
  5.  
  6. \begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
  7. \caption{caption}
  8. \label{table:label} \\ % add \\ command to tell LaTeX to start a new line
  9.  
  10. % Appear table header at the first page as well
  11. \hline
  12. line1 & line2 & $t_1$ & $t_{12}$ & $t_2$ & $r$(\%)& $D$(GB)& $D_{nc}(GB)$&$G_t$(\%)&Station\\
  13. \hline
  14. \endfirsthead
  15.  
  16. % Appear the table header at the top of every page
  17. \hline
  18. line1 & line2 & $t_1$ & $t_{12}$ & $t_2$ & $r$(\%)& $D$(GB)& $D_{nc}(GB)$&$G_t$(\%)&Station\\
  19. \hline
  20. \endhead
  21.  
  22. % Appear \hline at the bottom of every page
  23. \hline
  24. \endfoot
  25.  
  26. % data begins here
  27. 10 & 2 & 0:22:00 & 9:46:00 & 2:00:00 & 80.49 & 159.18 & 302.25 & 89.88 & Cours Dillon \\
  28. 204 & 205 & 2:01:00 & 2:57:00 & 1:11:00 & 47.97 & 95.21 & 138.43 & 45.38 & Ayguevives Collège \\
  29. % more data here
  30. \hline
  31. \end{longtable}
  32.  
  33. \end{document}

3. 适应页面宽度

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

3.1 改变字体大小

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

  1. \begin{document}
  2.  
  3. % Temporarily change the font size
  4. {
  5. \small
  6. \tiny
  7.  
  8. \begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
  9. ...
  10. \end{longtable}
  11.  
  12. } % End of changing the font size
  13.  
  14. \end{document}

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

  1. \begin{document}
  2.  
  3. \begin{footnotesize}
  4. \begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
  5. ...
  6. \end{longtable}
  7. \end{footnotesize}
  8.  
  9. \end{document}

3.2 缩小列间的间距

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

  1. \begin{document}
  2.  
  3. % Change the intercolumn space
  4. \setlength{\tabcolsep}{2pt}
  5.  
  6. \begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
  7. ...
  8. \end{longtable}
  9.  
  10. \end{document}

3.3 调整表格边缘

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

  1. \begin{document}
  2.  
  3. % Adjust margins
  4. \setlength\LTleft{-1in}
  5. \setlength\LTright{-1in plus 1 fill}
  6.  
  7. \begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
  8. ...
  9. \end{longtable}
  10.  
  11. \end{document}

3.4 多行显示

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

  1. \documentclass[twoside,12pt]{article}
  2. \usepackage{longtable}
  3. \usepackage{tabu}
  4.  
  5. \begin{document}
  6.  
  7. % Use longtabu
  8. \begin{longtabu} to \textwidth {|X|X|X|X|X|X|X|X|X|X|}
  9. ...
  10. \end{longtabu}
  11.  
  12. \end{document}

3.5 纵向显示

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

  1. \documentclass[twoside,12pt]{article}
  2. \usepackage{longtable}
  3. \usepackage{lscape} % for landscape
  4.  
  5. \begin{document}
  6.  
  7. \begin{landscape}
  8. \begin{longtable}{|c|c|r|r|r|r|r|r|r|l|}
  9. ...
  10. \end{longtable}
  11. \end{landscape}
  12.  
  13. \end{document}

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

有了这些,应该能调成自己满意的格式:-) 完整的例子,我已分享到我的GitHub,在这里longtable.tex

References:
[1] The longtable package – CTeX.pdf
[2] TeX – LaTeX Stack Exchange: longtable: hline doesn’t display on the first page
[3] TeX – LaTeX Stack Exchange: How to set width of longtable


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM