配置目录时出现问题

配置目录时出现问题

我已经设定了目录 (TOC)如所附截图所示,显示方框内的章节号

一切都很完美,直到这本书超过 9 章。问题就出现了;也就是说,当章节号变成 2 位数字时,问题就出现了。显然,如图所示,我想要纠正的不良效果是包含章节号的框未对齐

实际上,更不合适的是那些包含个位数的框,因为它们会更多地侵占左边距。这些框应左对齐,但要尊重左边距。

另一方面,我也希望章节编号在包含它们的框内完全居中, 也优化代码,使小节编号右侧的空格自动调整取决于数字的宽度。

一个最小工作示例 __________________________________________________

\documentclass[a4paper,14pt,twoside,reqno]{extbook}
\usepackage[margin=28mm
]{geometry}
\usepackage{amsmath,amssymb,amsthm,mathpazo}
\usepackage[english]{babel}
\usepackage{titlesec}
\usepackage{tocloft, etoolbox}

% ------------------------------<<< Set up TOC >>>-------------------------------

\titleformat{\chapter}{\large \bfseries}{\filright \fboxrule 3pt \framebox[1.8em][c]{\thechapter}}{16pt}{\large}

%By https://tex.stackexchange.com/users/239495/c-peters:
\renewcommand\cftchapnumwidth{2em}
\renewcommand\cftsecindent{2em}
\renewcommand\cftsubsecindent{2em}
\renewcommand\cftchapaftersnum{\hskip-1.6em
    \filright \fboxrule 3pt \fbox{$\mathstrut${\Large~~~}}}%[1.8em][c]{}}
\renewcommand\cftsecpresnum{\S \hspace{6pt}}
\renewcommand\cftsubsecpresnum{$ \bullet $ \hspace{2pt}}
\renewcommand\cftsecnumwidth{4em}
\renewcommand\cftsubsecnumwidth{4em} %Sets the spacing of the title after the subsection number. The gap varies discretely depending on the number of sections and subsections, I would approach it starting with the minimum gap, that is, with \renewcommand \cftsubsecnumwidth {2.5em}, and once the document is completed you can see if it is necessary to increase the parameter of this command. 
% END _____________________________________________

% -----<<< Prints the part title between two horizontal lines >>>------

\renewcommand{\cftpartfont}{\bfseries\Large\hrule} %add line above part
\renewcommand{\cftbeforepartskip}{5mm}
\renewcommand{\cftpartafterpnum}{\\\hrule}%add line below part
\cftpagenumbersoff{part}%get rid of part page numbers
\renewcommand{\cftparskip}{4mm}%control line spacing
% END _____________________________________________

\begin{document}
    \pagestyle{plain}
    \tableofcontents
    
    \addcontentsline{toc}{chapter}{FOREWORD.}
    \chapter*{FOREWORD.}
    
    \part{Part Example 1.}
    \chapter{Chapter Example 1.}
    \chapter{Chapter Example 2.}
    \chapter{Chapter Example 3.}
    \chapter{Chapter Example 4.}
    \chapter{Chapter Example 5.}
    \chapter{Chapter Example 6.}
    \chapter{Chapter Example 7.}
    \chapter{Chapter Example 8.}
    \chapter{Chapter Example 9.}
    \section{Section Example 1.}
    \subsection{Subsection Example 1.}
    \chapter{Chapter Example 10.}
    \chapter{Chapter Example 11.}
    \chapter{Chapter Example 12.}
\end{document}

目录

目录

目录

答案1

尝试一下这个代码。

在序言末尾添加了一个补丁,使方框对齐并使章节号居中。(仅保留了相关的软件包。)

b

% !TeX TS-program = pdflatex 
 
\documentclass[a4paper,14pt,twoside,reqno]{extbook}
\usepackage[margin=28mm
]{geometry}
    
\usepackage{times}

\usepackage{titlesec}   
\usepackage{tocloft}

\titleformat{\chapter}{\large \bfseries}{\filright \fboxrule 3pt \framebox[1.8em][c]{\thechapter}}{16pt}{\large}

\newlength{\chapternW}\setlength{\chapternW}{3.0em}% width of the chapter number box <<<<<<<<<<<<   

\renewcommand\cftchapnumwidth{\chapternW}% changed <<<<<<<<<<
\renewcommand\cftsecindent{\chapternW} %changed <<<<<<<<<<
\renewcommand\cftsubsecindent{\chapternW} %changed <<<<<<<<<<

\renewcommand\cftsecpresnum{\S \hspace{6pt}}
\renewcommand\cftsubsecpresnum{$ \bullet $ \hspace{2pt}}
\renewcommand\cftsecnumwidth{4em}
\renewcommand\cftsubsecnumwidth{4em} %Sets the spacing of the title after the subsection number
    
\renewcommand{\cftpartfont}{\bfseries\Large\hrule}%add line above part

\renewcommand{\cftbeforepartskip}{5mm}
\renewcommand{\cftpartafterpnum}{\\\hrule}%add line below part
\cftpagenumbersoff{part}%get rid of part page numbers

\renewcommand{\cftparskip}{4mm}%control line spacing

%************************************** added <<<<<<<<<<<<<<<<<<<<<<<<<<
\usepackage{xpatch}
\tracingpatches
\makeatletter
\xpatchcmd\@chapter{\protect\numberline{\thechapter}#1}
{\protect\numberline{\setlength{\fboxrule}{2pt} \framebox[2em]{\thechapter}}#1}{}{}
%**************************************

\begin{document}
    \pagestyle{plain}
    \tableofcontents
    
    \addcontentsline{toc}{chapter}{FOREWORD.}
    \chapter*{FOREWORD.}
    
    \part{Part Example 1.}
    \chapter{Chapter Example 1.}
    \chapter{Chapter Example 2.}
    \chapter{Chapter Example 3.}
    \chapter{Chapter Example 4.}
    \chapter{Chapter Example 5.}
    \chapter{Chapter Example 6.}
    \chapter{Chapter Example 7.}
    \chapter{Chapter Example 8.}
    \chapter{Chapter Example 9.}
    \section{Section Example 1.}
    \subsection{Subsection Example 1.}
    \chapter{Chapter Example 10.}
    \chapter{Chapter Example 11.}
    \chapter{Chapter Example 12.}
\end{document}

相关内容