2

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

enter image description here

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

enter image description here

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.

0

1 Answer 1

3

You use \section* in the redefinition of \chapter, but redefine \section so it no longer has a starred form. So the * is interpreted as the sole argument to \section which then becomes \oldsection{*} etc..

Probably you want \oldsection* in the redefinition of \chapter.

\renewcommand{\chapter}[1]{\oldchapter{#1}%
  \etocsetnexttocdepth{1}%
  \etocsettocstyle{\oldsection*{Local contents}}{}%
  \cftsecindent 0pt
  \localtableofcontents
}

However, if you really need starred forms of the redefined commands somewhere, you would need a different implementation. If you really want to maintain \chapter and \section as-is and simply add the contents listings, which is probably sensible, you might try

\documentclass[12pt]{book}%
\usepackage{tocloft} 
\usepackage{etoc}
    
\let\oldchapter\chapter 
\RenewDocumentCommand \chapter { s +O {#3} +m }
{%
  \IfBooleanTF {#1}{%
    \oldchapter*{#3}%
  }{%
    \oldchapter[#2]{#3}%
    \etocsetnexttocdepth{1}%
    \etocsettocstyle{\section*{Local contents}}{}%
    \cftsecindent 0pt
    \localtableofcontents
  }%
}

\let\oldsection\section
\RenewDocumentCommand \section { s +O {#3} +m }
{%
  \IfBooleanTF {#1}{%
    \oldsection*{#3}%
  }{%
    \oldsection[#2]{#3}%
    \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}

which produces the same typeset result but emulates the behaviour of the original commands more closely.

2
  • Thanks. I did notice that \section* was used setting up the chapter TOC, but because this was done before \let\oldsection\section, I did not think I need to worry about the renaming, since it happens I am done using the original \section command. Commented Jun 23 at 9:19
  • @Nasser \renewcommand doesn't do an \edef, so the new definition is expanded only when \chapter is used, by which point \section has been redefined. Commented Jun 23 at 9:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.