% \iffalse meta-comment
% !TEX TS-program = dtxmk 
%
% Copyright (C) 2005 by John Burt <burt@brandeis.edu>
% --------------------------------------------------
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1997/12/01]
%<package>\ProvidesPackage{endheads}
%<package>   [2013/10/14 v1.55 endheads file] 
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{makeidx,multicol,keyval,ifthen}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{endheads.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{514}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% 
% \changes{v1.0}{2005/08/21}{Initial version}
% \changes{v1.1}{2005/09/25}{uses needspace prevent orphan chapter 
% beginnings in notes}
% \changes{v1.15}{2005/09/25}{uses titleref to capture chapter titles for 
% notes}
% \changes{v1.151}{2005/09/25}{Corrections to user's guide}
% \changes{v1.152}{2006/11/10}{Fixed strange white space error}
% \changes{v1.2}{2006/11/10}{Added commands for multilingual use}
% \changes{v1.3}{2007/12/12}{Added commands for multilingual use}
% \changes{v1.4}{2008/04/12}{Added compatibility with memoir class. Added
% commands for multilingual use}
%\changes{v1.45}{2008/04/14}{Corrected problem with putting entry for 
%endnotes in table of contents}
%\changes{v1.5}{2011/12/29}{Corrected problem with page styles for 
%first pages of chapters}
%\changes{v1.51}{2012/09/03}{Corrected problem with page page headings in memoir style} 
%\changes{v1.55}{2013/10/13}{Corrected conflict with biblatex} 
% \GetFileInfo{endheads.sty}
% 
% \DoNotIndex{\newcommand,\newenvironment,\def,\edef,\else,\renewcommand,\makeatletter,\makeatother}
% \DoNotIndex{\abovedisplayskip,\belowdisplayskip,\abovedisplayshortskip,\belowdisplayshortskip}
% \DoNotIndex{\newcommand, \section, \subsection, \subsubsection,
% \fileversion,} \DoNotIndex{\filedate, \docdate, \leavevmode,
% \vspace, \small, \normalsize, \fontsize,} \DoNotIndex{\newcommand,
% \section, \subsection, \subsubsection, \fileversion}
% \DoNotIndex{\filedate, \docdate, \leavevmode, \vspace, \small,
% \normalsize, \fontsize} \DoNotIndex{\selectfont, \abovedisplayskip,
% \belowdisplayskip, \setlength, \item} \DoNotIndex{\global, \input,
% \def, \edef, \texttt, \textsf, \setcounter}
% \DoNotIndex{\pagenumbering, \oddsidemargin, \evensidemargin,
% \newdimen, \renewcommand} \DoNotIndex{\makeatletter, \let, \@listi,
% \@listl, \z@, \@xpt, \@xipt, \p@ , \@plus2} \DoNotIndex{\@minus5,
% \@minus3, \@plus3, \makeatother, \@minus4, \relax, \pagestyle}
% \DoNotIndex{\fancyhead, \fancyfoot, \thepage, \@ixp}
% \DoNotIndex{\begin \botmark, \botmark, \catcode, \croppadbot,
% \croppadtop} \DoNotIndex{\croppagewidth, \endverse, \equal,
% \fancyfoot, \fancyhead, \fancyhf} \DoNotIndex{\fancypagestyle,
% \firstmark, \footrulewidth, \hbox, \hbox, \headrulewidth}
% \DoNotIndex{\hfil, \hspace, \ifthenelse, \immediate, \itemindent,
% \listparindent} \DoNotIndex{\makeatletter, \nolinebreak, \openout,
% \raggedbottom, \setcounter, \textrm} \DoNotIndex{\value, \vspace,
% \write} 
% \DoNotIndex{\afterpoemtitleskip, \addtocounter, \advance,
% \begin, \bf, \bigskip}
% \DoNotIndex{\bigskipamount, \fi, \@fulltitle, \label, \large, \leftheader}
% \DoNotIndex{\leftheadervalue, \leftmargin, \leftmargini}
% \DoNotIndex{\medskip, \newcounter, \nobreak, \par, \textbf}
% \DoNotIndex{\closeout,\em,\emph,\expandafter,\Huge,\it,\jobname,\kern,\newif,
% \newwrite,\p@,\pageref,\renewenvironment,\rm,\scriptsize,\string, 
% \textsc, \unskip}
% \setcounter{IndexColumns}{2}
% \MakeShortVerb{|}
% \def\fileversion{1.55}
% \def\filedate{14 October 2013}
% \def\docdate{14 October 2013}
% \title{Running Headers for Endnotes\thanks{This document
%   corresponds to \textsf{endheads}~\fileversion, dated \filedate.}}
% \author{John Burt \\ \texttt{burt@brandeis.edu}}
% \date{October 14, 2012}
% \maketitle
% \begin{abstract}
%  \textsf{endheads} provides running headers of the form ``Notes to 
%  pp.~xx--yy'' for endnotes. \textsf{endheads} is designed to work 
%  with \textsf{endnotes}. 
% \end{abstract}
% \tableofcontents
% \hfuzz=50pt
% \section{Introduction} It is a convenience, if you are 
% flipping though the endnotes of a book, to be able to find the 
% endnote you are looking for by looking at the running header. 
% \textsf{endheads} uses the |\mark| mechanism to update the running 
% header continuously, so that it always indicates to what pages in 
% the main text the notes on a particular page refer.  
% \textsf{endheads} also enables one to reset endnote numbering at 
% the beginning of each chapter.
%
% \textsf{endheads} requires the following packages to work correctly:
% with the standard document classes, or the komascript classes:
% \textsf{endnotes} (obviously), \textsf{fancyhdr} (also obviously),
% \textsf{ifthen} (for comparing page numbers), \textsf{needspace} (to
% prevent widowed chapter titles in notes sections when resetting
% notes by chapter using either |\notesbychapter| or
% |\resetendnotes|), and \textsf{titleref} (to send the chapter titles
% to the notes section).
%
%With \textsf{memoir} class, \textsf{endheads} requires fewer packages, 
% since many of the package functions are built into \textsf{memoir} and some,
% \textsf{fancyhdr} most especially, conflict with it. Minimally, you will need \textsf{endnotes}
% and \textsf{ifthen}. You might wish to call \textsf{memoir} with the oldfontcommands
% option.
%
% \section{Setup} \DescribeMacro{\setupendnoteheaders} This is the
% only macro you need to call to use this package.  If you use
% |\setupendnoteheaders| you should remember to start your endnotes on
% a new page.  |\setupendnoteheaders| sets the first page of the notes
% section to the |empty| pagestyle, since you normally don't have
% running headers on the first page of a section.  And it changes the
% pagestyle for subsequent pages to have a running header of the form
% ``Notes to pages xx--yy.''  This header will appear in the center of
% each header.  Then it modifies the |\endnote| command, having it
% increment a counter that keeps track of all the endnotes (even if
% you are resetting the number by chapter), and defines a label for
% that note.  Finally, it writes on the external file for the endnotes
% a command to set the |\mark| for that page of the endnotes using a
% |\pageref| to the label it has just defined.  \textsf{endheads}
% creates a label and a pageref for every note.
%
% \DescribeMacro{\notesbychapter} |\notesbychapter| resets the note 
% counter every time you issue a |\chapter| command, and it puts a 
% centered line reading ``Chapter \textless\emph{chapter 
% number}\textgreater ~ \textless\emph{chapter title}\textgreater'' in the 
% notes at that point. If you want only the chapter number, not its 
% title, issue |\titleinnotesfalse| in your preamble. Please note 
% that  with the standard classes 
% \textsf{endheads} modifies |\@chapter|, adding a few lines on 
% to the end of the command. If some other package does the same, you 
% may need to reconcile them. With \textsf{memoir} class, |\chapter| is modified
% only if  |\notesbychapter| is issued.
%
% \begin{sloppypar}
% \DescribeMacro{\setstyleforchapternotebegin} 
% \DescribeMacro{\setstyleforchapternoteend} 
% |\setstyleforchapternotebegin| and |\setstyleforchapternoteend| 
% allows you to customize the appearance of the line that separates 
% chapters in the endnotes. If, for instance, rather having a 
% centered line in the small font size, you want a flushleft line in 
% the large fontsize, issue 
% |\setstyleforchapternotebegin{\begin{flushleft}\large}| and 
% |\setstyleforchapternoteend{\end{flushleft}}| in your preamble.
% To set these lines in italic or bold, issue 
% |\setstyleforchapternotebegin{\em}| or 
% |\setstyleforchapternotebegin{\bf}| in your preamble. (It's safe, 
% indeed required, to use the old style font commands here, since the 
% chapter title line in the notes is confined to a group, which 
% restricts the scope of the font style command to the line. If you are
% using \textsf{memoir} class, you should use the oldfontcommands option if you
% plan on issuing |\setstyleforchatpernotebegin| and |\setstyleforchapternoteend|.) 
% Remember that if you open an environment with 
% |\setstyleforchapternotebegin| you must close it in 
% |\setstyleforchapternoteend|. So, for instance, if you wish the 
% chapter line to be set flushleft, in boldface, in normal font size, 
% you would issue 
% |\setstyleforchapternotebegin{\begin{flushleft}\begin{bf}\normalsize}| and 
% |\setstyleforchapternoteend{\end{bf}\end{flushleft}}| in your 
% preamble.
% \end{sloppypar}
%
% \DescribeMacro{\resetendnotes} Conceivably you might wish to do what
% |\notesbychapter| does for each chapter individually.  For instance,
% there may be a chapter without endnotes, in which case
% |\notesbychapter| would issue the centered line for that chapter,
% but no notes would follow, which is ugly.  Also, since the
% Bibliography is, as far as \LaTeX\ is concerned, a chapter, you must
% use |\resetendnotes| before each chapter rather issuing
% |\notesbychapter| in the preamble if you have a bibliography, or odd
% results will follow.
% 
% \section{Using \textsf{endheads} in a Multilingual World}
% 
% \begin{sloppypar}
% By default, \textsf{endheads} uses English language running headers.  
% As of version 1.2 these
% are easily changed. \end{sloppypar} 
% 
% 
% \DescribeMacro{\changenotesname} To change the name for the endnote
% section for notes (the default is Notes), issue
% |\changenotesname{your version}|.  To change the running header (by
% default ``Notes to'') issue |\changenotesheader{your version}|.  
%
% \DescribeMacro{\changenotescontentsname}
%To
% change the name for the  notes in the table of contents ( by
% default ``Notes'') issue |\changenotescontentsname{your version}|.
%
% \DescribeMacro{\notesincontents} If you do not wish to include a line for the endnotes
% in the table of contents, issue |\notesincontentsonfalse| (This is the default). To include a line
% for the endnotes in the table of contents, issue |\notesincontents|  
% or (toggling the boolean) |\notesincontentsontrue|. If you wish to 
% rethink how to present the notes in the table of contents, include 
% an |\addtocontents| line just before you issue |\theendnotes|, the command which 
% will read all of your endnotes into your document at that point. For instance, I set the 
% word ``Notes'' in the table of contents flush with the left margin, just where the numbers
% in the preceding chapters are. You might, however, wish to indent that word just a bit, so
% that it will be flush with the chapter titles above, rather than with the chapter numbers.
% If you wish to do this, just before issuing |\theendnotes|, issue
% |\addcontentsline{toc}{chapter}{\protect\numberline{} \the\notescontentsname}|.
%
% \begin{sloppypar}
% \DescribeMacro{\changesinglepageabbrev}
% \DescribeMacro{\changemultiplepageabbrev} Running headers refer to
% single pages with the English abbreviation ``p.''  and to multiple
% pages with the English abbreviation ``pp.''  To change these issue
% |\changesinglepageabbrev{new abbrev}| or
% |\changemultiplepageabbrev{new abbrev}| respectively.  The ultimate
% outcome is to translate a header of the form ``Notes to
% pp.~xx-yy'' to the form appropriate for your
% language. \end{sloppypar}
%
% \begin{sloppypar}
% \DescribeMacro{\changechapternotesline}If you are using |\resetendnotes| 
% or |\setnotesbychapter| you may also want to 
% change the text of the line those commands add to the endnotes so as to use some
% other word than ``Chapter.''  To change this, 
% issue |\changechapternotesline{your version}|.\end{sloppypar}
% 
% \section{Using \textsf{endheads} with \textsf{biblatex}}
% If you use \textsf{biblatex} you might get an error message 
% that reads ``Package biblatex Error: Patching 'endnotes' package failed.'' 
% What is happening here is that \textsf{biblatex} and \textsf{endheads} are both
% trying to modify the |\theendnotes| command from \textsf{endnotes}. If
% \textsf{endheads} goes first, \textsf{biblatex} can't find the portion of the command 
% it is trying to modify. Now
% \textsf{biblatex} redefines |\theendnotes| at |\begin{document}|, 
% and \textsf{endheads }redefines |\theendnotes| when |\setupendnoteheaders|
% is issued, so issue |\setupdendnoteheaders| 
% after |\begin{document}| and everything should be OK.
% \StopEventually{\PrintChanges\PrintIndex}
% \section{Implementation}
% \subsection{General Purpose Routines}
% Essentially these routines are code posted by Heiko Oberdiek to 
% comp.text.tex. If you set a counter from a |\pageref|, that 
% |\pageref| is undefined on the first run, and \TeX\ will not only 
% complain about the undefined reference, but give a ``missing 
% number'' error if you try to do anything with that reference, such 
% as compare one page number to another. These commands enable one to 
% use the |\pageref| without generating the missing number error on 
% the first run.
%    \begin{macrocode}
\providecommand*{\ifrefundefined}[1]{%
  \expandafter\ifx\csname r@#1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\providecommand*{\@extract@ref}[2]{%
  \expandafter\expandafter\expandafter#1\csname
r@#2\endcsname{}{}\@nil
}

\providecommand*{\@extractref}{%
  \@extract@ref\@car
}

\providecommand*{\@extractpageref}{%
  \@extract@ref\@secondcar
}

\long\def\@secondcar#1#2#3\@nil{#2}

\providecommand*{\setcounterfromref}[2]{%
  \ifrefundefined{#2}{%
    \protect\G@refundefinedtrue
    \@latex@warning{Reference `#2' on page \thepage \space
        undefined}%
    \setcounter{#1}{0}%
  }{%
    \setcounter{#1}{\@extractref{#2}}%
  }%
}
\providecommand*{\setcounterfrompageref}[2]{%
  \ifrefundefined{#2}{%
    \protect\G@refundefinedtrue
    \@latex@warning{Reference `#2' on page \thepage \space
        undefined}%
    \setcounter{#1}{0}%
  }{%
    \setcounter{#1}{\@extractpageref{#2}}%
  }%
}
\long\def\appendtomacro#1#2{%
\begingroup
    \toks@\expandafter{#1#2}%
    \xdef#1{\the\toks@}%
  \endgroup}
%    \end{macrocode}
% \subsection{Building Blocks}
% First, define some counters:
%    \begin{macrocode}
\newcounter{allendnotes}
\setcounter{allendnotes}{1}
\newcounter{notepageholder}
%    \end{macrocode}
%    \begin{macro}{\mymarks}
% A command to set the marks. This will be called by the page style.
%    \begin{macrocode}
\newtoks{\singlepageabbrev}
\singlepageabbrev={p.}
\newcommand{\changesinglepageabbrev}[1]{\singlepageabbrev={#1}}
\newtoks{\multiplepageabbrev}
\newcommand{\changemultiplepageabbrev}[1]{\multiplepageabbrev={#1}}
\multiplepageabbrev{pp.}
\@ifclassloaded{memoir}{\newcommand{\mymarks}{%
   \ifthenelse{\equal{\rightmark}{\leftmark}}%
        {\unskip ~\the\singlepageabbrev~\rightmark}% if equal
        {\unskip ~\the\multiplepageabbrev~\rightmark--\leftmark}}%if not equal
}
{\newcommand{\mymarks}{%
   \ifthenelse{\equal{\firstmark}{\botmark}}%
        {\unskip ~\the\singlepageabbrev~\firstmark}% if equal
        {\unskip ~\the\multiplepageabbrev~\firstmark--\botmark}}%if not equal
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\endnotespagestyle}
% A command to set the page style in \textsf{fancyhdr}. You might 
% want to change some aspects of this, such as where to put the page 
% number, to make it consistent with the rest of your document.
%    \begin{macrocode}

\newtoks{\endnotesname}
\endnotesname={Notes}
\newcommand{\changenotesname}[1]{\def\notesname{#1}}
\newtoks{\notesheadername}
\notesheadername={Notes to}
\newcommand{\changenotesheader}[1]{\notesheadername={#1}}
\newtoks{\notescontentsname}
\notescontentsname={Notes}
\newcommand{\changenotescontentsname}[1]{\notescontentsname={#1}}	
\newtoks{\chapternoteslinename}
\chapternoteslinename={Chapter}
\newcommand{\changechapternotesline}[1]{\chapternoteslinename={#1}}	
\@ifclassloaded{memoir}{
\makepagestyle{endnotesstyle}
\makeevenhead{endnotesstyle}{{\small\textrm{\thepage}}}{{\small{\em \the\notesheadername~\mymarks}}}{}
\makeoddhead{endnotesstyle}{}{{\small{\em \the\notesheadername~\mymarks}}}{{\small\textrm{\thepage}}}
\makeevenfoot{endnotesstyle}{}{}{}
\makeoddfoot{endnotesstyle}{}{}{}
}{
\fancypagestyle{endnotesstyle}{%
\fancyhf{}
\fancyhead[CO]{{\small{\em \the\notesheadername~\mymarks}}}
\fancyhead[CE]{{\small{\em \the\notesheadername~\mymarks}}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhead[RO,LE]{{\small\textrm{\thepage}}}
}
}
		
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\checknoteheaders}
% This macro sets the counter notepageholder to the value given by 
% the pageref of the note that is being output. All that strange 
% business with |\string| is there because I want to use the 
% |\immediate\write| mechanism to control what gets expanded and what 
% doesn't get expanded on the external file for the endnotes, |\jobname.ent|. 
% Everything except the actual number of the endnote is written to 
% the external file as unexpanded tokens. When, after you call 
% |\theendnotes|, you read in the external file for the endnotes, 
% |\jobname.ent|, all these tokens become a command to set the |\mark|.
%    \begin{macrocode}
\@ifclassloaded{memoir}{\newcommand{\checknoteheaders}{%
\string\setcounterfrompageref\string{notepageholder\string}%
\string{notl\theallendnotes\string}%
\string\markboth\string{\string\thenotepageholder\string}\string{\string\thenotepageholder\string}%
}
}{\newcommand{\checknoteheaders}{%
\string\setcounterfrompageref\string{notepageholder\string}%
\string{notl\theallendnotes\string}%
\string\mark\string{\string\thenotepageholder\string}%
}
}

%    \end{macrocode}
%    \end{macro}
% \subsection{User Code}
%    \begin{macro}{\notesincontents}
%    \begin{macrocode}
\newif\ifnotesincontentson \notesincontentsonfalse
\newcommand{\notesincontents}{\notesincontentsontrue}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\setupendnoteheaders}
% |\setupendnoteheaders| turns on processing of running headers, and 
% adds two lines to the definition of |\endnote|
%    \begin{macrocode}
\newif\ifendnoteheaderson \endnoteheadersonfalse
\newcommand{\setupendnoteheaders}{%
\endnoteheadersontrue
\if@enotesopen \else \@openenotes \fi
\immediate\write\@enotes{%
\string\thispagestyle\string{empty\string}
\string\pagestyle\string{endnotesstyle\string}}
\immediate\write\@enotes{\string\ifnotesincontentson}%
\immediate\write\@enotes{\string\addcontentsline\string{toc\string}
\string{chapter\string}\string{\string\the\string\notescontentsname\string}}%
%\immediate\write\@enotes{\string{\string\notescontentsname\string}}%
%\string{\string\protect\string\numberline
%\string{\string}  \string\notescontentsname \string}
\immediate\write\@enotes{\string\fi}
\def\endnote{\@ifnextchar[\@xendnote{\stepcounter{endnote}%
% my addition begins
     \stepcounter{allendnotes}\label{notl\theallendnotes}%
     \immediate\write\@enotes{\checknoteheaders}%
% my addition ends
     \protected@xdef\@theenmark{\theendnote}%
     \@endnotemark\@endnotetext}}
\let\oldtheendnotes=\theendnotes
\renewcommand{\theendnotes}{%
\immediate\write\@enotes{\string\thispagestyle\string{endnotesstyle\string}}
\oldtheendnotes}
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\resetendnotes}
% Tells the |\chapter| macro to reset the note counter and to 
% put a line with the chapter number in the endnotes.
%    \begin{macrocode}
\def\strip#1>{}
\newcommand{\literalendnote}[1]{\if@enotesopen \else 
\@openenotes\setupendnoteheaders \fi
     \begingroup
        \def\next{#1}%
        \newlinechar='40
        \immediate\write\@enotes{\expandafter\strip\meaning\next}%
     \endgroup%
}
\newif\iftitleinnotes \titleinnotestrue
\newcommand{\styleforchapternotebegin}{%
}
\newcommand{\styleforchapternoteend}{%
}
\newcommand{\setstyleforchapternotebegin}[1]{%
\renewcommand{\styleforchapternotebegin}{#1}}
\newcommand{\setstyleforchapternoteend}[1]{%
\renewcommand{\styleforchapternoteend}{#1}}%
\setstyleforchapternotebegin{\begin{center}}
\setstyleforchapternoteend{\end{center}}
\newcommand{\resetendnotes}{%
\label{chapter\thechapter}%
\setcounter{endnote}{0}%
\if@enotesopen \else \@openenotes \fi
\immediate\write\@enotes{\string\begingroup}
\immediate\write\@enotes{\string\goodbreak}
\immediate\write\@enotes{\string\Needspace\string{5\string\baselineskip\string}}
\immediate\write\@enotes{\string\styleforchapternotebegin}
% \immediate\write\@enotes{\string\begin\string{center\string}}
%\immediate\write\@enotes{\string{}
\immediate\write\@enotes{\the\chapternoteslinename\string\ \thechapter\string~}
\iftitleinnotes
\immediate\write\@enotes{\string\titleref\string{chapter\thechapter\string}}
\fi
%\immediate\write\@enotes{\string}}
\immediate\write\@enotes{\string\styleforchapternoteend}
% \immediate\write\@enotes{\string\end\string{center\string}}
\immediate\write\@enotes{\string\nopagebreak}
\immediate\write\@enotes{\string\endgroup}
}          
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\notesbychapter}
%    \begin{macrocode}
% \newcommand{\notesbychapter}{%
% \appendtomacro{\chapter}{%
% \relax
% \resetendnotes}%
% }
\newif\ifnotesbychapteron \notesbychapteronfalse
\@ifclassloaded{memoir}{\newcommand{\notesbychapter}{\notesbychapterontrue
\addtoiargdef{\chapter}{}{ \iftitleinnotes%
%   \label{chapter\thechapter}%
    \fi%
    \ifnotesbychapteron%
    \relax%
    \resetendnotes%
    \fi}}
}{\newcommand{\notesbychapter}{\notesbychapterontrue}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{chapter}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi
		    \iftitleinnotes%
%		    \label{chapter\thechapter}%
		    \fi%
		    \ifnotesbychapteron%
		    \relax%
		    \resetendnotes%
		    \fi}
}

%    \end{macrocode}
%    \end{macro}
% \Finale
\endinput			   