![这段代码有什么问题(如何修复过满的 \hbox)?](https://linux22.com/image/466783/%E8%BF%99%E6%AE%B5%E4%BB%A3%E7%A0%81%E6%9C%89%E4%BB%80%E4%B9%88%E9%97%AE%E9%A2%98%EF%BC%88%E5%A6%82%E4%BD%95%E4%BF%AE%E5%A4%8D%E8%BF%87%E6%BB%A1%E7%9A%84%20%5Chbox%EF%BC%89%EF%BC%9F.png)
我收到了很多“溢出 \hbox“在编译大型文档时出现警告,显然这是由以下 ToC 定制引起的。编译后的 PDF 输出看起来不错,但我想优化代码并删除所有警告和令人讨厌的过满 hbox 消息。
\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[11pt,twoside,draft]{book}
\usepackage[T1]{fontenc}
\usepackage[nomath]{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{microtype,textcomp}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{2}
\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize}%
% Chapters modification in ToC :
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnumb}{\newline}
\setlength{\cftchapindent}{0em} % Spacement to left of chapter number.
\setlength{\cftchapnumwidth}{0em} %
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\pagestyle{plain}
\chapter{A first chapter}
\section{A section title}
Blabla
\chapter{A second chapter}
\section{A section title}
Blabla
\chapter{A third chapter}
\section{A section title}
Blabla
\chapter{A fourth chapter}
\section{A section title}
Blabla
\chapter{A fifth chapter}
\section{A section title}
Blabla
\end{document}
我draft
向文档类添加了该选项,以查看导致这些消息的原因,但我不知道下一步该怎么做。那么上面的 MWE 代码有什么问题?
预览上面的代码所起的作用(黑色矩形来自draft
文档类中的选项):
答案1
tocloft
用于\cftchapnumwidth
包含整个章节号的框。具体使用以下定义:
\renewcommand{\numberline}[1]{%
\hb@xt@\@tempdima % in case of chapters this is similar to \makebox[\cftchapnumwidth]
{\@cftbsnum #1\@cftasnum\hfil \@cftasnumb}
您已将其设置\cftchapnumwidth
为 0。因此,章节号(无论是否更改\cftchappresnum
和\cftchapaftersnumb
)始终较宽,这会导致 \hbox 溢出。如果您不想忽略警告,可以尝试在 a\rlap
或 a中设置章节号\makebox[0pt][l]
。为此,您可以使用 hack,但您也应该知道,这种 hack 将来可能会失效:
\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[11pt,twoside,draft]{book}
\usepackage[T1]{fontenc}
\usepackage[nomath]{lmodern}
\usepackage[utf8]{inputenc}% Not needed any longer
\usepackage[french]{babel}
\usepackage{microtype,textcomp}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{2}
\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize}%
% Chapters modification in ToC :
\renewcommand{\cftchappresnum}{\specialchapterintoc}% Warning: Using a command with argument is a hack only and would fail for long numbers
\renewcommand{\cftchapaftersnumb}{\newline}
\setlength{\cftchapindent}{0em} % Spacement to left of chapter number.
\setlength{\cftchapnumwidth}{0em} %
\newcommand*{\specialchapterintoc}[1]{\rlap{\chaptername\ #1}}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\pagestyle{plain}
\chapter{A first chapter}
\section{A section title}
Blabla
\chapter{A second chapter}
\section{A section title}
Blabla
\chapter{A third chapter}
\section{A section title}
Blabla
\chapter{A fourth chapter}
\section{A section title}
Blabla
\chapter{A fifth chapter}
\section{A section title}
Blabla
\end{document}
另一个建议是修补\numberline
不再使用\hbox
零宽度,如果\cftXnumwidth
为零。假设宽度为线宽的一半就足够了:
\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[11pt,twoside,draft]{book}
\usepackage[T1]{fontenc}
\usepackage[nomath]{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{microtype,textcomp}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{2}
\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize}%
% Chapters modification in ToC :
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnumb}{\newline}
\setlength{\cftchapindent}{0em} % Spacement to left of chapter number.
\setlength{\cftchapnumwidth}{0em} %
\makeatletter
\AddToHook{cmd/numberline/before}{%
\ifdim \@tempdima=\z@ \@tempdima.5\linewidth\fi
}
\makeatother
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\pagestyle{plain}
\chapter{A first chapter}
\section{A section title}
Blabla
\chapter{A second chapter}
\section{A section title}
Blabla
\chapter{A third chapter}
\section{A section title}
Blabla
\chapter{A fourth chapter}
\section{A section title}
Blabla
\chapter{A fifth chapter}
\section{A section title}
Blabla
\end{document}
这仍然使用的内部结构tocloft
,但恕我直言,比第一个建议干净得多。
另一个建议是使用支持数字后换行的包,例如,tocbasic
:
\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[11pt,twoside,draft]{book}
\usepackage[T1]{fontenc}
\usepackage[nomath]{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{microtype,textcomp}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{tocbasic}
\DeclareTOCStyleEntry[%
breakafternumber=true,% do a linebreak after the number
entrynumberformat=\chapternumberwithprefix,% see definition below
numwidth=.5\linewidth,% I think, this would be enough, but …
dynnumwidth,% if not use the needed width.
]{tocline}{chapter}
\newcommand*{\chapternumberwithprefix}[1]{\chaptername\ #1}
% Following command replaces your redefinition of \cftsubsubsecfont:
\DeclareTOCStyleEntry[
entrynumberformat=\normalfont\footnotesize\textsf
]{tocline}{subsubsection}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\pagestyle{plain}
\chapter{A first chapter}
\section{A section title}
Blabla
\chapter{A second chapter}
\section{A section title}
Blabla
\chapter{A third chapter}
\section{A section title}
Blabla
\chapter{A fourth chapter}
\section{A section title}
Blabla
\chapter{A fifth chapter}
\section{A section title}
Blabla
\end{document}
答案2
你可以使用这个:
{\setbox0\hbox{\cftchapfont\chaptername\ \kern\cftchapnumwidth}%
\global\cftchapnumwidth\wd0}
\renewcommand{\cftchapaftersnumb}{\newline\mbox{}\kern-\cftchapnumwidth}
包含上述更改的完整代码:
\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[11pt,twoside,draft]{book}
\usepackage[T1]{fontenc}
\usepackage[nomath]{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{microtype,textcomp}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{2}
\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize}%
% Chapters modification in ToC :
\renewcommand{\cftchappresnum}{\chaptername\ }
{\setbox0\hbox{\cftchapfont\chaptername\ \kern\cftchapnumwidth}%
\global\cftchapnumwidth\wd0}
\renewcommand{\cftchapaftersnumb}{\newline\mbox{}\kern-\cftchapnumwidth}
% from original
% \setlength{\cftchapindent}{0em} % Spacement to left of chapter number.
% \setlength{\cftchapnumwidth}{0em} %
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\pagestyle{plain}
\chapter{A first chapter}
\section{A section title}
Blabla
\chapter{A second chapter}
\section{A section title}
Blabla
\chapter{A third chapter}
\section{A section title}
Blabla
\chapter{A fourth chapter}
\section{A section title}
Blabla
\chapter{A fifth chapter}
\section{A section title}
Blabla
\end{document}