%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%       ****************************************
%       *              TABNOTES                *
%       ****************************************
%
%  Date of this version: 28 August 2009.
%  Matthias Borck-Elsner
%
%% Based on endnotes.sty Copyright 2002 John Lavagnino
%%
%% 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.
%
%  Uses an extra external file, with .tne extension, to hold the
%  text of the tabnotes.  This may be deleted after the run; a new
%  version is generated each time--it doesn't require information
%  collected from the previous run.
%
%  This code does not obey \nofiles.  Perhaps it should.
%
%
%  To turn all the footnotes in your documents into tabnotes, say
%
%     \let\footnote=\tabnote
%
%  in your preamble, and then add something like
%
%     \newpage
%     \begingroup
%     \parindent 0pt
%     \parskip 2ex
%     \def\tnotesize{\normalsize}
%     \thetabnotes
%     \endgroup
%
% as the last thing in your document.  (But \thetabnotes all
% by itself will work.)
%

%
%
%       ****************************************
%       *        TABNOTE COMMANDS              *
%       ****************************************
%
%
%   \tabnote{NOTE}       : User command to insert a tabnote.
%
%   \tabnote[NUM]{NOTE}  : User command to insert a tabnote numbered
%                           NUM, where NUM is a number -- 1, 2,
%                           etc.  For example, if tabnotes are numbered
%                           *, **, etc. within pages, then  \tabnote[2]{...}
%                           produces tabnote '**'.  This command does  not
%                           step the tabnote counter.
%
%   \tabnotemark[NUM]    : Command to produce just the tabnote mark in
%                           the text, but no tabnote.  With no argument,
%                           it steps the tabnote counter before  generating
%                           the mark.
%
%   \tabnotetext[NUM]{TEXT} : Command to produce the tabnote but no
%                              mark.  \tabnote is equivalent to
%                              \tabnotemark \tabnotetext .
%
%   \addtotabnotes{TEXT} : Command to add text or commands to current
%                              tabnotes file: for inserting headings,
%                              pagebreaks, and the like into tabnotes
%                              sections.  TEXT a moving argument:
%                              \protect required for fragile commands.
%
%       ****************************************
%       *        TABNOTE USER COMMANDS         *
%       ****************************************
%
%   Tabnotes use the following parameters, similar to those relating
%   to footnotes:
%
%   \tnotesize   : Size-changing command for tabnotes.
%
%   \thetabnote : In usual LaTeX style, produces the tabnote number.
%
%   \thetnmark : Holds the current tabnote's mark--e.g., \dag or '1' or 'a'.
%                 (You don't want to set this yourself, as it comes
%                 either from the autonumbering of notes or from
%                 the optional argument to \tabnote. But you'll need
%                 to use it if you define your own \maketnmark.)
%
%   \maketnmark : A macro to generate the tabnote marker from  \thetnmark
%                  The default definition is \hbox{$^\thetnmark$}.
%
%   \@makeentext{NOTE} :
%        Must produce the actual tabnote, using \thetnmark as the mark
%        of the tabnote and NOTE as the text.  It is called when effectively
%        inside a \parbox, with \hsize = \columnwidth.  For example, it might
%        be as simple as
%               $^{\thetnmark}$ NOTE
%
%
%       ****************************************
%       *        TABNOTE PSEUDOCODE            *
%       ****************************************
%
% \tabnote{NOTE}  ==
%  BEGIN
%    \stepcounter{tabnote}
%    \@thetnmark :=G eval (\thetabnote)
%    \@tabnotemark
%    \@tabnotetext{NOTE}
%  END
%
% \tabnote[NUM]{NOTE} ==
%  BEGIN
%    begingroup
%       counter tabnote :=L NUM
%       \@thetnmark :=G eval (\thetabnote)
%    endgroup
%    \@tabnotemark
%    \@tabnotetext{NOTE}
%  END
%
% \@tabnotetext{NOTE} ==
%  BEGIN
%    write to \@tnotes file: "\@doantnote{TABNOTE MARK}"
%    begingroup
%       \next := NOTE
%       set \newlinechar for \write to \space
%       write to \@tnotes file: \meaning\next
%               (that is, "macro:->NOTE)
%    endgroup
%  END
%
% \addtotabnotes{TEXT} ==
%  BEGIN
%    open tabnotes file if not already open
%    begingroup
%       let \protect to \string
%       set \newlinechar for \write to \space
%       write TEXT to \@tnotes file
%    endgroup
%  END
%
% \tabnotemark      ==
%  BEGIN \stepcounter{tabnote}
%        \@thetnmark :=G eval(\thetabnote)
%        \@tabnotemark
%  END
%
% \tabnotemark[NUM] ==
%   BEGIN
%       begingroup
%         tabnote counter :=L NUM
%        \@thetnmark :=G eval(\thetabnote)
%       endgroup
%       \@tabnotemark
%   END
%
% \@tabnotemark ==
%   BEGIN
%    \leavevmode
%    IF hmode THEN \@x@sf := \the\spacefactor FI
%    \maketnmark          % put number in main text
%    IF hmode THEN \spacefactor := \@x@sf FI
%   END
%
% \tabnotetext      ==
%    BEGIN \@thetnmark :=G eval (\thetabnote)
%          \@tabnotetext
%    END
%
% \tabnotetext[NUM] ==
%    BEGIN begingroup  counter tabnote :=L NUM
%                      \@thetnmark :=G eval (\thetabnote)
%          endgroup
%          \@tabnotetext
%    END
%
%       ****************************************
%       *           ENDNOTE MACROS             *
%       ****************************************
%

\@definecounter{tabnote}
\def\thetabnote{\@arabic\c@tabnote}

\def\@maketnmark{\hbox{\@textsuperscript{\normalfont\@thetnmark}}}
\def\maketnmark{\@maketnmark}

\def\thetnmark{\@thetnmark}

\newdimen\tabnotesep

\def\tabnote{\@ifnextchar[\@xtabnote{\stepcounter{tabnote}%
     \protected@xdef\@thetnmark{\thetabnote}%
     \@tabnotemark\@tabnotetext}}

\def\@xtabnote[#1]{%
   \begingroup
     \c@tabnote=#1\relax
     \unrestored@protected@xdef\@thetnmark{\thetabnote}%
   \endgroup
   \@tabnotemark\@tabnotetext}

%  Here begins a section of tabnote code that's really different from
% the footnote code of LaTeX.

\let\@doantnote=0
\let\@endantnote=0

\newwrite\@tnotes
\newif\if@tnotesopen \global\@tnotesopenfalse

\def\@opentnotes{\immediate\openout\@tnotes=\jobname.tne\relax
      \global\@tnotesopentrue}

%  The stuff with \next and \meaning is a trick from the TeXbook, 382,
% there intended for setting verbatim text, but here used to avoid
% macro expansion when the footnote text is written.  \next will have
% the entire text of the footnote as one long line, which might well
% overflow limits on output line length; the business with \newlinechar
% makes every space become a newline in the \@tnotes file, so that all
% of the lines wind up being quite short.

\long\def\@tabnotetext#1{%
     \if@tnotesopen \else \@opentnotes \fi
     \immediate\write\@tnotes{\@doantnote{\@thetnmark}}%
     \begingroup
        \def\next{#1}%
        \newlinechar='40
        \immediate\write\@tnotes{\meaning\next}%
     \endgroup
     \immediate\write\@tnotes{\@endantnote}}

% \addtotabnotes works the way the other tabnote macros probably should
% have, requiring the use of \protect for fragile commands.

\long\def\addtotabnotes#1{%
     \if@tnotesopen \else \@opentnotes \fi
     \begingroup
        \newlinechar='40
        \let\protect\string
        \immediate\write\@tnotes{#1}%
     \endgroup}

%  End of unique tabnote code

\def\tabnotemark{%
   \@ifnextchar[\@xtabnotemark
     {\stepcounter{tabnote}%
      \protected@xdef\@thetnmark{\thetabnote}%
      \@tabnotemark}}

\def\@xtabnotemark[#1]{%
   \begingroup
      \c@tabnote #1\relax
      \unrestored@protected@xdef\@thetnmark{\thetabnote}%
   \endgroup
   \@tabnotemark}

\def\@tabnotemark{%
  \leavevmode
  \ifhmode\edef\@x@sf{\the\spacefactor}\nobreak\fi
  \maketnmark
  \ifhmode\spacefactor\@x@sf\fi
  \relax}

\def\tabnotetext{%
     \@ifnextchar [\@xtabnotenext
       {\protected@xdef\@thetnmark{\thetabnote}%
    \@tabnotetext}}

\def\@xtabnotenext[#1]{\begingroup \c@tabnote=#1\relax
   \xdef\@thetnmark{\thetabnote}\endgroup \@tabnotetext}

\def\@xtabnotenext[#1]{%
  \begingroup
     \c@tabnote=#1\relax
     \unrestored@protected@xdef\@thetnmark{\thetabnote}%
  \endgroup
  \@tabnotetext}


%  \thetabnotes actually prints out the tabnotes.

%  The user may want separate tabnotes for each chapter, or a big
% block of them at the end of the whole document.  As it stands,
% either will work; you just say \thetabnotes wherever you want the
% tabnotes so far to be inserted.  However, you must add
% \setcounter{tabnote}{0} after that if you want subsequent tabnotes
% to start numbering at 1 again.

%  \tnoteformat is provided so user can specify some special formatting
% for the tabnotes.  It needs to set up the paragraph parameters, start
% the paragraph, and print the label.  The \mbox stuff in \tnoteheading
% is to make and undo a dummy paragraph, to get around the games \section*
% plays with paragraph indenting and instead give us uniform
% indenting for all notes.

\def\tnotesname{Tablenotes}%
\def\tnoteheading{\subsection*{\tnotesname
  \@mkboth{\MakeUppercase{\tnotesname}}{\MakeUppercase{\tnotesname}}}%
  \mbox{}\par\vskip-\baselineskip}

\def\tnoteformat{\rightskip\z@ \leftskip\z@ \parindent=1.8em
  \leavevmode\llap{\maketnmark}}

\def\tnotesize{\footnotesize}

\def\thetabnotes{\immediate\closeout\@tnotes \global\@tnotesopenfalse
  \begingroup
    \makeatletter
    %
    % The machinery with \@ResetGT and > here ensures that
    % \@doantnote works properly even if > is an active character
    % at the point where \thetabnotes is invoked. > needs to have
    % catcode 12 when the arguments of \@doantnote are scanned, so
    % that the > in the string "macro:->" is matched.  The actual
    % footnote text is not an argument to \@doantnote, but just
    % follows it in the .tne file; so \@ResetGT can reset the
    % category code for > that should be used when processing
    % that text.  That resetting takes place within a
    % \begingroup-\endgroup block set up by \@doantnote and
    % \@endantnote, so the catcode for > is back to 12 for the
    % next note.
    %
    \edef\@tempa{`\string >}%
    \ifnum\catcode\@tempa=12%
      \let\@ResetGT\relax
    \else
      \edef\@ResetGT{\noexpand\catcode\@tempa=\the\catcode\@tempa}%
      \@makeother\>%
    \fi
    \def\@doantnote##1##2>{\def\@thetnmark{##1}\par\begingroup
        \@ResetGT
        \edef\@currentlabel{\csname p@tabnote\endcsname\@thetnmark}%
        \tnoteformat}
    \def\@endantnote{\par\endgroup}%
    \tnoteheading
    \tnotesize
    \input{\jobname.tne}%
  \endgroup}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
