11

A comment to my answer to Biblatex and citation reset between chapters made me realize that, contrary to normal chapters in the book and report class, \chapter* (the starred version) does not reset the numbering of footnotes:

\documentclass{book}

\begin{document}

\chapter*{Preface}

Some text.\footnote{A footnote}

\chapter*{Introduction}

Some text.\footnote{A footnote}

\chapter{First}

Some text.\footnote{A footnote}

\end{document}

The same is true for \chapter in the frontmatter of the book class:

\documentclass{book}

\begin{document}

\frontmatter

\chapter{Preface}

Some text.\footnote{A footnote}

\chapter{Introduction}

Some text.\footnote{A footnote}

\mainmatter

\chapter{First}

Some text.\footnote{A footnote}

\end{document}

How to reset footnote numbering at \chapter* and at frontmatter chapters?

lockstep
  • 250,273
  • This would probably be easiest by patching the \chapter command. – Stephan Lehmke Apr 27 '12 at 04:17
  • @StephanLehmke I suspected so, but I'm not familiar with patching only the starred version of a command (the non-starred version \chapter works fine). – lockstep Apr 27 '12 at 04:18
  • But it wouldn't harm setting the footnote counter to zero twice in immediate succession, would it? – Stephan Lehmke Apr 27 '12 at 04:21
  • @StephanLehmke Probably not, but I have been surprised by counter behaviour on another occasion: http://tex.stackexchange.com/questions/24822/resetting-theorem-counters-sections-and-missing-subsections – lockstep Apr 27 '12 at 04:24
  • \@makeschapterhead typesets the title of "starred" chapters. See my answer. – Stephan Lehmke Apr 27 '12 at 04:35
  • Look again, edited. As the "reset" is just globally setting to 0, it's really no problem if it's done twice, as long as no footnote can slip between the two resets :-) – Stephan Lehmke Apr 27 '12 at 04:53

3 Answers3

16

I would patch \@chapter and \@schapter to set the footnote counter to zero; the first one will take care of the situation for \chapter* and the second one will take care of chapters created with \chapter under \frontmatter (the chapter counter is not reset under \frontmatter since the definition for \@chapter starts with

\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%

and \frontmatter does

\@mainmatterfalse

So the patching would be something like

\usepackage{etoolbox}

\makeatletter
\pretocmd{\@schapter}{\setcounter{footnote}{0}}{}{}
\pretocmd{\@chapter}{\setcounter{footnote}{0}}{}{}
\makeatother
lockstep
  • 250,273
Gonzalo Medina
  • 505,128
  • Just to be sure: Patching \@chapter will reset the footnote counter twice for mainmatter chapters (by yor patch plus by \refstepcounter{chapter} -- do you think this is insignificant? – lockstep Apr 27 '12 at 04:49
  • 1
    I would use \pretocmd, otherwise there could be a problem with a footnote inside the chapter title. – Stephan Lehmke Apr 27 '12 at 04:51
  • @StephanLehmke: now it's my turn: darn! ;-) – Gonzalo Medina Apr 27 '12 at 04:52
  • @lockstep: No, I think it's not a problem. – Gonzalo Medina Apr 27 '12 at 04:56
  • It was hard to chose among the two answers, especially because both Stefan and you improved the other one's answer. I accepted Stefan's answer beccause its latest version works for memoir (both answers work for KOMA-Script classes). – lockstep Apr 27 '12 at 05:19
  • Dang -- your answer works for memoir, too. So I accepted it instead, because you were the first to tackle the \frontmatter issue. – lockstep Apr 27 '12 at 05:24
  • @GonzaloMedina Why not patching directly \chapter, then? – egreg Apr 28 '12 at 09:07
6

As Gonzalo mentioned, it's not sufficient to only patch the macro to output the "starred" version of the chapterhead. So I suggest to patch \chapter.

\usepackage{etoolbox}

\makeatletter
\patchcmd\chapter{\thispagestyle}{\global\c@footnote\z@\thispagestyle}{}{}
\makeatother
lockstep
  • 250,273
  • +1. Is \global essential, or only a safety precaution? – lockstep Apr 27 '12 at 04:40
  • @lockstep I just modeled it on the way it works in non-starred chapters. But even if it might work without \global in this case, it's never a good idea to handle counters with or without \global inconsistently. I bet David can explain why (something with retaining values). – Stephan Lehmke Apr 27 '12 at 04:43
  • 2
    But this won't solve the problem for \frontmatter. – Gonzalo Medina Apr 27 '12 at 04:44
  • @GonzaloMedina darn, I looked at report ;-) See my edit. – Stephan Lehmke Apr 27 '12 at 04:50
  • Shouldn't one use the xpatch package for patching macros with optional arguments like \chapter? – lockstep Apr 27 '12 at 04:56
  • 1
    @lockstep In this case, the optional argument handling it done by \secdef which comes after my patch. Inserting after \thispagestyle{plain} is really a good place because it comes after \cleardoublepage(no issues with held over material) at a time when global ressources are configured. – Stephan Lehmke Apr 27 '12 at 04:58
  • 1
    Is inserting \global\c@footnote\z@ immediately before \thispagestyle safe? This way, your solution would work with the memoirclass -- its \chapter command uses \thispagestyle{chapter}. – lockstep Apr 27 '12 at 05:09
  • @lockstep Yea, no problem. Good idea. Edited. – Stephan Lehmke Apr 27 '12 at 05:12
  • @StephanLehmke Sorry for accepting and unaccepting -- I should have tested more thouroughly. – lockstep Apr 27 '12 at 05:25
  • @lockstep No problem. The best answer should be the accepted one to serve as a reference for people finding this question later. – Stephan Lehmke Apr 27 '12 at 05:50
  • 1
    @StephanLehmke \setcounter{footnote}{0} is precisely the same as \global\c@footnote\z@. Well, it also checks if the counter is defined, which it is. I'd advocate the LaTeX method. – egreg Apr 28 '12 at 09:13
  • @egreg Good idea in principle, but I have modeled this strictly after \refstepcounter which seemed to be safest, considering the original task. So I think it's a matter of taste in this special case ;-) – Stephan Lehmke Apr 28 '12 at 09:47
6

Although both answers provided so far, solved the problem with the footnote counters, perhaps a better way is to define semantic commands for the additional front matter sections.

This is a much better approach than injecting material into the posterior or anterior of macros, with yet another package.

I have seen sections in front matter as Preface, Foreword, Acknowledgments, Series Editor's Preface, About this Book, List of Contributors, Definitions, Author Affiliations, Abbreviations, Declaration and many others, and that is in English books. These sometimes are also included in the Table of Contents. Some of them are signed and others have a variety of other formatting requirements. For this reason I prefer to set them as environments.

Here is a factory command for making such environments (solving the footnote problem as well) and enforcing consistency all in five lines of code and no need for any package.

\newcommand\forewordname{foreword}
\newcommand\definitionsname{Definitions}
\newcommand\makepreamblecmd[1]{%
    \expandafter\newenvironment\expandafter{#1}{%
    \setcounter{footnote}{0}
    \chapter*{#1}}% could be other formatting macro
{}}

You create the new environments so,

 \makepreamblecmd{\forewordname}
 \makepreamblecmd{\definitionsname}

By using macros such as \forewordname you also cater for languages other than English.

Here is a full minimal,

\documentclass{book}
\makeatletter
\newcommand\forewordname{foreword}
\newcommand\definitionsname{Definitions}
\newcommand\prefacename{Preface}
\newcommand\makepreamblecmd[1]{%
    \expandafter\newenvironment\expandafter{#1}{%
    \setcounter{footnote}{0}
   \chapter*{#1}}%
{}}

\title{test}
\begin{document}
\frontmatter
\maketitle
\tableofcontents
\makepreamblecmd{\forewordname}
\makepreamblecmd{\definitionsname}
\makepreamblecmd{\prefacename}
\begin{foreword}
Some text.\footnote{A footnote}
Some text.\footnote{A footnote}
\end{foreword}
\begin{Definitions}
Some text.\footnote{A footnote}
Some text.\footnote{A footnote}
\end{Definitions}

\begin{Preface}
Some text.\footnote{A footnote}
Some text.\footnote{A footnote}
\end{Preface}

\mainmatter
\chapter{First}
Some text.\footnote{A footnote}
\end{document}

One could modify the "factory method" that generates the environments to add the name to the ToC and to ensure it is hyperlinked, if required.

yannisl
  • 117,160