4

The answer to this question How to remove chapter numbering without removing it from tableofcontents was given as:

\newcommand{\mychapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
    \addcontentsline{toc}{chapter}{#2}
}

I don't have enough reputation to comment on posts yet so I wanted to ask what is the use of resetting the \setcounter{section}{0} counter line. I am trying to learn LaTeX in a bit more depth so would appreciate if someone could explain what they do. commenting them out of my preamble doesnt seem to make any difference. Does the section counter not refer to the count of the chapter before it and therefore if you've changed that counter it will change automatically?

  • Normally, section counters automatically reset when the chapter is increased – user31729 Apr 12 '15 at 17:23
4

The user answering (How to remove chapter numbering without removing it from tableofcontents) the linked question wants to remove the chapter numbers there, but keep the section numbering 'consistent'.

This can be achieved in the way the user did: Use \chapter* to prevent the chapter counter to be displayed, however, this does not increase or set the chapter counter.

Section counters are (usually) defined via \newcounter{section}[chapter], so each time they are reset, when \refstepcounter{chapter} or \stepcounter comes in action, which is not the case when \chapter*{}is applied (for \chapter it's done so). (\refstepcounter is the labelling-counter-step, calling \stepcounter, which is in turn some special version of \addtocounter{countername}{1}.)

The answerer had to manually set \setcounter{section}{0} in this case, each time \mychapter is used to achieve the specific needs of the OP of the linked question. This way the section numbers are consistent and reset each time if such a specific fake chapter is used

The order of usual hierarchy: (without \part)

  1. Chapter counter get's \stepcountered or \refstepcountered.
  2. Since \newcounter{section}[chapter] was used, this the previous step triggers the resetting of the section counter. The section counter does not depend on a special chapter counter value, it's just the increasing step.

The answer to the question is here: In general, section counters do not require to be reset if not explictly defined, but in the linked question, the section numbering should be consistent and with the method applied in the linked answer, this \setcounter{section}{0} is just one way to do so.

If one bypasses the automatic resetting, one has to pay ;-)

|improve this answer|||||
  • The dependent counters are already reset with \stepcounter. – Heiko Oberdiek Apr 12 '15 at 17:33
  • Ok, so just to check I understand: section counters would normally refer to chapter counters but as chapter was redefined as mychapter in this case they had to be reset manually? Is that correct? How comes when I comment this line out I dont get any errors in my numbering both in document and in the TOC? THanks – PaulBarr Apr 13 '15 at 8:46
  • @PaulBarr: Did you recompile twice. Using the minimal document from the linked answer, there is a continous counting if \setcounter{section}{0} is commented out. – user31729 Apr 13 '15 at 12:10
  • Thats strange, so when I use the minimal document I get the issue you mention but when I use it in the document I am writing I dont get the issue of continuous counting. – PaulBarr Apr 14 '15 at 7:48
  • Perhaps, you should post the 'real' document as well or transmit it via mail (to my address, see my profil, if you prefer to) – user31729 Apr 14 '15 at 11:52
3

The \setcounter{<counter>} doesn't reset the related counters.

For every counter there is an associated list of counters that should be reset when the counter is stepped. Only the \stepcounter{<chapter>} command does the resetting (and \refstepcounter as well, because it calls the basic command).

The shown code could be improved by removing one argument:

\newcommand{\mychapter}[1]{%
  \cleardoublepage
  \refstepcounter{chapter}%
  \addcontentsline{toc}{chapter}{#1}%
  \chapter*{#1}%
}

which would also make it compatible with hyperref, because \refstepcounter also makes an anchor, when hyperref is loaded, which the entry in the bookmark and the link in the TOC will refer to.

If we need chapters to start from zero or other numbers, it's sufficient to add, in the preamble,

\AtBeginDocument{\setcounter{chapter}{-1}}

(or -2 if there are two chapters to take care of).

|improve this answer|||||
  • When I do this code I stop getting the chapter titles in the TOC or in the document, they are replaced with 1,2 etc but no title. – PaulBarr Apr 13 '15 at 8:46

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.