% Author: Alexey B. Shipunov (plantago@herba.msu.ru)

\ProvidesPackage{biokey}%
        [2008/02/11 v3.1 Typesetting biological identification keys]

\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}
% `Automatically' 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}

% PUNCTS
% Common punct
\let\TT=\T

% 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.5em
\addtolength{\TML@a}{-\TML@b}
\addtolength{\TML@b}{0.5em}
\def\Indent@T{\hangindent2em\noindent}
\def\OTSTUP{2em}
\def\Indent@C{\parindent\OTSTUP\hangindent\parindent}

% Common thesa
\def\Z#1. {\Indent@T\makebox[1.5em][r]%
        {\SHRIFTZ{#1.}}\hspace{0.5em}}
% Thesa with back reference
\def\ZZ#1(#2).{\Indent@T\makebox[\TML@a][r]%
        {\SHRIFTZ{#1}}\hspace{0.45em}\SHRIFTZ{(#2).}}
% Antitheses
\def\ZZZ#1{\Indent@T\makebox[\TML@a][r]%
        {#1}\hspace{\TML@b}}
% 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\smallskip%
        \Indent@C%
        \footnotesize#1\par\smallskip}}
% You can use it as `numberless thesa'
\def\KOM#1\par{{\nopagebreak\smallskip%
        \Indent@C#1\par\smallskip}}
% Comment for object
\def\VPRAVO#1 \par{{\nopagebreak\vskip-\parskip
        \hbox to \linewidth {\hfil #1}\par}}

% AUTOMATICAL KEYS
\def\TEZA#1{\refstepcounter{TMC@Ba}\label{#1}%
        \Indent@T\makebox[1.5em][r]%
        {\SHRIFTZ{\arabic{TMC@Ba}.}}\hspace{0.5em}\ignorespaces}
\def\STEZA#1#2{\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

% LEVELED 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 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

% Some useful stuff
\let\K=\textit
\let\J=\textbf
\def\KN#1 {\textit{#1 }}
\def\NN#1 #2 {\textit{#1 #2 }}
\def\IN#1.#2.{#1.\,#2.~\ignorespaces}

\endinput
