Currently, to make local TOC for each chapter and each section, I manually type some code just below each chapter and section title.
I wanted to automate this using \renewcommand so that the code to generate the local TOC is added automatically. This example below shows it is not working well. I get local TOC for sections, but for some reason, the chapter TOC do not show.
Will first show the MWE where code to add TOC is added manually. Then show my attempt to automate this. This is the manual code
\documentclass[12pt]{book}%
\usepackage{tocloft}
\usepackage{etoc}
\begin{document}
\chapter{chapter one}
\etocsetnexttocdepth{1}
\etocsettocstyle{\section*{Local contents}}{}
\cftsecindent 0pt
\localtableofcontents
\section{section 1}
\etocsetnexttocdepth{2}
\etocsettocstyle{\subsection*{\hspace*{20pt}Local contents}}{}
\cftsubsecindent 20pt
\localtableofcontents
\subsection{subsection 1}
etc..
\subsection{subsection 2}
etc..
\section{section 2}
etc..
\end{document}
Compiling the above using lualatex, in TL 2025 on Linux, gives what is expected. Local TOC for each chapter and each section
The example below tries to automate this using \renewcommand
\documentclass[12pt]{book}%
\usepackage{tocloft}
\usepackage{etoc}
\let\oldchapter\chapter
\renewcommand{\chapter}[1]{\oldchapter{#1}%
\etocsetnexttocdepth{1}
\etocsettocstyle{\section*{Local contents}}{}
\cftsecindent 0pt
\localtableofcontents
}
\let\oldsection\section
\renewcommand{\section}[1]{\oldsection{#1}%
\etocsetnexttocdepth{2}
\etocsettocstyle{\subsection*{\hspace*{20pt}Local contents}}{}
\cftsubsecindent 20pt
\localtableofcontents
}
\begin{document}
\chapter{chapter one}
\section{section 1}
etc...
\subsection{subsection 1}
etc..
\subsection{subsection 2}
etc..
\section{section 2}
etc..
\subsection{subsection 1}
etc..
\subsection{subsection 2}
etc..
\end{document}
Compiling the above using lualatex (few times) gives this
I am using the same code to add local TOC for chapter and section. But it seems due to using the code inside \renewcommand causes a problem.
Is there a way to correct the above or better way to do the above other than \renewcommand?
I do not want to change the \section command to new command such as \mysection or such. I want to keep the \section and \chapter command as is, just insert local TOC automatically for each.

