0

I explain my problem: I am writing a book and I am using \newcounter to number the examples. At the moment I am creating an enviroment like this

\newcounter{xmpl}[chapter]
\newenvironment{example}{
     \def\FrameCommand{\colorbox{colxmplboxcolor}}%
     \MakeFramed{\advance\hsize-\width \FrameRestore}
     \vspace{0.2cm} \noindent \refstepcounter{xmpl} \textbf{Example  \thexmpl \\ }
}
{\vspace{0.2cm}\par\noindent%
    \ignorespacesafterend
    \endMakeFramed}
\definecolor{colxmplboxcolor}{HTML}{F8E0E0}

In this way newcounter will reset at any chapter, but what if I want the number of the chapter inside the number of the example? For instance:

Second example of chapter 4

Example 4.2

6
  • 3
    Before you get too far have you checked the options from the tcolorbox package. It can straight up do what you are doing by hand right here, with much better interfaces.
    – daleif
    Mar 23, 2020 at 11:43
  • But colorbox is for the background color, why should it be useful in numbering? Mar 23, 2020 at 11:52
  • 1
    Everything you do here, including the background, counters etc can be made using tcolorbox via a key=value interface. See section 17 in the tcolorbox manual. It is a wast of time trying to reinvent the wheel.
    – daleif
    Mar 23, 2020 at 11:54
  • Otherwise just redefine \thexmpl (I would have sworn that \newcounter did that automatically)
    – daleif
    Mar 23, 2020 at 11:57
  • 1
    I've read the manual of tcolorbox. I didn't know the existance of the package. It helps a lot and is way better. Thanks Mar 23, 2020 at 13:27

1 Answer 1

1

You can simply use commands from the LaTeX kernel:

\newcounter{xmpl}
\counterwithin{xmpl}{chapter}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .