[Tex/LaTex] Chapter numbers in endnotes

chaptersendnotes

I need to list the endnotes chapter-by-chapter at the end of a book class Latex document. I borrowed this code from

How to reset endnotes counter at parts and chapters

\documentclass[12pt]{book}

\usepackage{endnotes,chngcntr}

\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\latexchapter\chapter

\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}

\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 will appear at the end, under the heading "Yet Another Amazing
Story", with the counter reset to 1, since this is the first endnote of this chapter.}

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

\theendnotes

\end{document}

It's working great for me, EXCEPT that in the endnotes I get just the Chapter titles, but NO CHAPTER NUMBERS. How to I edit this to include Chapter numbers?

Example: right now I get this:

Notes

"Amazing Story"

(list of notes for that chapter)

I want this:

Notes

"Chapter 1. Amazing Story"

(list of notes for that chapter)

Best Answer

Just change the redefinition of \chapter as follows

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

Here's, anyway, the complete example

\documentclass[12pt]{book}

\usepackage{endnotes,chngcntr}

\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\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}

\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 will appear at the end, under the heading "Yet Another Amazing
Story", with the counter reset to 1, since this is the first endnote of this chapter.}

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

\theendnotes

\end{document}

enter image description here

Related Question