一、背景
我们使用xelatex引擎编译下面的代码。
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\hbox{ugly suburban sprawl}
\hbox to 2in{ugly suburban sprawldsaddsadasdsadddas}
\hbox to 2in{ugly suburban sprawldsaddsaaaaa}
\hbox to 2in{ugly suburban sprawldsaddsaaaa}
\hbox to 2in{ugly suburban sprawldsaddsaaa}
\hbox to 2in{ugly suburban sprawldsaddsaa}
\hbox spread 2in{ugly suburban sprawl}
\hbox{ugly suburban sprawl}
\hbox to 2in{ugly\hfill suburban\hfill sprawl}
\hbox spread 2in{ugly \hfill suburban \hfill sprawl}
$\hbox spread 2in{ugly \hfill suburban \hfill sprawl}$\\
$$\hbox spread 2in{ugly \hfill suburban \hfill sprawl}$$\\
\end{document}我们可以发现诸如【Overfull \hbox (36.50002pt too wide)】 、【Underfull \hbox (badness 3503)】的错误。
二、解决方法
参考资料
errors - What does the phrase "Underfull \hbox (badness 10000) in paragraph" actually mean? - TeX - LaTeX Stack ExchangeOverfull \hbox或Underfull \hbox错误提示 基于LaTex+VSCode+MAC_Zhao-Jichao的博客-CSDN博客LaTeX: 解决 Badbox warning Underfull \hbox (badness 10000) in paragraph_Mic28的博客-CSDN博客Overfull \hbox或Underfull \hbox错误提示 基于LaTex+VSCode+MAC_Zhao-Jichao的博客-CSDN博客errors - What does the phrase "Underfull \hbox (badness 10000) in paragraph" actually mean? - TeX - LaTeX Stack Exchange
在利用\hbox命令建立方框时,以文本的自然宽度作为方框的宽度;而利用\hbox to xxx命令建立,以指定的xxx长度作为方框宽度;而利用\hbox spread xxx建立,是在文本自然宽度基础上加xxx长度作为方框宽度。
显然,上面出现的overfull和underfull错误,第一种形式建立是不可能发生的!而只有第二种、第三种才会发生。
overfull代表方框过填充了,文本内容超过了方框指定宽度;如本文第一部分的错误【Overfull \hbox (36.50002pt too wide)】所示,36.5pt代表过填充的程度。
underfull代表方框设定宽度太大了,内容未填满;如本文第一部分的错误【Underfull \hbox (badness 3503)】所示,3503代表欠填充的程度。
对于overfull警报的解决方法——很简单,就是将指定宽度数值xxx设定更大一些。
对于underfull警报的解决方法——在不减少文本内容的前提下有三种。第一种是去除段落后的\\,第二种是减小指定宽度数值xxx,第三种是在文本中加入\hfill或\hfil命令。
其中第一种,一般是因为数学公式环境(不管是行间公式还是行内)中用\hbox命令时还在行尾加了\\,导致了underfull警报;
而\hbox命令在正文环境中使用,行尾加\\出现的应该是错误【There's no line here to end.
】。

三、badness的数值大小
我们也可以发现,underfull警报,是发生在badness 超过一定大小之后的。
如下图所示,差一两个字符a,并不会引发警报(有警报的行都有波浪线)。

当badness的数值超过了10000,就显示为10000,无法再继续上涨了,因为没太大意义。