如何强制长表格适应页面宽度

如何强制长表格适应页面宽度

我有一张长表,其格式直接取自背面:

\begin{center}
    \begin{longtable}{|l|l|l||l|l|l|}
        \caption{A sample long table.} \label{tab:long} \\
        
        \hline \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{c|}{\textbf{Second column}} & \multicolumn{1}{c|}{\textbf{Third column}} & \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{|c|}{\textbf{First column}}\\ \hline 
        \endfirsthead
        
        \multicolumn{6}{c}%
        {{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
        \hline \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{c|}{\textbf{Second column}} & \multicolumn{1}{c|}{\textbf{Third column}} & \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{|c|}{\textbf{First column}}\\ \hline 
        \endhead
        
        \hline \multicolumn{6}{|r|}{{Continued on next page}} \\ \hline
        \endfoot
        
        \hline \hline
        \endlastfoot 
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 \\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 \\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 \\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 \\

我的表格中有 6 列,由于有 6 列,所以表格的总宽度超出了页面。有什么办法可以解决这个问题吗?通常,我会对文档中的普通表格使用以下命令,效果很好。

\resizebox{0.99\textwidth}{!}

我不知道如何缩小长表格以使其像其他表格一样适合页面。

答案1

您没有提供 MWE,因此我们只能询问您的页面布局。假设它由geometry包决定,并且表格的虚拟内容接近实际内容,则可以按如下方式设计表格:

在此处输入图片描述

它是通过使用以下tabularray包生成的:

\documentclass{article}
\usepackage{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
  \ExplSyntaxOn
\NewChildSelector{eachfive}
  {
    \int_step_inline:nnnn {6}{5}{\l_tblr_childs_total_tl}
      { \clist_put_right:Nn \l_tblr_childs_clist {##1} }
  }
\ExplSyntaxOff

\begin{document}
\begin{longtblr}[
    caption = {A sample long table.}, 
      label = {tab:long}
                ]{rowhead = 1,
                  colspec = {@{} X[0.5,l]X[1.2,l]X[1,l]
                                 X[0.5,l]X[1.2,l]X[1,l] @{}},
                column{3} = {rightsep=2em},
                   rowsep = 0.5pt,
                   row{1} = {font=\small\bfseries},
             row{eachfive} = {abovesep=1ex},
                 }                                                      
    \toprule
{First\\ column}    
    & {Second\\ column} 
        & {Third\\ column}  
            & {First\\ column}
                & {Second\\ column}
                        & {Third\\ column}                              \\
    \cmidrule[r=-0.5]{1-3}
    \cmidrule[l=-0.5]{4-6}
% table body
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\

One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
One & abcdef ghjijklmn & 123.456778 & One & abcdef ghjijklmn & 123.456778 \\
    \bottomrule
\end{longtblr}

\end{document}

答案2

因为没有平均能量损失给出答案,假设标准article类。

现在,如果表格宽度超出页面宽度,那么好的做法是减小字体大小,而不是调整字体大小。您可能会发现原因这里

此外,如果无法减小字体大小,您可以考虑使用固定宽度的列。

梅威瑟:

\documentclass{article}

\usepackage{array}
\usepackage{longtable}
\usepackage{lipsum}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}
    \lipsum[1]
    \footnotesize
    \begin{longtable}{|l|l|l||l|l|l|}
        \caption{A sample long table (reduced font size).} \label{tab:long} \\
        
        \hline \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{c|}{\textbf{Second column}} & \multicolumn{1}{c||}{\textbf{Third column}} & \multicolumn{1}{c|}{\textbf{First column}} & \multicolumn{1}{c|}{\textbf{First column}} & \multicolumn{1}{c|}{\textbf{First column}}\\ \hline 
        \endfirsthead
        
        \multicolumn{6}{c}%
        {{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
        \hline \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{c|}{\textbf{Second column}} & \multicolumn{1}{c|}{\textbf{Third column}} & \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{|c|}{\textbf{First column}}\\ \hline 
        \endhead
        
        \hline \multicolumn{6}{|r|}{{Continued on next page}} \\ \hline
        \endfoot
        
        \hline \hline
        \endlastfoot 
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 & \\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 &\\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 &\\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 &\\
    \end{longtable}
    \normalsize
    \lipsum[2]
    
    \begin{longtable}{*{6}{|p{1.5cm}}|}%{|l|l|l||l|l|l|}
        \caption{A sample long table (using fixed width column).} \label{tab:long2} \\
        
        \hline \multicolumn{1}{|c|}{\textbf{First column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{Second column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{Third column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{First column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{First column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{First column}}\\ \hline 
        \endfirsthead
        
        \multicolumn{6}{c}%
        {{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
        \hline \multicolumn{1}{|C{1.5cm}|}{\textbf{First column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{Second column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{Third column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{First column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{First column}} & \multicolumn{1}{C{1.5cm}|}{\textbf{First column}}\\ \hline  
        \endhead
        
        \hline \multicolumn{6}{|r|}{{Continued on next page}} \\ \hline
        \endfoot
        
        \hline \hline
        \endlastfoot 
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 & \\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 &\\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 &\\
        One & abcdef ghjijklmn & 123.456778 One & abcdef ghjijklmn & 123.456778 &\\
    \end{longtable}
    \lipsum[3]
\end{document}

输出:

输出

相关内容