I have a document with Chapters and endnotes, and a table of contents (TOC). In order not to generate a number for the last chapter (Epilogue), I use \chapter*{Epilogue} in the code and then I put an extra line in the TOC by hand, using the command \addcontentsline{toc}. The problem is that as the result, the page number for the section "Notes" in the TOC appears to be wrong: it is equal to the page number of the Epilogue.
How to change this number (increase it by 2)?
Here is the code (most of it was borrowed from the answer to my earlier question Chapter numbers in endnotes):
\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. There are no endnotes here.
\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. No endnotes here.
\addtoendnotes{\unexpanded{\enotedivision{}{}}}
\addcontentsline{toc}{chapter}{Notes}
\theendnotes
\end{document}
\cleardoublepagebefore\addcontentsline{toc}{chapter}{Notes}(and also\phantomsectionif you're usinghyperref). – egreg Mar 07 '13 at 00:34\addtocontents{toc}{\protect\contentsline{chapter}{Notes}{42}}But this will break withhyperref. – egreg Mar 07 '13 at 00:52.tocfile written byhyperrefis different and depends on the anchor name chosen by it and is not readily predictable. However, why assigning an arbitrary page? The\cleardoublepage\phantomsection\addcontentslinepath is safe ifhyperrefis used. – egreg Mar 07 '13 at 10:23