% Author: Alexey Shipunov, dactylorhiza@gmail.com

\ProvidesPackage{biokey}%
    [2019/10/02 v.3.11 Typesetting biological identification keys]

% 20191002 \ZZZ updated, \DD added
% 20150911 \Ver@T added to make keys more readable
% 20150912 Alien stuff removed, \FK supplied with the short vertical space
% 20150913 Antitheses aligned better now, comments' breaks adjusted
% 20150916 Better aligment for backtrack links
% 20150920 'biokey' environment resets theses counter (if hyperref is used, specify 'hypertexnames=false')

\newlength\TML@a
\newlength\TML@b
\newlength\TML@c
\newbox\dot@box
\newcounter{TMC@Ba}

\newif\if@MARG
\DeclareOption{nomargin}{\@MARGfalse}
\DeclareOption{margin}{\@MARGtrue}

\ExecuteOptions{nomargin}
\ProcessOptions

% REFERENCES
% Main reference
\def\SameDecl{\samepage}
\AtBeginDocument{\setbox\dot@box=\hbox to .5em{\hss.\hss}}
\def\dbox@fill{\leaders\copy%
        \dot@box\hskip 4.3\wd\dot@box plus1fill minus .3\wd\dot@box}
\def\dbox@fil{\leaders\copy\dot@box\hfil}
% Against vertical breaks
\def\dot@leaders{\doublehyphendemerits0\finalhyphendemerits0%
        \parfillskip0pt\SameDecl%
        \penalty800\dbox@fill\penalty400%
        \vadjust{}\penalty10000\dbox@fil}
\def\T#1\par{{\dot@leaders\SHRIFTN{#1}\par}\par}
\def\TT#1\par{{\dot@leaders\SHRIFTN{#1}\par}\par}
% `Automatic' paragraph aligment
\def\TTTT#1\par{{\tolerance 9999 \dot@leaders\SHRIFTN{#1}\par}\par}

% Old style reference
\def\old@f{\hbox to .5em{\hss.\hss}}
\def\old@filt{\xleaders\old@f\hfill}
\def\old@dot@leaders{\doublehyphendemerits0\finalhyphendemerits0%
        \parfillskip0pt\SameDecl%
        \unskip\nobreak\old@filt\penalty50%
        \xleaders\old@f\hskip2em%
        \vadjust{}\nobreak\old@filt}
\def\TTT#1\par{{\old@dot@leaders\SHRIFTN{#1}\par}\par}

% OBJECTS
% With indent
\def\N#1\par{{\penalty50\SameDecl%
        \hskip 1em plus 1.2em minus .2em
\penalty200\hskip0pt\SHRIFTN{#1}\par}\par}

% With glue, but without leaders
\def\NN#1\par{{\parfillskip0pt\SameDecl%
        \doublehyphendemerits0\finalhyphendemerits0%
        \penalty10000\hskip 2em plus 1fil\penalty400%
        \vadjust{}\penalty10000\hfil\SHRIFTN{#1}\par}\par}

% Object as new paragraph
\def\NNN#1\par{{\nopagebreak\par%
        \Indent@C\SHRIFTN{#1}\par}\par}

% Hanging punct
\def\VT{\@ifstar{\H@P}{\H@@P}}
% Leaders stopped on right margin of text
\def\H@@P#1\par{{\dot@leaders
        \makebox[0pt][l]{\hspace{.5em}#1}\par}\par}
% Leaders stopped before punct
\def\H@P#1\par{{\dot@leaders
        \makebox[-.5em][l]{#1}\par}\par}

% THESES AND ANTITHESES
% Hooks...
\def\SHRIFTZ{\relax}
\def\SHRIFTN{\relax}

% Lengths (hang = 2em)
\settowidth{\TML@b}{\SHRIFTZ{.}}
\TML@a=1.6em % found visually
\addtolength{\TML@a}{-\TML@b}
\addtolength{\TML@b}{0.6em} % found visually
\def\Ver@T{\vspace{.5\baselineskip}}
\def\Indent@T{\hangindent2em\noindent}
\def\OTSTUP{2em}
\def\Indent@C{\parindent\OTSTUP\hangindent\parindent}

% Common thesa
\def\Z#1. {\Ver@T\Indent@T\makebox[1.5em][r]%
        {\SHRIFTZ{#1.}}\hspace{0.5em}}
% Thesa with back reference
\def\ZZ#1(#2).{\Ver@T\Indent@T\makebox[\TML@a][r]%
        {\SHRIFTZ{#1}}\hspace{0.45em}\SHRIFTZ{(#2).}}
% Antitheses
\def\ZZZ#1{\Indent@T\makebox[1.5em][r]{#1}% older variant: \makebox[\TML@a][r]
        \hspace{0.5em}} % to make exactly 2em box like in \TE and \SE

% First
\def\AN{\ZZZ{$-$}}
% You can replace it to $+$
\def\Ap{\ZZZ{$+$}}
% Second (with trichotomy)
\def\AAN{\ZZZ{$=$}}
% Third (with tetrachotomy)
\def\AAAN{\ZZZ{$\equiv$}}

% COMMENTS
\def\FK#1\par{{\nopagebreak\vspace{.1\baselineskip}\nopagebreak%
        \Indent@C%
        \footnotesize#1\par\smallskip}}
% You can use it as `numberless thesa'
\def\KOM#1\par{{\nopagebreak\Ver@T%
        \Indent@C#1\par\smallskip}}
% Comment for the object
\def\VPRAVO#1 \par{{\nopagebreak\vskip-\parskip\nopagebreak%
        \hbox to \linewidth {\hfil #1}\par}}

% AUTOMATIC KEYS
\def\TEZA#1{\Ver@T\refstepcounter{TMC@Ba}\label{#1}%
        \Indent@T\makebox[1.5em][r]%
        {\SHRIFTZ{\arabic{TMC@Ba}.}}\hspace{0.5em}\ignorespaces}
\def\STEZA#1#2{\Ver@T\refstepcounter{TMC@Ba}\label{#1}%
        \Indent@T\makebox[\TML@a][r]%
        {\SHRIFTZ{\arabic{TMC@Ba}}}\hspace{0.45em}\SHRIFTZ{(\ref{#2}).}}
\def\SSYLKA#1{\T\ref{#1}.}
% Shorthands
\let\TE\TEZA
\let\SE\STEZA
\let\SS\SSYLKA
\def\DD#1{\T\ref{#1}.} % this is fo the case when \SS does not work

% IDENTED KEYS
\newenvironment{SDVIG}[1][1]
       {\TML@c=#1em \let\TML@@c\TML@c
        \def\Indent@T{\hangindent=2em\parindent=0em
        \advance\hangindent by 2\TML@@c
        \advance\parindent by 2\TML@@c}
        \def\Indent@C{\hangindent=2em\parindent=2em
        \advance\hangindent by 2\TML@@c
        \advance\parindent by 2\TML@@c}}{}
% Shorthands
\let\LE\SDVIG
\let\endLE\endSDVIG

% KEYS WITH MARKERS ON THE LEFT PAGE MARGIN
\if@MARG
\def\Indent@C{\relax}%
\def\Z#1. {\noindent\makebox[0em][r]%
        {\SHRIFTZ{#1.}\hspace{0.5em}}}%
\def\ZZ#1(#2).{\settowidth{\TML@c}{(#2)}%
        \noindent\makebox[\TML@c][r]%
        {\SHRIFTZ{#1}\hspace{0.5em}\SHRIFTZ{(#2).}}}%
\def\ZZZ#1{\noindent\makebox[0em][r]%
        {#1\hspace{\TML@b}}}%
\else\relax
\fi

% ENVIRONMENT WHICH RESETS THESES COUNTER
% If hyperref used, requires option "hypertexnames=false"
\newenvironment{biokey}{%
        \setcounter{TMC@Ba}{0}%
        \relax}

\endinput
