summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/endheads/endheads.dtx
blob: c4ca223e5ba7e089ea3d5c5e5bd6775a29f7a0cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
% \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>   [2017/4/6 v1.6 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} 
%\changes{v1.6}{2017/4/6}{Warnings about known issues}
% \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.6}
% \def\filedate{5 April 2017}
% \def\docdate{5 Apri 2017}
% \title{Running Headers for Endnotes\thanks{This document
%   corresponds to \textsf{endheads}~\fileversion, dated \filedate.}}
% \author{John Burt \\ \texttt{burt@brandeis.edu}}
% \date{April 5, 2017}
% \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.
% \section{Known Issues}
% The running headers in \textsf{endheads} will only work correctly if all endnotes are issued on pages 
% with arabic numbering. Endnotes issued on pages with roman numbering will break 
% \textsf{endheads}, because the calculations required to update the headers 
% require arabic numbers.
% 
% Normally if you are using |\footnote| in the body of the text you can issue 
% |\let\footnote=\endnote| to get endnotes with \textsf{endnote}. This will break 
% \textsf{endheads}, however, (the running headers will all have your final 
% chapter title, not the proper page numbers) so all of your endnotes must be made with 
% |\endnote|.
% \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