使用 \addtocontents{toc} 而不使用 \addtocontents{ptc} 添加行和空格

使用 \addtocontents{toc} 而不使用 \addtocontents{ptc} 添加行和空格

我对此有一个后续问题: 想要 \addtocontents{toc} 而不想要 \addtocontents{ptc}

我使用 titletoc 包,想在目录中添加垂直空格和一行,但不要在部分目录中添加。但是,行和空格确实出现在所有部分目录中,如下图红色标记。链接问题中的解决方案对我不起作用,因为我想添加一个对象而不是一个部分。有人知道如何解决这个问题吗?

这是我的 MWE:

\documentclass{article}
\usepackage{titletoc}

\begin{document}
\tableofcontents

\section{Section1}
Here the text of the document begins with Section 1.

\section{Section2}
\startcontents % Want partial TOC for Section2
\printcontents{}{1}{}
Here is the text of Section 2.
\subsection{Subsection2.1}
Here is the text of the first Subsection.
\subsection{Subsection2.2}
Here is the text of the second Subsection.
\stopcontents %Stop the contents for partial TOC

% For some reason I want a line and spaces ONLY in the MAIN TOC, not in the partial TOC
\addtocontents{toc}{\protect\addvspace{10pt} \protect{\hrule height 1.2pt} \protect\addvspace{10pt}}

\section{Section3}
\startcontents % Want partial TOC for Section3
\printcontents{}{1}{}
Here is the text of Section 3.
\subsection{Subsection3.1}
Here is the text of the first Subsection.
\subsection{Subsection3.2}
Here is the text of the second Subsection.
\stopcontents %Stop the contents for partial TOC

\end{document}

在此处输入图片描述

答案1

这是一个解决方案。这个想法是添加\addtocontents{toc}{\protect\myruleandspace}定义\myruleandspace

\newcommand{\myruleandspace}{\addvspace{10pt} \hrule height 1.2pt \addvspace{10pt}}
\tableofcontents
\renewcommand{\myruleandspace}{}

平均能量损失

\documentclass{article}
\usepackage{titletoc}


\begin{document}
\newcommand{\myruleandspace}{\addvspace{10pt} \hrule height 1.2pt \addvspace{10pt}}
\tableofcontents
\renewcommand{\myruleandspace}{}

\section{Section1}
Here the text of the document begins with Section 1.

\section{Section2}
\startcontents % Want partial TOC for Section2
\printcontents{}{1}{}
Here is the text of Section 2.
\subsection{Subsection2.1}
Here is the text of the first Subsection.
\subsection{Subsection2.2}
Here is the text of the second Subsection.
\stopcontents %Stop the contents for partial TOC

% For some reason I want a line and spaces ONLY in the MAIN TOC, not in the partial TOC
\addtocontents{toc}{\protect\myruleandspace}

\section{Section3}
\startcontents % Want partial TOC for Section3
\printcontents{}{1}{}
Here is the text of Section 3.
\subsection{Subsection3.1}
Here is the text of the first Subsection.
\subsection{Subsection3.2}
Here is the text of the second Subsection.
\stopcontents %Stop the contents for partial TOC

\end{document}

相关内容