Adicionando linhas e espaços com \addtocontents{toc} sem \addtocontents{ptc}

Adicionando linhas e espaços com \addtocontents{toc} sem \addtocontents{ptc}

Eu tenho uma pergunta complementar a esta: Quer \addtocontents{toc} sem \addtocontents{ptc}

Eu uso o pacote titletoc e quero adicionar espaços verticais e uma linha no índice, mas NÃO no sumário parcial. No entanto, as linhas e espaços aparecem em todos os índices parciais, conforme marcado em vermelho na imagem abaixo. A solução na pergunta vinculada não funcionou para mim, pois quero adicionar um objeto e não uma seção. Alguém sabe como contornar isso?

Aqui está meu 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}

insira a descrição da imagem aqui

Responder1

Aqui está uma solução. A ideia é adicionar \addtocontents{toc}{\protect\myruleandspace}onde \myruleandspaceé definido com

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

MWE

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

informação relacionada