# How to reset endnotes counter at parts and chapters

I have a document (book class, see condensed working-example below) in the book class, in which I've got some endnotes (extra notes that get saved until the end of the document as opposed to printed at the bottom of the current or nearest right-hand page). I also have footnotes, which I want to remain independent; I've achieved this, and have them resetting every page, and have the endnotes resetting every new chapter or part.

Now, when I call \theendnotes I get a list of all the endnotes in the document in order, with the correct numberings (i.e. having reset the counter every chapter/part). I would like the part/chapter divisions displayed as section/subsection headings respectively, and without these interfering with my table of contents.

I'm working with the endnotes package just now, but open to other ideas if they'd solve my problem without me getting my hands too dirty (I'm no pro when it comes to LaTeX hackery). The reason I have both footnotes and endnotes is that I'm translating an old academic text, and I need to be [reasonably] faithful to the original formatting. Working example below:

\documentclass{book}
\usepackage{endnotes}
\makeatletter
\@addtoreset{footnote}{page}    % Reset footnote "numbering" (symbols) every new page
\@addtoreset{endnote}{part}     % Reset endnote numbering every new part
\@addtoreset{endnote}{chapter}  % Reset endnote numbering every new chapter
\makeatother
\usepackage{footmisc}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}%
\begin{document}
Some text,\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
not in any structural part of the document.
\part{Part One}
Some\footnote{This is the first footnote, symbolised by a star.}
text,\footnote{Second footnote, a pair of stars.}
not\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
part\footnote{Third footnote, a solitary dagger.}
of any chapter.
\chapter{Chapter 1}
This\footnote{A footnote, should be a single star (symbol \#1), not a pair of daggers (\#4) because of reset footnote numbering at new page.}
is chapter one.\endnote{This endnote should be numbered 1, since it's the first of a chapter.}
\part{Part Two}
Text.\endnote{This should also be numbered 1, since it's the first in its part.}
\theendnotes
\end{document}

-
I've had a look at the pagenotes package, which seems to address a lot of the issues, but falls at the last hurdle with respect to its behaviour respecting \parts; if we have some text falling before any of the \chapter invocations in its part then it's in "chapter n-1" where n is the number of the first chapter in that \part. –  Nick Loughlin Jul 6 '12 at 16:48

Here's an automatic version:

\documentclass{book}
\usepackage{endnotes,chngcntr}
\usepackage[perpage,symbol*]{footmisc}

\counterwithin*{endnote}{part}     % Reset endnote numbering every new part
\counterwithin*{endnote}{chapter}  % Reset endnote numbering every new chapter

\makeatletter
\setcounter{secnumdepth}{-2}
\chapter*{\notesname}
\mbox{}\par\vskip-\baselineskip
\let\@afterindentfalse\@afterindenttrue
}
\makeatother

\usepackage{xparse}
\let\latexpart\part
\let\latexchapter\chapter

\RenewDocumentCommand{\part}{som}{%
\IfBooleanTF{#1}
{\latexpart*{#3}}
{\IfNoValueTF{#2}
{\latexpart{#3}}
{\latexpart[#2]{#3}}%
}
}
\RenewDocumentCommand{\chapter}{som}{%
\IfBooleanTF{#1}
{\latexchapter*{#3}}
{\IfNoValueTF{#2}
{\latexchapter{#3}}
{\latexchapter[#2]{#3}}%
}
}

\begin{document}
Some text,\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
not in any structural part of the document.

\part{Part One}
Some\footnote{This is the first footnote, symbolised by a star.}
text,\footnote{Second footnote, a pair of stars.}
not\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
part\footnote{Third footnote, a solitary dagger.}
of any chapter.

\chapter{Chapter 1}

This\footnote{A footnote, should be a single star (symbol \#1), not a pair of daggers (\#4)
because of reset footnote numbering at new page.}
is chapter one.\endnote{This endnote should be numbered 1, since it's the first of a chapter.}

\part{Part Two}
Text.\endnote{This should also be numbered 1, since it's the first in its part.}

\theendnotes

\end{document}


First of all I've said \usepackage[perpage,symbol*]{footmisc} to get footnotes numbered per page. Consult the documentation for how to change the order of symbols. I also changed a bit the \enoteheading command.

The substantial part is the redefinition of \part and \chapter to write their titles in the endnotes file. The original meaning is saved and xparse is used to easily redefine them.

Here's a slightly different version that avoids printing a chapter (subsection) header in the endnotes if there's no endnote in it. Part (section) headers are always printed.

\documentclass{book}
\usepackage{endnotes,chngcntr}
\usepackage[perpage,symbol*]{footmisc}

\counterwithin*{endnote}{part}     % Reset endnote numbering every new part
\counterwithin*{endnote}{chapter}  % Reset endnote numbering every new chapter

\makeatletter
\setcounter{secnumdepth}{-2}
\chapter*{\notesname}
\mbox{}\par\vskip-\baselineskip
\let\@afterindentfalse\@afterindenttrue
}
\makeatother

\usepackage{xparse}
\let\latexpart\part
\let\latexchapter\chapter

\RenewDocumentCommand{\part}{som}{%
\IfBooleanTF{#1}
{\latexpart*{#3}}
{\IfNoValueTF{#2}
{\latexpart{#3}}
{\latexpart[#2]{#3}}%
}
}
\RenewDocumentCommand{\chapter}{som}{%
\IfBooleanTF{#1}
{\latexchapter*{#3}}
{\IfNoValueTF{#2}
{\latexchapter{#3}}
{\latexchapter[#2]{#3}}%
}
}
\makeatletter
\def\enotedivision#1#2{\@ifnextchar\enotedivision{}{#1{#2}}}
\makeatletter

\begin{document}
Some text,\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
not in any structural part of the document.

\part{Part One}
Some\footnote{This is the first footnote, symbolised by a star.}
text,\footnote{Second footnote, a pair of stars.}
not\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
part\footnote{Third footnote, a solitary dagger.}
of any chapter.

\chapter{Chapter 1}
This\footnote{A footnote, should be a single star (symbol \#1), not a pair of daggers (\#4)
because of reset footnote numbering at new page.}
is chapter one.\endnote{This endnote should be numbered 1, since it's the first of a chapter.}

\chapter{Chapter 2}
No endnotes

\part{Part Two}
Text.\endnote{This should also be numbered 1, since it's the first in its part.}

\chapter{Chapter 3}
No endnotes.

\theendnotes

\end{document}

-
It would take a little more work to automate not including a sectional heading in \theendnotes if that particular part/chapter has no \endnote. –  Werner Jul 6 '12 at 17:20
@Werner Yes, but I've some ideas. However in works of this kind it's quite difficult that no footnote appear. –  egreg Jul 6 '12 at 17:22
Agreed. "Some ideas" may include compiling more than once, of course. –  Werner Jul 6 '12 at 17:27
@Werner I don't think so. :) –  egreg Jul 6 '12 at 17:30
I'm intrigued.. –  Werner Jul 6 '12 at 17:39

Here is a first attempt at inserting the information manually. I'm sure it can be automated, but that would require making sure that you have end notes in each of the parts/chapters that you use so as to avoid outputting sectional headings without any end notes in them:

\documentclass{book}
\usepackage{endnotes}% http://ctan.org/pkg/endnotes
\makeatletter
\@addtoreset{footnote}{page}    % Reset footnote "numbering" (symbols) every new page
\@addtoreset{endnote}{part}     % Reset endnote numbering every new part
\@addtoreset{endnote}{chapter}  % Reset endnote numbering every new chapter
\makeatother
\usepackage{footmisc}% http://ctan.org/pkg/footmisc
\renewcommand{\thefootnote}{\fnsymbol{footnote}}%
\begin{document}
Some text,\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
not in any structural part of the document.
\part{Part One}
\makeatletter
\makeatother
Some\footnote{This is the first footnote, symbolised by a star.}
text,\footnote{Second footnote, a pair of stars.}
not\endnote{This endnote will naturally be numbered 1, as the first endnote in the document.}
part\footnote{Third footnote, a solitary dagger.}
of any chapter.
\chapter{First chapter}
\makeatletter
\makeatother
This\footnote{A footnote, should be a single star (symbol \#1), not a pair of daggers (\#4) because of reset footnote numbering at new page.}
is chapter one.\endnote{This endnote should be numbered 1, since it's the first of a chapter.}
\part{Part Two}
\makeatletter
\makeatother
Text.\endnote{This should also be numbered 1, since it's the first in its part.}
\theendnotes
\end{document}


endnotes provides \addtoendnotes{<stuff>} which writes to <jobname>.ent - the end notes file. Fragile commands inside <stuff> need to be \protected:

\makeatletter

I've used \section* for the \part breakdown, and \subsection* for the \chapter breakdown, since starred versions don't end up in the ToC (by default). The setting of \@afterindenttrue can be done globally for the end notes, but I kept it local to each insertion for now (for one, setting it globally would remove the \makeatletter ... \makeatother requirement).