表格:溢出的水平盒子

表格:溢出的水平盒子

当我尝试计算以下 Latex 代码时,我不断收到“overfull hbox”错误。我尝试使用 P{xcm} 手动控制列宽,但这似乎也不起作用。有人知道一个可靠的解决方案吗?

\begin{table}[H]
\begin{tabular}{|ccccccc|}
\hline
\multicolumn{7}{|c|}{\textbf{Mechanical Loads}}                                                                                                                                                                                             \\ \hline
\multicolumn{1}{|c|}{Requirement} & \multicolumn{1}{c|}{Description}       & \multicolumn{1}{c|}{Engineer} & \multicolumn{1}{c|}{Location}       & \multicolumn{1}{c|}{TR Number} & \multicolumn{1}{c|}{Date Completed} & Pass / Fail \\ \hline
\multicolumn{1}{|c|}{Test 1}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 2}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 3}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 4}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 5}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 6}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 7}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 8}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 9}      & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 10}     & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 11}     & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 12}     & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\multicolumn{1}{|c|}{Test 13}     & \multicolumn{1}{c|}{Brief description} & \multicolumn{1}{c|}{Example}  & \multicolumn{1}{c|}{United Kingdom} & \multicolumn{1}{c|}{}          & \multicolumn{1}{c|}{}               &             \\ \hline
\end{tabular}
\end{table}

答案1

首先,你必须摆脱大量的\multicolumn{1}包装器。没有这个修复,其他一切都不重要。(题外话:为什么要插入这些包装器?可能是因为设计工作是机器人做的??)

一旦这些包装器消失,我建议您使用一个tabularx环境,将其整体宽度设置为\textwidth,并允许在六个数据列中的单元格内换行。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,ragged2e,booktabs}
\newcolumntype{C}{>{\Centering\hspace{0pt}}X}
\renewcommand\tabularxcolumn[1]{m{#1}}

\begin{document}
\begin{table}[ht]
\setlength\tabcolsep{3pt} % default: 6pt
\caption{Mechanical Loads\strut}
\begin{tabularx}{\textwidth}{@{} l CCCCCC @{}}
\toprule
Req'ment & Description & Engineer & Location & TR Number & Date Completed & Pass\slash Fail \\ 
\midrule
Test 1 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
Test 2 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
\dots \\
%Test 3 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 4 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 5 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 6 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 7 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 8 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 9 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 10 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 11 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
%Test 12 & Brief description & Example & United Kingdom &  &  & \\ \addlinespace
Test 13 & Brief description & Example & United Kingdom &  &  & \\ 
\bottomrule
\end{tabularx}
\end{table}
\end{document}

答案2

  • 请始终提供 MWE(最小工作示例),这是一个小但完整、可编译的文档,可以重现您的问题
  • 由于缺少它,我们没有关于您的文档页面布局(大小\textwidth,这对表格显示有很大影响)的任何信息。
  • 看来您使用了一些表格生成器来编写表格,这些生成器会用不必要的\multicolumn{1}{c}{...}包装器填充代码。删除它们!
  • 所选的列类型以及使用的\multicolumns 不能将单元格中的文本分成几行(或更多行),因此会出现问题。
  • 你可以考虑使用 ˙tabularray` 包来制作表格。使用它制作表格代码会更简洁:
\documentclass{article}
\usepackage{ragged2e}
\usepackage{tabularray,ragged2e}
\UseTblrLibrary{booktabs}
%\newcolumntype{C}{>{\Centering\hspace{0pt}}X}
%\renewcommand\tabularxcolumn[1]{m{#1}}

\begin{document}
    \begin{table}[ht]
\caption{Mechanical Loads\strut}
\begin{tblr}{colsep=1pt,
             colspec = {@{} l 
                        *{6}{X[m, font=\linespread{0.84}\selectfont,
                                  cmd=\Centering\hspace{0pt}]} @{}}
            }
\toprule
Req'ment & Description & Engineer & Location & TR Number & Date Completed & Pass\slash Fail \\
\midrule
Test  1 & Brief description & Example & United Kingdom &  &  & \\ 
Test  2 & Brief description & Example & United Kingdom &  &  & \\ 
Test  3 & Brief description & Example & United Kingdom &  &  & \\
Test  4 & Brief description & Example & United Kingdom &  &  & \\
Test  5 & Brief description & Example & United Kingdom &  &  & \\
Test  6 & Brief description & Example & United Kingdom &  &  & \\
Test  7 & Brief description & Example & United Kingdom &  &  & \\
Test  8 & Brief description & Example & United Kingdom &  &  & \\
Test  9 & Brief description & Example & United Kingdom &  &  & \\
Test 10 & Brief description & Example & United Kingdom &  &  & \\
Test 11 & Brief description & Example & United Kingdom &  &  & \\
Test 12 & Brief description & Example & United Kingdom &  &  & \\
Test 13 & Brief description & Example & United Kingdom &  &  & \\
\bottomrule
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

答案3

首先,推荐阅读以下文章。其次,不要使用表格生成器!坦率地说,ChatGPT 可以为像您这样的简单表格生成更干净的代码。无论如何,链接指向内容,阅读后您应该能够在没有太多帮助的情况下创建此表格甚至更复杂的表格。

根据您的示例,表格太宽,无法容纳在一页内。在众多解决方案中,我认为您可以制作两个组并将它们堆叠在一起,或者旋转并垂直呈现表格(请参阅下面的部分)。您还应该使用标题代替额外的顶部标题,如果您愿意,可以对其进行格式化;例如,在下面的代码中,取消注释行\DeclareCaption...和行内行\captionsetup以使标题加粗,但我不建议这样做。

如果单元格太宽,p{}-type column 会创建类似段落的单元格,并在必要时添加换行符。另请参阅米科的答案例如使用X-type 来统一列。或者,可以使用方便的宏手动添加换行符,例如\thead用于标题或\makecell常规单元格。在这两种情况下\\都可以接受,但这两个宏都需要额外的makecell包。最重要的是,\Gape在最后一个标题中增加行高以增加标题空间(也需要makecell包)。

旋转

\documentclass{article}
\usepackage{rotating}
\usepackage{makecell}
\usepackage{caption}

% \DeclareCaptionTextFormat{bold-text}{\bfseries#1}
% \DeclareCaptionLabelFormat{bold-label}{\bfseries\bothIfFirst{#1~}{}#2}
\captionsetup[table]{
  position=top,
  skip=3pt,
  % textformat=bold-text,
  % labelformat=bold-label,
}
\renewcommand{\theadfont}{\normalsize}
\renewcommand{\theadgape}{\Gape[0pt][0pt]}


\begin{document}
\begin{sidewaystable}
  \renewcommand{\arraystretch}{1.25}
  \centering
  \caption{Mechanical Loads}\label{tab:mech-loads-v}
  \begin{tabular}{l *6{c}}
    \hline
    \thead{Requirement}
    & \thead{Description} & \thead{Engineer} & \thead{Location}
    & \thead{TR\\Number} & \thead{Date\\Completed} & \thead{\Gape[10pt]{Pass/Fail}} \\
    \hline
    Test 1  & Brief description & Example & United Kingdom & & & \\
    Test 2  & Brief description & Example & United Kingdom & & & \\
    Test 3  & Brief description & Example & United Kingdom & & & \\
    Test 4  & Brief description & Example & United Kingdom & & & \\
    Test 5  & Brief description & Example & United Kingdom & & & \\
    Test 6  & Brief description & Example & United Kingdom & & & \\
    Test 7  & Brief description & Example & United Kingdom & & & \\
    Test 8  & Brief description & Example & United Kingdom & & & \\
    Test 9  & Brief description & Example & United Kingdom & & & \\
    Test 10 & Brief description & Example & United Kingdom & & & \\
    Test 11 & Brief description & Example & United Kingdom & & & \\
    Test 12 & Brief description & Example & United Kingdom & & & \\
    Test 13 & Brief description & Example & United Kingdom & & & \\
    \hline
  \end{tabular}
\end{sidewaystable}
\end{document}

分成小组

\documentclass{article}
\usepackage{makecell}
\usepackage{caption}

% \DeclareCaptionTextFormat{bold-text}{\bfseries#1}
% \DeclareCaptionLabelFormat{bold-label}{\bfseries\bothIfFirst{#1~}{}#2}
\captionsetup[table]{
  position=top,
  skip=3pt,
  % textformat=bold-text,
  % labelformat=bold-label,
}
\renewcommand{\theadfont}{\normalsize}
\renewcommand{\theadgape}{\Gape[0pt][0pt]}


\begin{document}
\begin{table}[tbh]
  \renewcommand{\arraystretch}{1.25}
  \centering
  \caption{Mechanical Loads}\label{tab:mech-loads-h}
  \begin{tabular}{l *3{c}}
    \hline
    \thead{Requirement}
    & \thead{Description} & \thead{Engineer} & \thead{\Gape[10pt]{Location}} \\
    \hline
    Test 1  & Brief description & Example & United Kingdom \\
    Test 2  & Brief description & Example & United Kingdom \\
    Test 3  & Brief description & Example & United Kingdom \\
    Test 4  & Brief description & Example & United Kingdom \\
    Test 5  & Brief description & Example & United Kingdom \\
    Test 6  & Brief description & Example & United Kingdom \\
    Test 7  & Brief description & Example & United Kingdom \\
    Test 8  & Brief description & Example & United Kingdom \\
    Test 9  & Brief description & Example & United Kingdom \\
    Test 10 & Brief description & Example & United Kingdom \\
    Test 11 & Brief description & Example & United Kingdom \\
    Test 12 & Brief description & Example & United Kingdom \\
    Test 13 & Brief description & Example & United Kingdom \\
    \hline
    \hline
    \thead{Requirement}
    & \thead{TR\\Number} & \thead{Date\\Completed} & \thead{\Gape[9pt]{Pass/Fail}} \\
    \hline
    Test 1  & & & \\
    Test 2  & & & \\
    Test 3  & & & \\
    Test 4  & & & \\
    Test 5  & & & \\
    Test 6  & & & \\
    Test 7  & & & \\
    Test 8  & & & \\
    Test 9  & & & \\
    Test 10 & & & \\
    Test 11 & & & \\
    Test 12 & & & \\
    Test 13 & & & \\
    \hline
  \end{tabular}
\end{table}
\end{document}
旋转 分裂
在此处输入图片描述 在此处输入图片描述

相关内容