Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
Put \cleardoublepage before \addcontentsline{toc}{chapter}{Notes} (and also \phantomsection if you're using hyperref). –  egreg Mar 7 '13 at 0:34
    
@egreg: This works in my example. However, I am wondering whether there is a more general method that would allow me in this situation to assign in the TOC any given page number to the Notes (not necessarily increasing it by 1 or by 2). –  Latex user Mar 7 '13 at 0:46
    
\addtocontents{toc}{\protect\contentsline{chapter}{Notes}{42}} But this will break with hyperref. –  egreg Mar 7 '13 at 0:52
    
@egreg: What exactly will break with hyperref if I use this line? And also: what did you mean by "\phantomsection if you're using hyperref" above? –  Latex user Mar 7 '13 at 1:29
    
The annotation on the .toc file written by hyperref is different and depends on the anchor name chosen by it and is not readily predictable. However, why assigning an arbitrary page? The \cleardoublepage\phantomsection\addcontentsline path is safe if hyperref is used. –  egreg Mar 7 '13 at 10:23
show 2 more comments

1 Answer

Put \cleardoublepage before \addcontentsline{toc}{chapter}{Notes} (and also \phantomsection if you're using hyperref).

\cleardoublepage
%\phantomsection % if you're using hyperref, uncomment this line
\addcontentsline{toc}{chapter}{Preface}

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

In order to assign a specific page number, it's best to say

\cleardoublepage
\setcounter{page}{42}
%\phantomsection % if you're using hyperref, uncomment this line
\addcontentsline{toc}{chapter}{Preface}

\chapter*{Preface}
\markboth{PREFACE}{}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.