包含长单元格内容的表格跨越多页

包含长单元格内容的表格跨越多页

我在用表格型创建一个非常大的描述表,其中许多单元格的内容要么很长(跨越多行),要么需要显式换行。下表是一个简单的示例:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{tabularx}        % splits cells with text longer than the column width
\usepackage{makecell}        % to allow for a header to span over two lines

\newcolumntype{f}{>{\hsize=.2\hsize}X}    % each of the columns has a defined static width
\newcolumntype{s}{>{\hsize=.3\hsize}X}
\newcolumntype{t}{>{\hsize=.5\hsize}X}
\newcommand{\heading}[1]{\multicolumn{1}{|c|}{#1}}   % centering the headers

\begin{document}
\begin{table}
\caption{Sample table}
\centering
    \begin{tabularx}{\textwidth}{|f|s|t|} 

    \hline
    \heading{\textbf{\makecell{Short\\Column}}} & \heading{\textbf{Medium Column}}  & \heading{\textbf{Long Column}} \\ \hline
    
    Value       & Text      & Text              \\ \hline
    Value       & Text      & This text is too long to fit in one line and is automatically split into three   \\ \hline
    Value       & Text      & - 0: Important \newline
                              - 1: instance \newline
                              - 2: where \newline
                              - 3: the lines \newline
                              - 4: are manually \newline
                              - 5: split \newline
                              - 6: into \newline
                              - 7: items        \\ \hline
    Value       & Text      & Text              \\ \hline
    Value       & Text      & Text              \\ \hline
    ...
    Value       & Text      & Text              \\ \hline
    
    \end{tabularx}
\end{table}

\end{document}

然后它会超出页面高度限制,直到消失在页面末尾(还请注意表格中间的页码):

在此处输入图片描述

有没有办法让这样的表格跨越多页?我找到的解决方案(例如201420162017)解决与我类似的问题——使用如下软件包长桌表列— 不适用于我已有的表格的某些细节。我曾尝试调整它们以使其如此,但无法使其工作。

提前感谢您的帮助!

答案1

正如@Bernard 在他的评论中所建议的那样,你应该

  • 不使用浮动table,这样可以防止表格在页面上断裂
  • 桌面用xltabular包装

使用任何类型的长表时您应该注意,这些表只能在行之间进行页面拆分。

您的表格的一个例子是:

\documentclass[12pt]{article}

\usepackage{xltabular}        % splits cells with text longer than the column width
\newcolumntype{f}{>{\hsize=.2\hsize\hsize=\linewidth}X}    % each of the columns has a defined static width
\newcolumntype{s}{>{\hsize=.3\hsize\hsize=\linewidth}X}
\newcolumntype{t}{>{\hsize=.5\hsize\hsize=\linewidth}X}
\usepackage{makecell}        % to allow for a header to span over two lines
\renewcommand\theadfont{\bfseries}

\begin{document}
\begingroup
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
    \begin{xltabular}{\textwidth}{|f|s|t|} 
\caption{Sample table}
\label{tab:longtable}   \\
    \hline
\thead{Short Column}   & \thead{Medium Column}   & \thead{Long Column}     \\
    \hline
\endfirsthead
\caption[]{Sample table (cont.)}\\
    \hline
\thead{Short Column}   & \thead{Medium Column}   & \thead{Long Column}     \\
    \hline
\endhead
\multicolumn{3}{r}{\footnotesize\itshape{Continued on the next page}}           \\
\endfoot
\endlastfoot
%   table body
Value       & Text      & Text              \\ \hline
Value       & Text      & This text is too long to fit in one line and is automatically split into three   \\ \hline
Value       & Text      & - 0: Important \newline
                          - 1: instance \newline
                          - 2: where \newline
                          - 3: the lines \newline
                          - 4: are manually \newline
                          - 5: split \newline
                          - 6: into \newline
                          - 7: items        \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
Value       & Text      & Text              \\ \hline
    \end{xltabular}
\endgroup

\end{document}

在此处输入图片描述

相关内容