使用 \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}

相關內容