0

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}
\addcontentsline{toc}{section}{\nameref{Excercise}}
\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
{
    colorlinks=true, %set true if you want colored links
    linktoc=all,     %set to all if you want both sections and subsections linked
    linkcolor=black,  %choose some color if you want links to stand out
    citecolor=teal,
}
\usepackage{tocloft}
\renewcommand\cftsecpresnum{\S}
\usepackage{titlesec}
\addcontentsline{toc}{section}{\nameref{Excercise}}
\setcounter{secnumdepth}{0}
\titleformat{\section}{\normalfont\Large\bfseries}{\S\thesection}{1em}{}
\begin{document}
    \pagenumbering{roman}
    \tableofcontents
    \hypersetup{linkcolor=blue}
    \newcommand{\link}[2]{{\hyperref[#1]{\underline{\normalfont #2}}}}
    \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}
1

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}}%
  \addcontentsline{toc}{section}{Exercises -- \thechapter} % added by PW
}
\begin{comment}
\hypersetup
{
    colorlinks=true, %set true if you want colored links
    linktoc=all,     %set to all if you want both sections and subsections linked
    linkcolor=black,  %choose some color if you want links to stand out
    citecolor=teal,
}
\end{comment}
\usepackage{tocloft}
\renewcommand\cftsecpresnum{\S}
\usepackage{titlesec}
%\addcontentsline{toc}{section}{\nameref{Excercise}} % PW commented out
\setcounter{secnumdepth}{0}
\titleformat{\section}{\normalfont\Large\bfseries}{\S\thesection}{1em}{}
\begin{document}
    \pagenumbering{roman}
    \tableofcontents
\begin{comment}
    \hypersetup{linkcolor=blue}
    \newcommand{\link}[2]{{\hyperref[#1]{\underline{\normalfont #2}}}}
\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).

|improve this answer|||||
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.