如何将 \cftafterpnum 应用于特定条目(并为目录的列添加标签)

如何将 \cftafterpnum 应用于特定条目(并为目录的列添加标签)

我正在尝试制作一个 3 列目录,其中包括章节名称、页码和用于对家庭作业进行评分的空白行。之前的一个问题给了我如何使用 tocloft 准备这个额外的空白行的答案:

\documentclass[12pt]{report}
\usepackage{tocloft}
\renewcommand{\cftsubsecafterpnum}{\quad\rule{2cm}{0.4pt}}
\renewcommand{\cftsecafterpnum}{\quad\rule{2cm}{0.4pt}}
\setlength{\cftbeforesecskip}{10pt}
\setlength{\cftbeforesubsecskip}{10pt}
\newcommand{\mychapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
    \addcontentsline{toc}{chapter}{#2}
}
\setcounter{secnumdepth}{0}
\begin{document}
    \tableofcontents
    \subsection*{Acknowledgements:}
Problem 1c: Jack Smith helps me with deriving this and that. 
    \clearpage
        \mychapter{1}{Section 1.1 Problems}
            \section{Problem 1}
                \subsection{Part C}
            \section{Problem 1d}
        \mychapter{2}{Section 1.2 Problems}
        \mychapter{3}{Problem A}
        \mychapter{4}{Problem B}
\end{document}

但是,我希望\quad\rule{2cm}{0.4pt}出现在问题 A 和 B 的页码之后,而不是其他章节之后。例如,\cftchapafterpnum如果可能的话,我想应用于特定条目。如果我这样做不方便,请告诉我,特别是如果有办法在每个部分上获得标题,(即标记垂直列“章节”、“页面”和“等级”)。另外,我知道这是一个基本问题,但是如何让章节出现在同一页上?例如,避免在报告类中自动将章节放在不同的页面上?

答案1

由于您正在处理与 ToC 相关的内容,因此您必须非常有策略地管理更改的插入。您的 MWE 中的以下附加内容提供了\addchapsorerule并将\removechapscorerule条件插入 ToC,这些条件将包括/排除章节规则。

在此处输入图片描述

\documentclass[12pt]{report}
\usepackage{tocloft}% http://ctan.org/pkg/tocloft
\newif\ifchaprule
\newcommand{\addchapscorerule}{\addtocontents{toc}{\protect\chapruletrue}}
\newcommand{\removechapscorerule}{\addtocontents{toc}{\protect\chaprulefalse}}
\renewcommand{\cftchapafterpnum}{\ifchaprule\quad\rule{2cm}{0.4pt}\fi}
\renewcommand{\cftsecafterpnum}{\quad\rule{2cm}{0.4pt}}
\renewcommand{\cftsubsecafterpnum}{\quad\rule{2cm}{0.4pt}}
\setlength{\cftbeforesecskip}{10pt}
\setlength{\cftbeforesubsecskip}{10pt}
\newcommand{\mychapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
    \addcontentsline{toc}{chapter}{#2}
}
\setcounter{secnumdepth}{0}
\begin{document}
\tableofcontents
\subsection*{Acknowledgements:}
Problem 1c: Jack Smith helps me with deriving this and that. 

\clearpage

\mychapter{1}{Section 1.1 Problems}
  \section{Problem 1}
    \subsection{Part C}
  \section{Problem 1d}
\mychapter{2}{Section 1.2 Problems}
\addchapscorerule% Start inserting chapter-rules from this point
\mychapter{3}{Problem A}
\removechapscorerule% Remove chapter-rules from this point
\mychapter{4}{Problem B}
\end{document}

答案2

(事实证明我没有时间去学习etoc这种复杂但功能强大的方法。希望其他人能给出答案。)

您可以按照如下方式定义新的“章节”命令:

% need \cftCHAPafterpnum for 'chapters'...
\renewcommand{\cftchapafterpnum}{\quad\rule{2cm}{0.4pt}}
\def\markline{\quad\protect\rule{2cm}{0.4pt}}
\newcommand{\probchapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
%    \addcontentsline{toc}{chapter}{#2}
    \cftaddtitleline{toc}{chapter}{#2}{\thepage\markline}
}

这会将额外的页码后空间切换至仅由 调用的章节\probchapter。完整示例:

\documentclass[12pt]{report}
\usepackage{tocloft}
% \renewcommand{\cftsubsecafterpnum}{\quad\rule{2cm}{0.4pt}} % <-- these are for subsections 
% \renewcommand{\cftsecafterpnum}{\quad\rule{2cm}{0.4pt}}    % <-- and sections
\setlength{\cftbeforesecskip}{10pt}
\setlength{\cftbeforesubsecskip}{10pt}
\newcommand{\mychapter}[2]{
  \protect\marklinetrue
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
    \addcontentsline{toc}{chapter}{#2}
}
% need \cftCHAPafterpnum for 'chapters'...
\renewcommand{\cftchapafterpnum}{\quad\rule{2cm}{0.4pt}}
\def\markline{\quad\protect\rule{2cm}{0.4pt}}
\newcommand{\probchapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
%    \addcontentsline{toc}{chapter}{#2}
    \cftaddtitleline{toc}{chapter}{#2}{\thepage\markline}
}
\setcounter{secnumdepth}{0}
\begin{document}
    \tableofcontents
    \subsection*{Acknowledgements:}
    Problem 1c: Jack Smith helps me with deriving this and that.
    \clearpage
    \mychapter{1}{Section 1.1 Problems}
    \section{Problem 1}
    \subsection{Part C}
    \section{Problem 1d}
    \mychapter{2}{Section 1.2 Problems}
    \probchapter{3}{Problem A}
    \probchapter{4}{Problem B}
\end{document}

至于其他问题,本网站上已经有答案。例如:

相关内容