轉載於此:http://sparkandshine.net/latex-use-notes-longtable-with-examples/#1
本文記錄我在插入長表格遇到的一些問題及解決方法,包括重復表頭、表尾(縱向顯示),調整表格適應頁面寬度(橫向顯示)。
1. 一個簡單實例
最簡單的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 & $t_1$ & $t_{12}$ & $t_2$ & $r$(\%)& $D$(GB)& $D_{nc}(GB)$&$G_t$(\%)&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
- % Appear table header at the first page as well
- \hline
- line1 & line2 & $t_1$ & $t_{12}$ & $t_2$ & $r$(\%)& $D$(GB)& $D_{nc}(GB)$&$G_t$(\%)&Station\\
- \hline
- \endfirsthead
- % Appear the table header at the top of every page
- \hline
- line1 & line2 & $t_1$ & $t_{12}$ & $t_2$ & $r$(\%)& $D$(GB)& $D_{nc}(GB)$&$G_t$(\%)&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. 適應頁面寬度
上面的方法解決了表格縱向顯示問題。對於橫向,如果一行有太多數據,默認情況下表格會截斷超出的部分。解決方法無非是改變字體大小,縮小列間的間距,調整表格邊緣,多行顯示,縱向顯示。
3.1 改變字體大小
在表格開始前聲明字體大小,比如\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}
3.2 縮小列間的間距
默認情況下,表格單元左側和右側會有填充(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}
3.3 調整表格邊緣
減少表格的邊緣(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}
3.4 多行顯示
使用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}
3.5 縱向顯示
使用\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}
以上這些方法,有時候需要組合使用。
有了這些,應該能調成自己滿意的格式:-) 完整的例子,我已分享到我的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