I'm writing a book in which each chapter ends with a list of numbered exercises. I would like to persuade LaTeX to count the total number of exercises in the book, to be referred to with \ref or something like it (e.g., \total from totcount). Each chapter is loaded separately using \include, and I want the counts to be correct even when using \includeonly.
It looks like the totcount package might help, provided I somehow jigger the exercise environments to step a suitable counter. But I'm not quite clear on how I can do that in a way that is compatible with the \suspend and \resume commands from package mdwlist.
Any suggestions how I would define an exercises environment that would work with totcount and be compatible with \suspend and \resume? Or suggest another approach entirely?
Here's an MWE in which each exercise is correctly numbered: exercises in chapter 1 are numbered 1 to 4, as are exercises in chapter 2. The problem I'm trying to solve is to accumulate the correct total (8) in the preface.
\documentclass{book}
\usepackage{totcount}
\usepackage{mdwlist}
\newtotcounter{exercises}
\newenvironment{exercises}
{\enumerate} % need something here to count each exercise
{\endenumerate}
\begin{document}
\section*{Preface}
There are \total{exercises} exercises. (Should be~8.)
% this chapter meant to be in a separate file loaded with \include
\chapter{Diet}
\section{Omnivorous}
\begin{exercises}
\item
Which do you prefer?
\begin{enumerate}
\item
Fish
\item
Fowl
\end{enumerate}
\item
What won't you touch?
\begin{enumerate}
\item
Snakes
\item
Snails
\end{enumerate}
\suspend{exercises}
\section{Vegetarian}
\resume{exercises}
\item
Which do you prefer?
\begin{enumerate}
\item
Corn
\item
Squash
\end{enumerate}
\item
What won't you touch?
\begin{enumerate}
\item
Broccoli
\item
Cilantro
\end{enumerate}
\end{exercises}
% this chapter meant to be in a separate file loaded with \include
\chapter{Technology}
\section{Typesetting}
\begin{exercises}
\item
Which do you prefer?
\begin{enumerate}
\item
Plain \TeX
\item
\LaTeX
\end{enumerate}
\item
What won't you touch?
\begin{enumerate}
\item
Word
\item
Scribe
\end{enumerate}
\suspend{exercises}
\section{Operating systems}
\resume{exercises}
\item
Which do you prefer?
\begin{enumerate}
\item
Debian
\item
Arch
\end{enumerate}
\item
What won't you touch?
\begin{enumerate}
\item
BeOS
\item
NeXT
\end{enumerate}
\end{exercises}
\end{document}

\suspendand\resumeor are these just commands that you think might help? Please add either a MWE or a mock-up of the output you want. – Aug 05 '20 at 00:51mdwlist: last version dated 1996. – egreg Aug 27 '23 at 11:07