5 How to define a custom counter for Exercises? 5天18时49分32秒

发布于 2025-09-08 21:32:41

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)?

查看更多

关注者
0
被浏览
121
3 个回答
Sagittarius Rover
这家伙很懒,什么也没写!

It seemly consists two questions:

  • define a custom counter for Exercises that is tied to the current chapter
  • create a separate List of Exercises (similar to a Table of Contents)

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功能做类似的实现。

追风少年
追风少年 1天前
这家伙很懒,什么也没写!

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} 
追风少年
追风少年 1天前
这家伙很懒,什么也没写!

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}

image.png

撰写答案 当前问题悬赏金额为¥5 元,被提问者采纳后将获得赏金

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览