如何将练习纳入目录

如何将练习纳入目录

在我最近关于数学的笔记中,我Exercises按以下方式将未编号的部分定义为:

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

问题是这没有出现在目录中。经过一番研究,我在代码中添加了以下几行:

\usepackage{nameref}
\addcontentsline{toc}{section}{\nameref{Excercise}}
\setcounter{secnumdepth}{0}

这并没有解决我的问题。如何处理?我想要练习(带有章节编号)出现在每章末尾的目录中,代码的结构当然如下面的 MWE 所示。

\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

这可能就是你想要的;它是你的 MWE 的编辑版本。我从未理解过hyperref,并注释掉了对它的所有引用。在其他地方,我添加了新代码或注释掉了你的代码。

% 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}

希望这会有所帮助。(我知道你必须小心打电话索取包裹的顺序hyperref)。

相关内容