# In a book, how do I automatically number and \label each exercise with three parts (chapter.section.problem)?

In a book with chapters and sections, I would like to number (and \label for possible reference elsewhere) the exercises at the end of each section as, for example, Exercise 1.2.3, for the third exercise at the end of the second section in the first chapter. It needs to be "dynamic" so that everything is automatically adjusted in the event that I add other exercises, move sections or chapters, etc. (I've already \labeled each chapter and each section.)

• Welcome to TeX! You can add a \label in the definition of exercise environment. A complete example is welcome. Jul 2, 2020 at 2:14
• LaTeX's \label-\ref system is fully dynamic, in the sense that a couple of recompile rounds will full update the cross-reference call-outs. If I understand your query correctly, though, you seem to be asking for a rather different type of "dynamic" -- viz., making the argument of a \label instruction (and off all related \ref instructions too!) associated with an exercise environment adjust so that it incorporates the current number of the exercise. If this interpretation is correct, the only sane piece of advice that can be given is, "Don't Even Think About Doing This."
– Mico
Jul 2, 2020 at 4:51
• (continuation of previous comment). If my interpretation is not correct, do please clarify what it is that you're trying to achieve.
– Mico
Jul 2, 2020 at 4:53
• Incidentally, it would really help if you provided some information about how the exercise environment is defined.
– Mico
Jul 2, 2020 at 4:54
• Please add some clarifications to your question. What exactly do you refer to with the term "label" here? Is it the \label command or do you refer to the exercise number? Jul 2, 2020 at 5:45

Assuming the sections are already numbered as chapter.section, you should be able to declare

\counterwithin{exercise}{section}


in the document preamble after whatever package you use to set up exercises. Check that package documentation to see if it offers an option to do this by itself.

You can separately control the display of the number and whether the counter gets reset every section by using two separate declarations

\renewcommand\theexercise{\thechapter.\thesection.\arabic{exercise}}


for the formatting and

 \makeatletter

• \counterwithin has been in the LaTeX kernel for a while. Jul 2, 2020 at 8:58