0

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

  • 1
    Welcome to TeX! You can add a \label in the definition of exercise environment. A complete example is welcome. – muzimuzhi Z Jul 2 '20 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 '20 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 '20 at 4:53
  • Incidentally, it would really help if you provided some information about how the exercise environment is defined. – Mico Jul 2 '20 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? – leandriis Jul 2 '20 at 5:45
1

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
 \@addtoreset{exercise}{section}
 \makeatother

for the resetting.

  • 1
    \counterwithin has been in the LaTeX kernel for a while. – egreg Jul 2 '20 at 8:58
  • Ha! Thanks from an oldie, to whom 2018 is like yesterday. I'll add it to answer, if you don't mind. – Donald Arseneau Jul 2 '20 at 9:37
  • Of course you can! – egreg Jul 2 '20 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.