Chapter name appearing in the endnotes

I have a book document with chapters and endnotes. I want endnotes to be separated by chapter, and I have achieved that, following the answer to my earlier question Chapter numbers in endnotes

However, there are two problems with the document below:

(1) The Preface and the Epilogue, the two chapters that do not have numbers (that is, are introduced with the \chapter* command), do not appear as headings in the Notes (the way "Chapter 1" and other chapters appear).

(2) the very last endnote, instead of having number 1 and appearing under a separate heading "Epilogue" (being the first endnote in the Epilogue), appears with number 2 and is lumped together with the endnotes from Chapter 3.

How to fix these problems?

\documentclass[12pt]{book}

\usepackage{endnotes,chngcntr}

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

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

\usepackage{xparse}

\let\latexchapter\chapter

\RenewDocumentCommand{\chapter}{som}{%
\IfBooleanTF{#1}
{\latexchapter*{#3}}
{\IfNoValueTF{#2}
{\latexchapter{#3}}
{\latexchapter[#2]{#3}}%
\noexpand\enotedivision{\noexpand\subsection}
{\chaptername\ \thechapter. \unexpanded{#3}}}%
}%
}
\makeatletter
\def\enotedivision#1#2{\@ifnextchar\enotedivision{}{#1{#2}}}
\makeatletter

\begin{document}

\setcounter{tocdepth}{1}

\tableofcontents

\chapter*{Preface\markboth{PREFACE}{}}

This is the Prologue.\endnote{There is one endnote here, which should appear under the heading "Prologue" (it doesn't).}

\chapter{Amazing Story}
As I am describing this story, I make a note which should appear at
the end.\endnote{This the first endnote.} And then I make the second
note.\endnote{This is the second note.}

\chapter{Another Amazing Story}
In this chapter, there are no endnotes. So it should not appear in the
"Notes" at the end.

\chapter{Yet Another Amazing Story}
In this chapter, I have some notes again, which I want to go to the
end.\endnote{This note should appear at the end, under the heading
"Chapter 3. Yet Another Amazing Story", with the counter reset to 1,
since this is the first endnote of this chapter.}

\chapter*{Epilogue\markboth{EPILOGUE}{}}

This is the Epilogue.\endnote{One last endnote. It should have number
1 and should appear under the heading Epilogue''. But it
doesn't...}

\cleardoublepage

\theendnotes

\end{document}


You have to add some code for the \chapter* call (and change the \chapter* in the definition of \enoteheading to use the original macro).

\documentclass[12pt]{book}

\usepackage{endnotes,chngcntr}

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

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

\usepackage{xparse}

\RenewDocumentCommand{\chapter}{som}{%
\IfBooleanTF{#1}
{\latexchapter*{#3}%
\setcounter{endnote}{0}%
\noexpand\enotedivision{\noexpand\subsection}
{\unexpanded{#3}}}%
}
{\IfNoValueTF{#2}
{\latexchapter{#3}}
{\latexchapter[#2]{#3}}%
\noexpand\enotedivision{\noexpand\subsection}
{\chaptername\ \thechapter. \unexpanded{#3}}}%
}%
}
\makeatletter
\def\enotedivision#1#2{\@ifnextchar\enotedivision{}{#1{#2}}}
\makeatletter

\begin{document}

\setcounter{tocdepth}{1}

\tableofcontents

\chapter*{Preface\markboth{PREFACE}{}}

This is the Prologue.\endnote{There is one endnote here, which should appear under the heading "Prologue" (it doesn't).}

\chapter{Amazing Story}
As I am describing this story, I make a note which should appear at
the end.\endnote{This the first endnote.} And then I make the second
note.\endnote{This is the second note.}

\chapter{Another Amazing Story}
In this chapter, there are no endnotes. So it should not appear in the
"Notes" at the end.

\chapter{Yet Another Amazing Story}
In this chapter, I have some notes again, which I want to go to the
end.\endnote{This note should appear at the end, under the heading
"Chapter 3. Yet Another Amazing Story", with the counter reset to 1,
since this is the first endnote of this chapter.}

\chapter*{Epilogue\markboth{EPILOGUE}{}}

This is the Epilogue.\endnote{One last endnote. It should have number
1 and should appear under the heading Epilogue''. But it
doesn't...}

\cleardoublepage

\theendnotes

\end{document}


Of course now the text doesn't tell the truth. :)

The solution developed by egreg will get you into trouble when there are endnotes in the chapter heading. The endnotes will then be displayed at the end of the section of the previous chapter, and the chapter heading in the endnotes section will contain the endnote mark, even though it might have a different TOC title without endnote mark.

A solution to this problem is discussed here:

Chapter heading in endnotes: Problems with unnumbered chapters (in KOMA) and endnotes in chapter headings

(It also discusses another problem that occurs if special operations in KOMA classes are used)

In short, the solution for the chapter definition could look like this:

\RenewDocumentCommand{\chapter}{som}{%
\IfBooleanTF{#1} % unnumbered chapters
{\latexchapter*{#3}%
\setcounter{endnote}{0}%
\noexpand\enotedivision{\noexpand\subsection}
{\unexpanded{#3}}}%
}
{ % numbered chapters
\IfNoValueTF{#2}
{ % no separate TOC title