# How to include Exercises in the TOC

in my recent note on Mathematics, I have defined the Exercises as unnumbered sections in the following way:

\newlist{exercise}{enumerate}{5}
\setlist[exercise]{label*=\thechapter.\arabic*.,ref=\thechapter.\arabic*, before={\section*{\hfill{Exercises -- \thechapter}\hfill}}}


The problem is this is not appearing in the table of contents. After a small research, I added the following lines in my codes:

\usepackage{nameref}
\setcounter{secnumdepth}{0}


Which did not solve my problem. How to deal with this? I want the Exercises (with the chapter number) to appear in the TOC at the end of each chapter and the structure of the codes will, of course, be as shown in the MWE below.

\documentclass[openany]{book}
\usepackage{hyperref}
\usepackage{nameref}
\usepackage[inline]{enumitem}
\newlist{exercise}{enumerate}{5}
\setlist[exercise]{label*=\thechapter.\arabic*.,ref=\thechapter.\arabic*, before={\section*{\hfill{Exercises -- \thechapter}\hfill}}}
\hypersetup
{
linktoc=all,     %set to all if you want both sections and subsections linked
citecolor=teal,
}
\usepackage{tocloft}
\renewcommand\cftsecpresnum{\S}
\usepackage{titlesec}
\setcounter{secnumdepth}{0}
\titleformat{\section}{\normalfont\Large\bfseries}{\S\thesection}{1em}{}
\begin{document}
\pagenumbering{roman}
\tableofcontents
\clearpage{\thispagestyle{empty}\cleardoublepage}
\pagenumbering{arabic}
\chapter{Chapter}
\section{Section}
\begin{exercise}
\item Exc. 1
\end{exercise}
\chapter{Chapter}
\begin{exercise}
\item Exc. 2.1
\end{exercise}
\end{document}


This may do what you are after; it is an edited version of your MWE. I have never understood hyperref and have commented out all references to it. In other places I have added new code or commented out your code.

% exercisestocprob.tex  SE 529958 add exercises to ToC

% commented out hyperef stuff which I have never understood
\documentclass[openany]{book}
\usepackage{comment}
%\usepackage{hyperref}
\usepackage{nameref}
\usepackage[inline]{enumitem}
\newlist{exercise}{enumerate}{5}
\setlist[exercise]{label*=\thechapter.\arabic*.,ref=\thechapter.\arabic*,
before={\section*{\hfill{Exercises -- \thechapter}\hfill}}%
}
\begin{comment}
\hypersetup
{
linktoc=all,     %set to all if you want both sections and subsections linked
citecolor=teal,
}
\end{comment}
\usepackage{tocloft}
\renewcommand\cftsecpresnum{\S}
\usepackage{titlesec}
\setcounter{secnumdepth}{0}
\titleformat{\section}{\normalfont\Large\bfseries}{\S\thesection}{1em}{}
\begin{document}
\pagenumbering{roman}
\tableofcontents
\begin{comment}
\end{comment}
\clearpage{\thispagestyle{empty}\cleardoublepage}
\pagenumbering{arabic}
\chapter{Chapter}
\section{Section}
Some sectional text.  % PW added just to see how sections are set
\begin{exercise}
\item Exc. 1
\end{exercise}
\chapter{Chapter}
\begin{exercise}
\item Exc. 2.1
\end{exercise}
\end{document}


Hope that this helps. (I know that you have to be careful about the order in which you call for the hyperref package).

• So far so good. It is working with hyperref as well. I really need that package for various reasons. Thanks much. – Subhajit Paul Feb 25 at 19:37