[Tex/LaTex] Chapter name appearing in the endnotes

chaptersendnotes

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
\renewcommand\enoteheading{%
\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}}%
 \addtoendnotes{%
   \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}{}}

\addcontentsline{toc}{chapter}{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}{}}

\addcontentsline{toc}{chapter}{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...}

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

\cleardoublepage

\addcontentsline{toc}{chapter}{Notes}

\theendnotes

\end{document}

Best Answer

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
\renewcommand\enoteheading{%
  \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}%
   \addtoendnotes{%
     \noexpand\enotedivision{\noexpand\subsection}
       {\unexpanded{#3}}}%
  }
  {\IfNoValueTF{#2}
     {\latexchapter{#3}}
     {\latexchapter[#2]{#3}}%
   \addtoendnotes{%
     \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}{}}

\addcontentsline{toc}{chapter}{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}{}}

\addcontentsline{toc}{chapter}{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...}

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

\cleardoublepage

\addcontentsline{toc}{chapter}{Notes}

\theendnotes

\end{document}

enter image description here

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

Related Question