# Using Koma-script for defining ToC for each chapter

I try to use solution suggested by @esddd in the question "Replacement minitoc package style by KOMA-script possibilities", but unfortunately I came across the following problem:

As you can see above, in the Toc defined for chapter, is added also entry for following Part. How the above entry for Part can be suppressed in the MNWE code? See MNWE:

MNWE

 \documentclass[fontsize=10pt,open=any,twocolumn]{scrbook}[2019/10/12]% needs version 3.27 or newer
\usepackage{blindtext}
\usepackage{scrwfile}% avoid trouble with the limited number of write handles
\usepackage{xpatch}

% https://tex.stackexchange.com/a/359758
% https://tex.stackexchange.com/a/502077
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
listname=My Content,
%unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
\value{tocdepth}=\chaptertocdepth% we want entries down to chapter
\listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part

\newif\ifusechaptertoc% Switch to tell \addtocentrydefault to not only make entries to the toc-file but also to the current section-toc-file
\newcommand*{\chaptertoc}[2][\thechapter]{% new command to generate and show a chapter toc
\usechaptertoctrue% switch on chapter-toc-entries
\edef\ext@chaptoc{tcc#1}% extension of the chapter-toc-file, e.g., tcc1
\DeclareNewTOC{\ext@chaptoc}% declare a new toc file
\setchapterpreamble{%
\begin{minipage}{\linewidth}
\hrulefill\par
\value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
\listoftoc*{\ext@chaptoc}% show the toc without header
\end{minipage}%
\par\bigskip\nobreak\noindent\hrulefill\par
\bigskip\noindent\ignorespaces% add some vertical space after the toc and do not indent the following text
}%
}
% \xpretocmd{⟨command⟩}{⟨prepend⟩}{⟨success⟩}{⟨failure⟩}
% \xapptocmd{⟨command⟩}{⟨append⟩}{⟨success⟩}{⟨failure⟩}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifusechaptertoc% if chapter toc entries should be generated
\ifstr{#1}{chapter}{}
\fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% automatically switch of chapter toc entries at start of every \chapter
\makeatother

\newcommand\setchaptertoc[1][]{%
\ifstr{#1}{}
}

\setcounter{tocdepth}{\partnumdepth}% depth of TOC
\begin{document}
\tableofcontents

\part{My Part I}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext

\part{My Part II}
\parttoc
\setchaptertoc
\chapter{My chapter II}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext
\end{document}


Code is well documented, but I still don’t understand why it’s working like that.

There was \xpretocmd\part{\usechaptertocfalse}{}{} missing in the other answer.

\documentclass[fontsize=10pt,open=any,twocolumn]{scrbook}[2019/10/12]% needs version 3.27 or newer
\providecommand*{\Ifstr}{\ifstr}% added because of changes in upcoming KOMA-Script version 3.28 and \ifstr replaced by \Ifstr in the code (see https://komascript.de/faq_deprecatedif)
\usepackage{blindtext}
\usepackage{scrwfile}% avoid trouble with the limited number of write handles
\usepackage{xpatch}

% https://tex.stackexchange.com/a/359758
% https://tex.stackexchange.com/a/502077
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
listname=My Content,
%unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
\value{tocdepth}=\chaptertocdepth% we want entries down to chapter
\listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part

\newif\ifusechaptertoc% Switch to tell \addtocentrydefault to not only make entries to the toc-file but also to the current section-toc-file
\newcommand*{\chaptertoc}[2][\thechapter]{% new command to generate and show a chapter toc
\usechaptertoctrue% switch on chapter-toc-entries
\edef\ext@chaptoc{tcc#1}% extension of the chapter-toc-file, e.g., tcc1
\DeclareNewTOC{\ext@chaptoc}% declare a new toc file
\setchapterpreamble{%
\begin{minipage}{\linewidth}
\hrulefill\par
\value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
\listoftoc*{\ext@chaptoc}% show the toc without header
\end{minipage}%
\par\bigskip\nobreak\noindent\hrulefill\par
\bigskip\noindent\ignorespaces% add some vertical space after the toc and do not indent the following text
}%
}
% \xpretocmd{command)}{(prepend⟩}{⟨success⟩}{⟨failure⟩}
% \xapptocmd{⟨command⟩}{⟨append⟩}{⟨success⟩}{⟨failure⟩}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifusechaptertoc% if chapter toc entries should be generated
\Ifstr{#1}{chapter}{}
\fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \chapter
\xpretocmd\part{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \part
\makeatother

\newcommand\setchaptertoc[1][]{%
\Ifstr{#1}{}
}

\setcounter{tocdepth}{\partnumdepth}% depth of TOC
\begin{document}
\tableofcontents

\part{My Part I}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext

\part{My Part II}
\parttoc
\setchaptertoc
\chapter{My chapter II}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext
\end{document}