使用包'titletoc'时出现一个非常奇怪的现象,请问原因是什么以及如何解决?

使用包'titletoc'时出现一个非常奇怪的现象,请问原因是什么以及如何解决?

当我在章节开头制作部分表格时,第一个包含“\paragraph”的章节的页码无法与其他章节的页码对齐。

\documentclass[openany]{book}
\raggedbottom       %reduce underfull \vbox

\usepackage{titlesec, titletoc}
\titlelabel{\thetitle\quad}
%change the part format in book 
\titleformat{\part}[frame]{\normalfont\huge\bfseries}{\partname\ \thepart }{20pt}{\Huge\centering}
%change the part format in table of contents
\titlecontents{part}%
[0pt]{\sffamily\bfseries\huge\protect\addvspace{25pt}\titlerule\addvspace{1.5ex}}%remove rule if you like
{}{\partname~}
{\hfill\contentspage}%replaced with {} if don't want page number for parts
[\addvspace{0.7ex}\titlerule\addvspace{10pt}]%remove rule if you like

%change the chapter format in table of contents
\titlecontents{chapter}%
[0pt]{\sffamily\bfseries\large\protect\addvspace{10pt}}%
{\chaptertitlename\ \thecontentslabel~}{}
{\hfill\contentspage}%
[\addvspace{1.5ex}]%

%change the appendix name from chapter to appendix
\makeatletter
\g@addto@macro{\appendix}{%
  \renewcommand\chaptername{Appendix}%
  \addtocontents{toc}{\protect\renewcommand*{\protect\chaptername}{\protect\appendixname}}%
}
\makeatother
%force paragraph newline
\titleformat{\paragraph}[hang]{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}

%customize page layout
\usepackage[a4paper, left=3cm, right=3cm]{geometry}

%color
\usepackage[dvipsnames]{xcolor}

%page headers and footers   left,right,odd,even
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\normalfont\small\rmfamily\nouppercase{\leftmark}}
\fancyhead[LO]{\normalfont\small\rmfamily\nouppercase{\rightmark}}
\fancyhead[LE,RO]{\thepage}


%revise\@endpart macro
\makeatletter
\def\@endpart{\bigbreak} 
\makeatother


%define a new command named cdotfill
\makeatletter
\newcommand\cdotfill{%
    \leavevmode\cleaders\hb@[email protected]{\hss$\cdot$\hss}\hfill\kern\z@
}
\makeatother

% to make a mini table by package titletoc 
\newcommand\startmycontents{
\vspace*{3pc}
\startcontents
{\normalsize\bfseries\contentsname\endgraf\vspace{5pt}}
\contentsmargin{1em}
\titlerule
\addvspace{0.3ex}
\printcontents{l}{1}{\setcounter{tocdepth}{1}}
\addvspace{0.2ex}
\titlerule
\addvspace{3ex}
}

\titleformat{\chapter}[display]{\normalfont\bfseries\huge}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titlecontents*{lsection}[4pt]{\small}{\thecontentslabel~}{}{\cdotfill\contentspage}[\endgraf]%

\begin{document}

\frontmatter
\tableofcontents
\chapter*{New features in C++11}

\mainmatter
\chapter{Getting Started}
\startmycontents
This chapter introduces most of the basic elements of C++: types, variables,
expressions, statements, and functions. Along the way, we’ll briefly explain how to
compile and execute a program.


    \section{Writing a Simple C++ Program}

        \subsection{Compiling and Executing Our Program}

    \section{A First Look at Input/Output}\label{a first look at input/output}

    \section{A Word about Comments}


    \section{Flow of Control}

        \subsection{The while Statement}\label{subsec:the while statement}

        \subsection{The for Statement}

    \section{Introducing Classes}   
        \subsection{The Sales\_item Class}\label{subsec:the salesitem class}
            \paragraph{Adding Sales\_items}
        \subsection{A First Look at Member Functions}
    \section{The Bookstore Program}
    \section*{Defined Terms} \markright{Defined Terms} \addcontentsline{toc}{section}{Defined Terms}

\stopcontents
\pagebreak\pagecolor{White}
\backmatter
\chapter{Index}

\end{document}

例如,上面的代码给出了如下部分表: 在此处输入图片描述

然后,如果我将“\paragraph{Adding Sales_items}”移至“编写一个简单的 C++ 程序”部分,代码将给出如下部分表: 在此处输入图片描述

谁能帮我找到对齐页码的方法并告诉我这种现象的原因?

答案1

更换\titlecontents*{lsection}...

\titlecontents{lsection}[4pt]{\small}{\thecontentslabel~}{}{\cdotfill\contentspage}

在此处输入图片描述

请注意,带星号的版本用于将条目分组到单个段落中。

相关内容