%%
%% This is file `novel-Footnotes.sty', part of class `novel'.
%% Copyright 2017 Robert Allgeyer.
%%
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% 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.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
%%
\ProvidesFile{novel-Footnotes.sty}%
[2017/05/04 v1.2 LaTeX file (commands for footnotes and endnotes)]
%%


%% FOOTNOTES
%% -----------------------------------------------------------------------------
% In `novel' class, you may place footnotes at the botom of the textblock.
% The markers may be symbolic (default begins with asterisk), or
% set so that the asterisk is not used, or the markers may be numbers,
% If symbolic, they will be automatically reset by ChapterStart.
% You can manually reset them at any time.
% You can also make a non-marked footnote, which does not increment the count.



%% footnotesize is calculated by layout, based on normal font size and skip.
%% It is used only for actual footnotes!

%
\newcounter{@fnsymbolcount} % counter, footenote symbolic markers
  \setcounter{@fnsymbolcount}{1}
\newcounter{@fnnumbercount} % counter, footnote numerical markers
  \setcounter{@fnnumbercount}{1}
\newcounter{@rnnumbercount} % counter, endnote markers (always numerical)
  \setcounter{@rnnumbercount}{1}
\newcounter{@maxfnsymbol} % max footnote symbol count before recycle
%
\gdef\ResetFootnote{% symbols or numbers
  \setcounter{@fnsymbolcount}{1}%
  \setcounter{@fnnumbercount}{1}%
}
\gdef\ResetFootnoteSymbol{\setcounter{@fnsymbolcount}{1}} % only symbols
\gdef\Resetendnote{\setcounter{@rnnumbercount}{1}}
%
\newtoggle{@usenomark} % when true: no mark, no increment count
\newtoggle{@placedfootnotenumber} % true if any numerical footnote placed
%
\let\my@oldfootnote\footnote\relax % copies the original definition
\let\footnote\relax % so it can be re-defined, below
%
\gdef\@fourem{% length of rule separating footnotes from main text
  \vspace{-\nbs}
  \noindent\strut\smash{\rule[-2pt]{0.33\textwidth}{0.05\normalFontSize}}
}
\let\footnoterule\@fourem
%


% \@makefnmark applies to the footnote marker in the flow of main text:
\gdef\@makefnmark{\iftoggle{@usenomark}{}{\@getfnmark}}
%
\long\gdef\@makefntext#1{% the actual footnote, at bottom of textblock
  \iftoggle{@usenomark}%
  {\forceindent#1}%
  {\forceindent\@getbotfnmark\,#1}%
} % end \@makefntext
% Now re-define \footnote:
\newcommand\footnote[2][]{%
  \ifthenelse{\equal{#1}{0}}{%
    \global\toggletrue{@usenomark}%
  }{%
    \global\togglefalse{@usenomark}%
  }%
  \gdef\footnoterule{\@fourem}%
  \begingroup%
  \def\footnotesize{% Not gdef. Only here in footnote. Calculated during layout.
    \@setfontsize\footnotesize{\@myfootnotesizenum pt}{\@myfootnoteskipnum pt}%
  }%
  \unskip\my@oldfootnote{#2}%
  \endgroup%
} % end \footnote
%
% \@rollfnsymbol prevents running out of symbolic markers:
\gdef\@rollfnsymbol{%
  \stepcounter{@fnsymbolcount}%
  \ifthenelse{\equal{\value{@fnsymbolcount}}{\value{@maxfnsymbol}}}{%
    \setcounter{@fnsymbolcount}{1}%
  }{}%
} % end \@rollfnsymbol
%
% \@getfnmark places a footnote marker in the running text.
% The marker depends upon whether it should be a number or a symbol.
% If the latter, then it depends on whether the symbols include asterisks.
% And, it depends upon the current count for that type of marker.
\gdef\@getfnmark{%
  \iftoggle{@usesymbolmark}%
  {%
    \global\togglefalse{@placedfootnotenumber}%
    \ifthenelse{\equal{\@myfnsymbols}{*}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{\charscale[1,0pt,-\@twast]{*}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{\charscale[0.8,0pt,-\@twdag]{†}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{\charscale[0.8,0pt,-\@twdbldag]{‡}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{\charscale[0.8,0pt,-\@twsect]{§}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{\charscale[1,0pt,-\@twast]{**}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{\charscale[0.8,0pt,-\@twdag]{††}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{7}}{\charscale[0.8,0pt,-\@twdbldag]{‡‡}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{8}}{\charscale[0.8,0pt,-\@twsect]{§§}}{}%
    }{}%
    \ifthenelse{\equal{\@myfnsymbols}{†}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{\charscale[0.8,0pt,-\@twdag]{†}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{\charscale[0.8,0pt,-\@twdbldag]{‡}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{\charscale[0.8,0pt,-\@twsect]{§}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{\charscale[0.8,0pt,-\@twdag]{††}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{\charscale[0.8,0pt,-\@twdbldag]{‡‡}}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{\charscale[0.8,0pt,-\@twsect]{§§}}{}%
    }{}%
  }{%
    \global\toggletrue{@placedfootnotenumber}%
    \sups{\arabic{@fnnumbercount}}%
  }%
} % end \@getfnmark
%
% \@getbotfnmark places the marker in front of the actual footnote,
% towards the bottom of the textblock. It duplicates the marker used in
% running text, but is not elevated. Then, it increments the relevant count.
% Note that (unlike some other TeX document classes) this marker is at
% the indent, so that the footnote text is farther inward than the indent.
% This is the preferred style.
\gdef\@getbotfnmark{%
  \iftoggle{@usesymbolmark}%
  {%
    \ifthenelse{\equal{\@myfnsymbols}{*}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{*}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{†}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{§}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{**}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{††}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{7}}{‡‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{8}}{§§}{}%
    }{}%
    \ifthenelse{\equal{\@myfnsymbols}{†}}{%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{1}}{†}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{2}}{‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{3}}{§}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{4}}{††}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{5}}{‡‡}{}%
      \ifthenelse{\equal{\value{@fnsymbolcount}}{6}}{§§}{}%
    }{}%
    \@rollfnsymbol%
  }{%
    \textrm{\arabic{@fnnumbercount}.}%
    \stepcounter{@fnnumbercount}%
  }%
} %end \@getbotfnmark
%
%% end footnotes


%% ENDNOTES
%% -----------------------------------------------------------------------------
% \endnote only places a marker. It does not store a note for later use.
% The \memo{} command is helpful, to make a non-printing note to yourself.
% Whenever \endnote is used, the marker number and page is added to a list,
% and the list is typed into the log file after the document ends. That way,
% you can hunt down where the endnotes markers were placed.
\newtoggle{@gotendnotes} % true when \endnote used
%
\long\gdef\@endnotelist{}
\newcounter{@endnotecount}
%
\DeclareDocumentCommand \endnote { }
{%
\global\toggletrue{@gotendnotes}%
\stepcounter{@endnotecount}\sups{\arabic{@endnotecount}}%
\long\edef\@endnotelist{\@endnotelist\space (\arabic{@endnotecount},p.\thepage)}%
}
%%
% \endnotetext{ID}{text} where ID is (usually) the endnote number.
% If ID is other than space or tilde, it prints at left margin, followed by
%   period. Then the entire text is block-indented. Only one paragraph allowed.
% If ID is space or empty, then the entire text is block-indented. This is
%   the method for continuing a prior endnote to a new paragraph. The text
%   may begin with \forceindent, according to your needs.
\DeclareDocumentCommand \endnotetext { m +m }% xparse allows \end... commands!
{
  \ifthenelse{\equal{#1}{\space}\OR\equal{#1}{}}%
    {\noindent\hangindent=\parindent\flexbox{\space}#2\par}%
    {\noindent\hangindent=\parindent\flexbox{#1.}#2\par}%
}
%% end endnotes



%% ATBEGINDOCUMENT
%% -----------------------------------------------------------------------------
%%
\AtBeginDocument{
  \gdef\SetFootnoteMarker#1{%
    \typeout{^^JClass `novel' Alert: %
    \string\SetFootnoteMarker\space ineffective after Preamble.^^J}%
  }
  % Must wait for \nbs to be calculated:
  \setlength\footnotesep{0.8\nbs}
  % Must wait for main font and its size to be active:
  \newlength\@hoaring
  \setlength\@hoaring{\heightof{Å}}
  \global\@hoaring=\@hoaring
  \newlength\@twast % tweaks asterisk
  \setlength\@twast{\heightof{*}}
  \addtolength\@twast{-\@hoaring}
  \global\@twast=\@twast
  \newlength\@twdag % tweaks dagger
  \setlength\@twdag{\heightof{†}}
  \addtolength\@twdag{-0.2\@twdag}
  \addtolength\@twdag{-\@hoaring}
  \global\@twdag=\@twdag
  \newlength\@twdbldag % tweaks double dagger
  \setlength\@twdbldag{\heightof{‡}}
  \addtolength\@twdbldag{-0.2\@twdbldag}
  \addtolength\@twdbldag{-\@hoaring}
  \global\@twdbldag=\@twdbldag
  \newlength\@twsect % tweaks section mark
  \setlength\@twsect{\heightof{§}}
  \addtolength\@twsect{-0.2\@twsect}
  \addtolength\@twsect{-\@hoaring}
  \global\@twsect=\@twsect
  \newlength\@tweight % tweaks 8, reliably the tallest digit
  \setlength\@tweight{\heightof{8}}%
  \addtolength\@tweight{-0.2\@tweight}%
  \addtolength\@tweight{-\@hoaring}%
  \global\@tweight=\@tweight%
  % Must wait to see how \SetFootnoteMarker was used:
  \ifthenelse{\equal{\@myfnsymbols}{*}}{\setcounter{@maxfnsymbol}{8}}{}
  \ifthenelse{\equal{\@myfnsymbols}{†}}{\setcounter{@maxfnsymbol}{6}}{}
  %
} % end \AtBeginDocument
%%


%% ATENDDOCUMENT
%% -----------------------------------------------------------------------------
%% Write a list of endnote (number,page) into log file,
% and Big Bad Warning if numerical footnotes and endnotes both used:
\AtEndDocument{%
  \iftoggle{@gotendnotes}%
    {\typeout{^^J^^JClass `novel' Alert: You placed endnotes (number,page):^^J%
    \@endnotelist%
    ^^J^^J}%
    }{}
  %
  \iftoggle{@placedfootnotenumber}%
    {\iftoggle{@gotendnotes}%
      {\typeout{^^JClass `novel' BIG BAD WARNING:^^J%
        Numbered footnotes and endnotes! Better use symbolic footnotes!^^J^^J}%
      }{}%
    }{}%
}% end \AtEndDocument
%%


%% end of file `novel-Footnotes.sty'

