[Tex/LaTex] How to reset endnotes counter at parts and chapters

endnotesfootnotes

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}
    \renewcommand\enoteheading{\chapter*{\notesname}\mbox{}\par\vskip-\baselineskip}
    \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}

Best Answer

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
\renewcommand\enoteheading{%
  \setcounter{secnumdepth}{-2}
  \chapter*{\notesname}
  \addtocontents{toc}{\protect\addvspace{10pt}} % adjust to suit
  \addcontentsline{toc}{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}}%
     \addtoendnotes{\unexpanded{\section{#3}}}
    }
}
\RenewDocumentCommand{\chapter}{som}{%
  \IfBooleanTF{#1}
    {\latexchapter*{#3}}
    {\IfNoValueTF{#2}
       {\latexchapter{#3}}
       {\latexchapter[#2]{#3}}%
     \addtoendnotes{\unexpanded{\subsection{#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
\renewcommand\enoteheading{%
  \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}}%
     \addtoendnotes{\unexpanded{\enotedivision{\section}{#3}}\relax}
    }
}
\RenewDocumentCommand{\chapter}{som}{%
  \IfBooleanTF{#1}
    {\latexchapter*{#3}}
    {\IfNoValueTF{#2}
       {\latexchapter{#3}}
       {\latexchapter[#2]{#3}}%
     \addtoendnotes{\unexpanded{\enotedivision{\subsection}{#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.


\addtoendnotes{\unexpanded{\enotedivision{}{}}}
\theendnotes

\end{document}
Related Question