2

I'm building a \documentclass[...]{book} latex file. Each chapter has some sections. Each section has some exercises. The exercises are displayed on \exercise command. In the preamble I did:

\newcommand{\exercise}{
    \paragraph{Exercício x}
}

How can I set x = \chapter . \section . \mycounter?

The \mycounter counter has to reset at beginning of each new section (I don't care about chapter because all exercises will be inside a section).

Thanks in advance.

  • \thechapter.\thesection.\themycounter? or better define \themycounter as \renecommand{\thechapter.\thesection.\arabic{mycounter}} . welcome to tex.se! – Zarko Nov 17 '18 at 19:03
  • @Zarko Under normal circumstances \thesection already prints the chapter number, so \thechapter.\thesection would duplicate the chapter number. – Phelype Oleinik Nov 17 '18 at 19:09
  • @PhelypeOleinik, well, you explain this in your answer. but not knowing anything about op document, it is difficult to say what is usual ... :-). – Zarko Nov 17 '18 at 19:16
  • @Zarko I completely agree :-) – Phelype Oleinik Nov 17 '18 at 19:19
1

You have to define a counter exercise with \newcounter{exercise}, then you say that it restarts at every section with \counterwithin{exercise}{section}.

To make \thesection (which prints the value of the section) to be chapter.section.exercise you can use \thesection.\arabic{exercise} because \thesection is already defined to print chapter.section. Of course you could also use \arabic{chapter}.\arabic{section}.\arabic{exercise}, but then if any of them happened to be, for instance, with roman numbers, it would look inconsistent.

Finally, at each exercise you use \refstepcounter{exercise} to add one to it, and print \theexercise.

enter image description here

\documentclass{book}

\usepackage{lipsum}

\newcounter{exercise}
\counterwithin{exercise}{section}
\renewcommand\theexercise{%
  \thesection.\arabic{exercise}%
}
\newcommand{\exercise}{%
  \refstepcounter{exercise}%
  \paragraph{Exercício~\theexercise}
}

\begin{document}

\chapter{I don't care about}

\section{This section}

\exercise \lipsum[1]

\exercise \lipsum[2]

\section{Another section}

\exercise \lipsum[3]

\end{document}
| improve this answer | |

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.