4

Theorem counters can be set to zero automatically on changes of chapter, section, or subsecion, by the second optional argument of \newtheorem (e.g. \newtheorem{foo}{Foo}[section] produces a theorem foo with counter foo that zeros at every change of chapter or section). I'd like to do that without going through a theorem, and to avoid adding the zeroing to the \chapter etc. macros. Is there a way to do so?

  • 1
    Would \numberwithin{foo}{section} -- requires the amsmath package -- be acceptable? That, or \makeatletter \@addtoreset{foo}{section} \makeatother. – Mico Oct 7 '14 at 15:45
  • Yep, perfect. I use amsmath, but only know so little about it :). – MickG Oct 7 '14 at 15:47
4

If I understand well your question,

\newcounter{foo}[chapter]

does what you want. If you want that the chapter number is part of the representation of foo, add

\renewcommand{\thefoo}{\thechapter.\arabic{foo}}

If foo is an already defined counter by a package and you have no control over its definition, add

\usepackage{chngcntr}

and do

\counterwithin{foo}{chapter}

if you want that the representation of foo starts with the chapter number or

\counterwithin*{foo}{chapter}

otherwise.

With \newcounter{foo}[chapter] the counter foo is bound to chapter, which means it's reset each time chapter is stepped (with \stepcounter). There's no record of what counter a given one is bound to. There is, of course, the list of counters bound to a given one. The list is \cl@<counter>, for instance \cl@chapter in the book class expands to

\@elt{section}\@elt{equation}\@elt{figure}\@elt{table}\@elt{footnote}

The \@elt macro is used by \stepcounter to reset the counter \@elt is given as argument. We can use it in various way, for instance to examine the list. The following code will show the status of a counter:

\documentclass{book}

\makeatletter
\newcommand{\showcounter}[1]{%
  \@ifundefined{c@#1}
    {\@latex@error{No counter `#1'}{}}
    {\typeout{`#1' is a defined counter
              with bound counters:}%
     \show@bound@counters{#1}%
     \typeout{===}}%
}
\newcommand{\show@bound@counters}[1]{%
  \begingroup % redefine \@elt in a group
  \def\@elt##1{\typeout{##1}}\@nameuse{cl@#1}%
  \endgroup
}
\makeatother

\showcounter{foo}
\showcounter{chapter}
\newcounter{foo}[section]

\showcounter{section}

If we run it through LaTeX we'll get the following output in the terminal and the log file:

! LaTeX Error: No counter `foo'.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.19 \showcounter{foo}

? 
`chapter' is a defined counter with bound counters:
section
equation
figure
table
footnote
===
`section' is a defined counter with bound counters:
subsection
foo
===
  • 1
    Is there a way to know what a counter is numbered in? – MickG Oct 7 '14 at 16:16
  • Or else: does \counterwithout give an error if I say e.g. \counterwithout{foo}{chapter} and foo is not numbered within chapter? – MickG Oct 7 '14 at 16:25
  • Since it seems the answer to that is "No, it doesn't", I am done here :). – MickG Oct 7 '14 at 19:35
  • @MickG No, you can see what counters are bound to chapter, say, but not whether section is bound to chapter (it is, of course). – egreg Oct 7 '14 at 20:00
5

You have (at least) two options (enter the instructions in the preamble):

  • \numberwithin{foo}{section} (requires the amsmath package).

    Instances of the foo environment will be numbered like "1.1", "1.2", etc.

  • \makeatletter \@addtoreset{foo}{section} \makeatother

    Instances of the foo environment will be numbered like "1", "2", etc. within each section, unless you do something like \renewcommand\thefoo{\thesection.\arabic{foo}}.

  • Suppose I have a counter numbered within section and want it to change and become numbered within chapter. Can I do that? – MickG Oct 7 '14 at 16:06
  • 1
    @MickG - Not sure what a use case for such a change might be. I suspect your readers will be very confused by mid-document changes in the numbering system of a counter. I see that egreg has provided two additional methods in his answer. The second method -- using \counterwithout{foo}{section} followed by \counterwithin{foo}{chapter} -- may meet your needs in this regard. – Mico Oct 7 '14 at 16:26
  • 1
    @MickG - It would be helpful if you stated your main objectives up front in the posting itself, rather than later on in comments to answers. Had I known that you were interested in changing the counter structure mid-document, I would have mentioned immediately the chngcntr package and its \counterwithin and \counterwithout macros, and I would have skipped the \numberwithin and \@addtoreset stuff. – Mico Oct 7 '14 at 19:43
  • Sorry @mico, I probably didn't think about this question long enough before posting it :). Besides, I couldn't have expected a possible way of doing what I asked in the post would not allow changing the within, so I'd have had to notice it with a small document and then post a comment or edit the question, which is what I did, save for the "Yep, perfect." comment. Sorry again. – MickG Oct 7 '14 at 20:44

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.