latex longtable and supertabular 跨页表格

本文详细介绍了如何在LaTeX中创建跨页长表格,包括使用longtable包的技巧,如重复表头、调整表格宽度、解决多列模式下的错误等。通过实际案例,展示了如何使长表格适应页面布局,确保在不同页面上的一致性和美观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1 Latex跨页表格

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

2.1. 一个简单实例

2.2. 重复表头、表尾

2.3. 适应页面宽度

2.3.1 改变字体大小

2.3.2 缩小列间的间距

2.3.3 调整表格边缘

2.3.4 多行显示

2.3.5 纵向显示

3 How to solve longtable is not in 1-column mode error?

3.1 longtable doesn't work well with twocolumn. But you can use supertabular. Here is an example:

3.2 As suggested by egreg, xtab is another choice. xtab is built on supertabular while avoiding its weaknesses.

3.3 If you still want to stick to longtable (e.g. because you are generating LaTeX through pandoc) you could redefine the \longtable command.

3.4 Another option, not covered in the previous answers, is to force single column mode along with a \clearpage before your longtable and then reset it to two column mode after. This would look like


1 Latex跨页表格

转载于 https://blog.csdn.net/lishoubox/article/details/7268858

\documentclass{article}

\usepackage{amsmath}

\usepackage{amssymb}

\usepackage{latexsym}

\usepackage{CJK}

\usepackage{longtable}

 

\begin{document}

\begin{CJK*}{GBK}{song}

\setlength{\LTleft}{0pt} \setlength{\LTright}{0pt} %表格与页面左右边缘之间的矩离均为0

 

\begin{longtable}{|l@{\extracolsep{\fill}}lrr|}% @{\extracolsep{\fill}} 设置使得后面所有列间距可以伸展到预定义的表格宽度。

 \caption{\label{tab:test}我的跨页表格}\\

\hline

\multicolumn{4}{|c|}{\bf 国际电话通达国家、代码及资费标准} \\ \hline

国家 & 中文国名 & 代码 & 资费/分钟 \\ \hline  \hline \endfirsthead %1第一页表头

\multicolumn{4}{r}{续表 ~??????} \\ \hline

国家 & 中文国名 & 代码 & 资费/分钟 \\ \hline  \hline \endhead %2续页表头

\hline

\multicolumn{2}{|c|}{前两列为字符串} & \multicolumn{2}{|c|}{后两列为数字} \\ \hline \endfoot %3除最后一页表尾

\multicolumn{2}{|c|}{最后一页字符串} & \multicolumn{2}{|c|}{最后一页数字} \\ \hline \endlastfoot % 4最后一页表尾

Afghanistan &阿富汗& 93   & 23.00\\\hline

Albania     &阿尔巴尼亚& 356  & 20.70\\\hline

Algeria     &阿尔及利亚& 213  & 27.60\\\hline

Andorra     &安道尔& 376  & 20.70\\\hline

Angola      &安哥拉& 244  & 27.60\\\hline

Argentina   &阿根廷&“& 27.60\\\hline

Armenia     &亚美尼亚& 374  & 18.90\\\hline

Ascension   &阿森松(英)&247  & 27.60\\\hline

Australia   &澳大利亚& 61   & 18.40\\\hline

Austria     &奥地利& 43   & 20.70\\\hline

Azerbaijan  &阿塞拜-M     & 994  & 18.90\\\hline

Bahrain     &巴林& 973  & 27.60\\\hline

Bangladesh  &加拉国& 880  & 17.20、\\\hline

Belarus     &白俄罗斯& 375  & 18.90\\\hline

Afghanistan &阿富汗& 93   & 23.00\\\hline

Albania     &阿尔巴尼亚& 356  & 20.70\\\hline

Algeria     &阿尔及利亚& 213  & 27.60\\\hline

Andorra     &安道尔& 376  & 20.70\\\hline

Angola      &安哥拉& 244  & 27.60\\\hline

Argentina   &阿根廷&“& 27.60\\\hline

Armenia     &亚美尼亚& 374  & 18.90\\\hline

Ascension   &阿森松(英)&247  & 27.60\\\hline

Australia   &澳大利亚& 61   & 18.40\\\hline

Austria     &奥地利& 43   & 20.70\\\hline

Azerbaijan  &阿塞拜-M     & 994  & 18.90\\\hline

Bahrain     &巴林& 973  & 27.60\\\hline

Bangladesh  &加拉国& 880  & 17.20、\\\hline

Belarus     &白俄罗斯& 375  & 18.90\\\hline

Afghanistan &阿富汗& 93   & 23.00\\\hline

Albania     &阿尔巴尼亚& 356  & 20.70\\\hline

Algeria     &阿尔及利亚& 213  & 27.60\\\hline

Andorra     &安道尔& 376  & 20.70\\\hline

Angola      &安哥拉& 244  & 27.60\\\hline

Argentina   &阿根廷&“& 27.60\\\hline

Armenia     &亚美尼亚& 374  & 18.90\\\hline

Ascension   &阿森松(英)&247  & 27.60\\\hline

Australia   &澳大利亚& 61   & 18.40\\\hline

Austria     &奥地利& 43   & 20.70\\\hline

Azerbaijan  &阿塞拜-M     & 994  & 18.90\\\hline

Bahrain     &巴林& 973  & 27.60\\\hline

Bangladesh  &加拉国& 880  & 17.20、\\\hline

Belarus     &白俄罗斯& 375  & 18.90\\\hline

Afghanistan &阿富汗& 93   & 23.00\\\hline

Albania     &阿尔巴尼亚& 356  & 20.70\\\hline

Algeria     &阿尔及利亚& 213  & 27.60\\\hline

Andorra     &安道尔& 376  & 20.70\\\hline

Angola      &安哥拉& 244  & 27.60\\\hline

Argentina   &阿根廷&“& 27.60\\\hline

Armenia     &亚美尼亚& 374  & 18.90\\\hline

Ascension   &阿森松(英)&247  & 27.60\\\hline

Australia   &澳大利亚& 61   & 18.40\\\hline

Austria     &奥地利& 43   & 20.70\\\hline

Azerbaijan  &阿塞拜-M     & 994  & 18.90\\\hline

Bahrain     &巴林& 973  & 27.60\\\hline

Bangladesh  &加拉国& 880  & 17.20、\\\hline

Belarus     &白俄罗斯& 375  & 18.90\\\hline

\end{longtable}

\end{CJK*}

\end{document}

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

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

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

2.3. 适应页面宽度

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

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

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

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

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

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

How to solve longtable is not in 1-column mode error?

转载于 https://tex.stackexchange.com/questions/161431/how-to-solve-longtable-is-not-in-1-column-mode-error

3.1 longtable doesn't work well with twocolumn. But you can use supertabular. Here is an example:

\documentclass[twocolumn]{article}
\usepackage{supertabular,booktabs}
\usepackage[textheight=10cm]{geometry}   %% just for this example.

\begin{document}

\tablefirsthead{\toprule First&\multicolumn{1}{c}{Name} \\ \midrule}
%
\tablehead{%
\multicolumn{2}{c}%
{{\bfseries  Continued from previous column}} \\
\toprule
First&\multicolumn{1}{c}{Name}\\ \midrule}
%
\tabletail{%
\midrule \multicolumn{2}{r}{{Continued on next column}} \\ \midrule}
\tablelasttail{%
\\\midrule
\multicolumn{2}{r}{{Concluded}} \\ \bottomrule}
\begin{supertabular}{ll}
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes here too \\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text
\end{supertabular}%
\end{document}

3.2 As suggested by egreg, xtab is another choice. xtab is built on supertabular while avoiding its weaknesses.

 

\documentclass[twocolumn]{article}
\usepackage{xtab,booktabs}
\usepackage[textheight=10cm]{geometry}   %% just for this example.

\begin{document}
\topcaption{This is top caption}
\bottomcaption{This is bottom caption}
\tablecaption{this is table caption}
\tablefirsthead{\toprule First&\multicolumn{1}{c}{Name} \\ \midrule}
%
\tablehead{%
\multicolumn{2}{c}%
{{\bfseries  Continued from previous column}} \\
\toprule
First&\multicolumn{1}{c}{Name}\\ \midrule}
%
\tabletail{%
\midrule \multicolumn{2}{r}{{Continued on next column}} \\ \midrule}
\tablelasttail{%
\\\midrule
\multicolumn{2}{r}{{Concluded}} \\ \bottomrule}
\begin{xtabular}{ll}
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes here too \\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text
\end{xtabular}%
\end{document}

3.3 If you still want to stick to longtable (e.g. because you are generating LaTeX through pandoc) you could redefine the \longtable command.

A possible solution is this one:

\makeatletter
\let\oldlt\longtable
\let\endoldlt\endlongtable
\def\longtable{\@ifnextchar[\longtable@i \longtable@ii}
\def\longtable@i[#1]{\begin{figure}[t]
\onecolumn
\begin{minipage}{0.5\textwidth}
\oldlt[#1]
}
\def\longtable@ii{\begin{figure}[t]
\onecolumn
\begin{minipage}{0.5\textwidth}
\oldlt
}
\def\endlongtable{\endoldlt
\end{minipage}
\twocolumn
\end{figure}}
\makeatother

NB: it works with two columns, which is what you'll find very often in journal article document classes.

To use this fragment with pandoc, you can e.g. use the -H switch which lets you include some additional code before your actual content. Just create a file called preamble.tex and insert the code above. You can then run pandoc -H preamble.tex and the code will automatically be included.

3.4 Another option, not covered in the previous answers, is to force single column mode along with a \clearpage before your longtable and then reset it to two column mode after. This would look like

\clearpage
\onecolumn
\begin{longtable}
...
\end{longtable}
\clearpage
\twocolumn

This is potentially more useful than the previous examples as it doesn't require rebuilding much, and has the added bonus of working in platforms like arXiv.

 

<think>嗯,用户问的是LaTeX如何允许表格断行。这个问题我之前遇到过,可能得回忆一下LaTeX中处理表格的方法。首先,常规的表格环境比如tabular是不支持的,长表格会直接溢出面底部。这时候可能需要用到一些特定的包或者环境。 首先想到的是longtable包,这个包专门用来处理的长表格。用户可能知道这个包,但不确定具体的用法。需要详细说明如何用longtable替换普通的tabular,以及需要注意的参数,比如表头、表尾的设置。可能还需要提到一些常见问题,比如表格线在分处的显示。 另外,有没有其他方法呢?比如使用supertabular或者xtab包?不过这些可能不如longtable常用,或者存在兼容性问题。可能应该以推荐longtable为主,因为它是LaTeX标准的一部分,稳定性更好。 还有,用户可能在使用booktabs这样的美化表格的包,这时候要提醒他们在longtable中同样可以使用这些样式,保持表格美观。另外,时可能会遇到表格标题的问题,需要正确设置Continued的标题,避免读者混淆。 还要注意表格的重复标题和脚注的设置,可能需要用\endhead, \endfoot等命令来定义每的顶部和底部内容。比如,第一的表头可能包含完整的标题,而后续面的表头显示“续表”。 另外,是否要提到调整表格参数,比如设置断裂的间隔?比如在适当的位置使用\\*来阻止分,或者调整表格的行间距,避免在行中间断裂。这可能对用户有帮助,但可能属于进阶内容,需要视情况简要提及。 最后,用户可能需要一个简单的例子来理解如何实现。提供一个基本的longtable示例,展示如何设置表头、中间内容,以及如何处理分后的显示。同时提醒他们编译时需要运行多次,让LaTeX正确处理分的位置。 可能还需要注意一些常见错误,比如忘记加载longtable包,或者没有正确使用\endfirsthead等命令导致标题重复或缺失。此外,如果表格太宽导致无法在面中显示,可能需要调整表格尺寸,但这属于另一个问题,这里主要解决的问题。 总之,回答应该分步骤,首先介绍longtable的基本用法,然后给出示例,最后提醒注意事项和常见问题。确保用户能够按照步骤操作,同时理解背后的原理。</think>在 LaTeX 中实现表格断行,需使用支持分表格环境(如 `longtable` 或 `xltabular`),替代基础的 `tabular` 或 `table` 环境。以下是具体方法: --- ### 方法 1:使用 `longtable` 包 **步骤:** 1. **加载包**: ```latex \usepackage{longtable} ``` 2. **定义表头与分规则**: ```latex \begin{longtable}{|c|c|c|} \caption{表格示例} \label{tab:my_table} \\ \hline \textbf{列1} & \textbf{列2} & \textbf{列3} \\ \hline \endfirsthead % 第一的表头 \hline \textbf{列1} & \textbf{列2} & \textbf{列3} \\ \hline \endhead % 后续面的表头(自动添加“续表”) \hline \endfoot % 表格底部(最后一) \hline \endlastfoot % 最后一的底部 % 表格内容 数据1 & 数据2 & 数据3 \\ \hline 数据4 & 数据5 & 数据6 \\ \hline % ... 更多数据(自动分) \end{longtable} ``` **关键特性**: - `\endfirsthead`:定义第一的表头。 - `\endhead`:定义后续面的表头(自动添加“续表”效果)。 - `\endfoot` 和 `\endlastfoot`:控制表格底部内容。 --- ### 方法 2:使用 `xltabular` 包(结合 `longtable` 与 `tabularx`) **步骤:** 1. **加载包**: ```latex \usepackage{xltabular} ``` 2. **定义弹性列宽**: ```latex \begin{xltabular}{\textwidth}{|X|X|X|} \caption{弹性列宽表格} \label{tab:xltab} \\ \hline \textbf{列1} & \textbf{列2} & \textbf{列3} \\ \hline \endfirsthead \hline \textbf{列1} & \textbf{列2} & \textbf{列3} \\ \hline \endhead % 表格内容 长文本1 & 长文本2 & 长文本3 \\ \hline 长文本4 & 长文本5 & 长文本6 \\ \hline % ... 更多数据 \end{xltabular} ``` **优势**: - 支持弹性列宽(`X` 列类型),自动调整列宽避免溢出。 --- ### 注意事项 1. **编译次数**:首次编译可能分不准确,需多次编译以稳定布局。 2. **表格线控制**:避免在分处使用 `\hline`,可能导致线不连续。可用 `\midrule`(需 `booktabs` 包)优化。 3. **超宽表格处理**:若表格过宽,需用 `\rotatebox`(`graphicx` 包)旋转或缩小字体。 --- ### 示例代码片段 ```latex \usepackage{longtable,booktabs} \begin{longtable}{@{}llr@{}} \caption{表格示例} \\ \toprule \textbf{名称} & \textbf{类别} & \textbf{价格} \\ \midrule \endfirsthead \multicolumn{3}{c}{续表~\thetable} \\ \toprule \textbf{名称} & \textbf{类别} & \textbf{价格} \\ \midrule \endhead \bottomrule \endfoot 苹果 & 水果 & 5.00 \\ 牛肉 & 肉类 & 30.00 \\ % ... 更多数据 \end{longtable} ``` --- 通过以上方法,可实现表格LaTeX 中自动断行,同时保持格式连贯性。建议优先使用 `longtable` 或 `xltabular` 替代基础表格环境。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值