%%
%% This is file `endheads.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% endheads.dtx  (with options: `package')
%% 
%% IMPORTANT NOTICE:
%% 
%% For the copyright see the source file.
%% 
%% Any modified versions of this file must be renamed
%% with new filenames distinct from endheads.sty.
%% 
%% For distribution of the original source see the terms
%% for copying and modification in the file endheads.dtx.
%% 
%% This generated file may be distributed as long as the
%% original source files, as listed above, are part of the
%% same distribution. (The sources need not necessarily be
%% in the same archive or directory.)
\NeedsTeXFormat{LaTeX2e}[1997/12/01]
\ProvidesPackage{endheads}
   [2013/10/14 v1.55 endheads file]
\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}
\newcounter{allendnotes}
\setcounter{allendnotes}{1}
\newcounter{notepageholder}
\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
}

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

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

\newif\ifnotesincontentson \notesincontentsonfalse
\newcommand{\notesincontents}{\notesincontentsontrue}
\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\fi}
\def\endnote{\@ifnextchar[\@xendnote{\stepcounter{endnote}%
     \stepcounter{allendnotes}\label{notl\theallendnotes}%
     \immediate\write\@enotes{\checknoteheaders}%
     \protected@xdef\@theenmark{\theendnote}%
     \@endnotemark\@endnotetext}}
\let\oldtheendnotes=\theendnotes
\renewcommand{\theendnotes}{%
\immediate\write\@enotes{\string\thispagestyle\string{endnotesstyle\string}}
\oldtheendnotes}
}
\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{\the\chapternoteslinename\string\ \thechapter\string~}
\iftitleinnotes
\immediate\write\@enotes{\string\titleref\string{chapter\thechapter\string}}
\fi
\immediate\write\@enotes{\string\styleforchapternoteend}
\immediate\write\@enotes{\string\nopagebreak}
\immediate\write\@enotes{\string\endgroup}
}
\newif\ifnotesbychapteron \notesbychapteronfalse
\@ifclassloaded{memoir}{\newcommand{\notesbychapter}{\notesbychapterontrue
\addtoiargdef{\chapter}{}{ \iftitleinnotes%
    \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%
    \fi%
    \ifnotesbychapteron%
    \relax%
    \resetendnotes%
    \fi}
}

\endinput
%%
%% End of file `endheads.sty'.
