# Chapter numbers/title in endnotes: spurious titles

This question relates to some existing entries on formatting endnotes to include chapter headings and numbers. The relevant entries are:

Chapter numbers in endnotes

Chapter name appearing in the endnotes

The problem: the endnotes include the final chapter of the document even if it does not have endnotes in it. That is, there are many chapters in a document; some have endnotes, some don't. The very last chapter does not have endnotes. The endnotes, however, always seem to contain the chapter heading for the last chapter.

Here is an example:

\documentclass{scrreprt}

\usepackage{endnotes,chngcntr}

\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\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}
\chapter{The first chapter}
Here text relating to an endnote.\endnote{This endnote contains some text}
\chapter{The next chapter}
Here is another piece of text that needs an endnote.\endnote{This endnote contains more text}
\chapter{The third chapter}
Here is the last bit of text that needs an endnote.\endnote{This endnote contains a whole lot more text}
\chapter{Fourth chapter}
Stuff here
\chapter{Conclusion}
Some more stuff

\newpage

\begingroup
\def\enotesize{\normalsize}
\theendnotes
\endgroup

\end{document}


Thanks for any pointers!

• I think I have solved it but am not sure why the solution works. If you add the line: \addtoendnotes{\unexpanded{\enotedivision{}{}}} Above the call to create the endnotes, it doesn't print the last chapter. – JaM Dec 2 '14 at 21:33

The solution to the problem is actually in the post Chapter numbers in endnotes, which is cited in the question.

The solution is to include one line of code (\addtoendnotes{\unexpanded{\enotedivision{}{}}}) above \theendnotes.

The example above then becomes:

\documentclass{scrreprt}

\usepackage{endnotes,chngcntr}

\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\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}
\chapter{The first chapter}
Here text relating to an endnote.\endnote{This endnote contains some text}
\chapter{The next chapter}
Here is another piece of text that needs an endnote.\endnote{This endnote contains more text}
\chapter{The third chapter}
Here is the last bit of text that needs an endnote.\endnote{This endnote contains a whole lot more text}
\chapter{Fourth chapter}
Stuff here
\chapter{Conclusion}
Some more stuff

\newpage

\begingroup
\def\enotesize{\normalsize}