I have a follow-up question to this one: Want \addtocontents{toc} without \addtocontents{ptc}

I use the titletoc package and want to add vertical spaces and a line in the table of contents, but NOT in the partial TOC. However, the lines and spaces do appear in all partial TOCs as marked in red in the picture below. The solution in the linked question did not work for me, because I want to add an object and not a section. Does anybody know how to circumvent this?

Here is my 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

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


Here is a solution. The idea is to add \addtocontents{toc}{\protect\myruleandspace} were \myruleandspace is defined with

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


MWE

\documentclass{article}
\usepackage{titletoc}

\begin{document}
\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

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

• Perfect! Thank you very much. However, I still don't understand why it works with defining a new command. – Juliane Jul 7 '17 at 13:05