In LaTeX’s book class, how to define a custom counter for Exercises that is tied to the current chapter (e.g., Exercise 1.1, 1.2 in Chapter 1, Exercise 2.1 in Chapter 2, etc.), and also create a separate List of Exercises (similar to a Table of Contents)?
It seemly consists two questions:
Noted that, one post should just focused on just one core point. BTW, you'd better provide an MWE, which make it convenient for users on the site to test and start with.
For the first one, you may have a look at chngcntr
package(which has been in the 2e-kernel)
For the second one, maybe you can give a glance on tocbasic
package in koma-script bundle.
此处用tocbasic
似乎不是非常合适,因为这需要exercise
成为浮动体。
最好是模仿\section
里的\@starttoc
功能做类似的实现。
Exactly! The first requirement has been meet. The second question remains unresolved. Please see the MWE below for more details.
\documentclass{book}
% Implementation for the first requirement.
% It do work but I don't know if it is a good practice.
\newcounter{exercise}[chapter]
\renewcommand{\theexercise}{\noindent\textbf{Exercise \thechapter.\arabic{exercise}}\quad}
\newcommand{\ex}{\addtocounter{exercise}{1}\theexercise}
\begin{document}
\chapter{Some basic mathematics}
\chapter{Differentiable manifolds and tensors}
\section{Definition of a manifold}
\ex $a^{2}+b^{2}=c^{2}$
\ex $\exp(\mathrm{i}\pi)=-1$
\section{The sphere as a manifold}
\ex $\cos^{2}\theta + \sin^{2}\theta = 1$
\ex $E=\hbar \omega$
\end{document}
This is a MWE.
\documentclass{book}
\usepackage{lipsum}
% -------------------------------
% Counter
\newcounter{exercise}[chapter]
\renewcommand{\theexercise}{\thechapter.\arabic{exercise}}
% -------------------------------
% Exercises environment (list-based)
\newenvironment{exercises}
{%
\par\vspace{\baselineskip}%
\noindent\rule{\textwidth}{0.8pt}\par%
\vspace{0.5\baselineskip}%
\begin{list}{}{%
\setlength{\leftmargin}{0pt}%
\setlength{\itemindent}{0pt}%
\setlength{\listparindent}{0pt}%
\setlength{\labelwidth}{0pt}%
\setlength{\labelsep}{0pt}%
\setlength{\itemsep}{1.5\baselineskip}%
\setlength{\parsep}{0pt}%
\setlength{\topsep}{0pt}%
}%
}
{%
\end{list}%
\vspace{0.5\baselineskip}%
\noindent\rule{\textwidth}{0.8pt}\par%
\vspace{\baselineskip}%
}
% -------------------------------
% List of Exercises machinery
\newcommand{\listexercisename}{List of Exercises}
\makeatletter
\newcommand{\listofexercises}{%
\chapter*{\listexercisename}%
\@starttoc{loe}%
}
\newcommand\l@exercise[2]{%
\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}
\makeatother
% -------------------------------
% Patch \item to also add to .loe
\let\olditem\item
\renewcommand{\item}[1][]{%
\refstepcounter{exercise}%
\addcontentsline{loe}{exercise}{Exercise~\theexercise\ifx\relax#1\relax\else: #1\fi}%
\olditem[\textbf{Exercise \theexercise}\quad]%
}
\begin{document}
\tableofcontents
\listoftables
\listofexercises
\chapter{Some basic mathematics}
\begin{table}
\centering
\begin{tabular}{c|c}
\hline\hline
(1,1) & (1,2) \\
\hline
(2,1) & (2,2) \\
\hline\hline
\end{tabular}
\caption{This is a table.}\label{tbl:1}
\end{table}
\chapter{Differentiable manifolds and tensors}
\section{Definition of a manifold}
\begin{exercises}
\item \lipsum[2][1-3] $a^{2}+b^{2}=c^{2}$
\begin{equation}
a^{2}+b^{2}=c^{2}
\end{equation}
\item \lipsum[2][1-3] $\exp(\mathrm{i}\pi)=-1$
\begin{equation}
\exp(\mathrm{i}\pi)=-1
\end{equation}
\item \lipsum[2][1-3] $\cos^{2}\theta + \sin^{2}\theta = 1$
\begin{equation}
\cos^{2}\theta + \sin^{2}\theta = 1
\end{equation}
\end{exercises}
\section{The sphere as a manifold}
\begin{exercises}
\item \lipsum[2][1-3] $E=\hbar \omega$
\begin{equation}
E=\hbar \omega
\end{equation}
\end{exercises}
\end{document}
Exactly! The first requirement has been meet. The second question remains unresolved. Please see the MWE below for more details.