% This is the librarian package.
% Relevant information can be found in librarian-doc.pdf
%
% Author: Paul Isambert.
% E-mail: zappathustra AT free DOT fr
% Comments and suggestions are welcome.
% Date: April 2010.

\csname lb@alreadyloaded\endcsname
\expandafter\let\csname lb@alreadyloaded\endcsname\endinput

\expandafter\edef\csname lb@restoreat\endcsname{%
  \catcode`\noexpand\@=\the\catcode`\@
  }
\catcode`\@=11
\def\lb@error#1{\errmessage{Librarian error: #1}}
\def\lb@badengine{%
  \lb@error{No proper engine. Use pdfTeX (v.1.4 at least), XeTeX or LuaTeX. I quit}%
  \lb@restoreat
  \endinput
  }
\ifx\scantokens\lb@undefined
  \expandafter\lb@badengine
\fi
\let\lb@next\relax
%%% Shamelessly stolen from Heiko Oberdiek's pdftexcmds.
\ifcsname directlua\endcsname
  \directlua{%
    librarian = { }
    function librarian.strcmp(A,B)
      if A == B then
        tex.write("0")
      elseif A <  B then
        tex.write("-1")
      else
        tex.write("1")
      end
    end
    }%
  \def\lb@strcmp#1#2{%
    \directlua{%
      librarian.strcmp("\luaescapestring{#1}","\luaescapestring{#2}")
      }%
    }
\else
  \ifcsname XeTeXversion\endcsname
    \let\lb@strcmp\strcmp
  \else
    \ifcsname pdfstrcmp\endcsname
      \let\lb@strcmp\pdfstrcmp
    \else
      \let\lb@next\lb@badengine
    \fi
  \fi
\fi
\lb@next
\unless\ifcsname lb@unexpanded\endcsname
  \let\lb@unexpanded\unexpanded
\fi
\unless\ifcsname lb@input\endcsname
  \let\lb@input\input
\fi


%%% TRIMMING (Vilely lifted from Will Robertson's trimspaces.)

\def\lb@trimleft#1{%
  \expandafter\def\expandafter#1\expandafter{%
    \romannumeral-`\.\expandafter\noexpand#1%
    }%
  }
\bgroup
\catcode`\Q=3
\gdef\lb@trimright#1{%
  \expandafter\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter#1%
  \expandafter\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter{%
  \expandafter\lb@@trimright#1Q Q}%
  }
\gdef\lb@@trimright#1 Q{%
  \lb@@@trimright#1Q%
  }
\gdef\lb@@@trimright#1Q#2{%
  #1%
  }
\egroup
\def\lb@trim#1{%
  \lb@trimleft#1%
  \lb@trimright#1%
  }

%%% KEYWORDS AND GENERAL MACROS

\def\lb@comment{comment} \def\lb@string{string} \def\lb@preamble{preamble} \def\lb@requested{requested}
\def\lb@author{author} \def\lb@editor{editor} \def\lb@namestring{name} \def\lb@endstring{lb@end}
\def\lb@eoe{\lb@eoe} \def\lb@gobbletoeoe#1\lb@eoe,{} \def\lb@gobble#1{}
{\def\:{\global\let\lb@space= }\: } \def\lb@@space{ } \def\lb@empty{}

\def\lb@ifcs#1#2#3#4{%
  \ifcsname#1:lb@#2\endcsname
    #3%
  \else
    #4%
  \fi
  }
\def\lb@defcs#1#2{%
  \expandafter\def\csname#1:lb@#2\endcsname
  }
\def\lb@gdefcs{%
  \global\lb@defcs
  }
\def\lb@edefcs#1#2{%
  \expandafter\edef\csname#1:lb@#2\endcsname
  }
\def\lb@xdefcs{%
  \global\lb@edefcs
  }
\def\lb@cs#1#2{%
  \csname#1:lb@#2\endcsname
  }%
\def\lb@uncs#1#2{%
  \lb@unexpanded\expandafter\expandafter\expandafter{%
    \csname#1:lb@#2\endcsname
    }%
  }%
\def\lb@apptocs#1#2#3{%
  \expandafter\expandafter\expandafter#1\csname#2:lb@#3\endcsname
  }
  
%%% ADDING AND RETRIEVING FIELD

\def\lb@addfield#1#2{%
  \lb@ifcs{#1}{retrieve}%
          {}%
          {\lb@makeretrieve{#1}%
           \WriteImmediateInfo{\noexpand\lb@makeretrieve{#1}}}%
  \lb@edefcs\lb@entrykey{fields}{%
    \lb@unexpanded{[[#1=#2]]}%
    \lb@uncs{\lb@entrykey}{fields}%
    }%
  }%

\def\lb@addvalue#1#2{\lb@addfield{#2}{#1}}
\def\lb@makeretrieve#1{%
  \lowercase{\lb@defcs{#1}{retrieve}##1[[#1=##2]]##3\lb@eoe{##2}}%
  \lowercase{\lb@defcs{#1}{retrievein}##1[[#1=##2]]##3\lb@eoe##4{\def##4{##2}}}%
  }
\def\CreateField#1{%
  \lowercase{\lb@ifcs{#1}{retrieve}}%
          {}%
          {\lb@makeretrieve{#1}}%
  }
\def\lb@@makeretrieve#1,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@eoe
    \lb@makeretrieve{#1}%
    \expandafter\lb@@makeretrieve
  \fi
  }
\lb@@makeretrieve address,annote,author,booktitle,chapter,crossref,%
                  edition,editor,howpublished,institution,journal,%
                  key,month,note,number,organization,pages,publisher,%
                  school,series,title,type,volume,year,file,%
%                 These are specific to Librarian.
                  entrytype,authornumber,editornumber,name,namenumber,nametype,\lb@eoe,%
\def\RetrieveField#1{%
  \RetrieveFieldFor{#1}\EntryKey
  }
\def\RetrieveFieldFor#1#2{%
  \lowercase{\lb@retrievefield{#1}{#2}}%
  }
\def\lb@retrievefield#1#2{%
  \lb@ifcs{#1}{retrieve}%
          {\lb@apptocs\lb@retrieve{#2}{fields}\lb@eoe{#1}}%
          {\lb@error{Unknown field: `#1'}}%
  }
\def\lb@retrieve#1\lb@eoe#2{%
    \lb@cs{#2}{retrieve}#1[[#2=]]\lb@eoe
  }
\def\RetrieveFieldIn#1{%
  \RetrieveFieldInFor{#1}\EntryKey
  }
\def\RetrieveFieldInFor#1#2{%
  \lowercase{\lb@retrievefieldin{#1}{#2}}%
  }%
\def\lb@retrievefieldin#1#2#3{%
  \lb@ifcs{#1}{retrieve}%
          {\lb@apptocs\lb@retrievein{#2}{fields}\lb@eoe{#1}#3}%
          {\lb@error{Unknown field: `#1'}}%
  }
\def\lb@retrievein#1\lb@eoe#2#3{%
  \lb@cs{#2}{retrievein}#1[[#2=]]\lb@eoe#3%
  }
\def\EntryNumber{%
  \EntryNumberFor\EntryKey
  }
\def\EntryNumberFor#1#2{%
  \lowercase{\lb@ifcs{#1@#2}{entrynumber}}%
                     {\lowercase{\lb@cs{#1@#2}{entrynumber}}}%
                     {\lb@error{`#1' doesn't belong to `#2'}}%
  }
\def\EntryNumberIn{%
  \EntryNumberInFor\EntryKey
  }
\def\EntryNumberInFor#1#2#3{%
  \lowercase{\lb@ifcs{#1@#2}{entrynumber}}%
                     {\lowercase{\edef#3{\lb@cs{#1@#2}{entrynumber}}}}%
                     {\lb@error{`#1' doesn't belong to `#2'}}%
  }
\def\TypesetField#1{%
  \TypesetFieldFor{#1}\EntryKey
  }%
\def\TypesetFieldFor#1#2#3#4{%
  \def\lb@temp{}%
  \RetrieveFieldInFor{#1}{#2}\lb@temp
  \ifx\lb@temp\lb@empty
    \def\lb@next{#4}%
  \else
    \def\lb@next{%
      \expandafter#3\expandafter{\lb@temp}%
      }%
  \fi
  \lb@next
  }

%%% CITATION

\newcount\lb@entrycount \newif\iflb@requested
\def\lb@mainlist{}%
\def\Cite#1{%
  \lowercase{\lb@cite{#1}}%
  }%
\def\lb@cite#1#2{%
  \lb@ifcs{#2}{entrylist}%
          {\lb@ifcs{#1@#2}{entrynumber}%
                   {}%
                   {\lb@xdefcs{#2}{entrycount}{\the\numexpr(\lb@cs{#2}{entrycount}+1)}%
                    \lb@xdefcs{#1@#2}{entrynumber}{\lb@cs{#2}{entrycount}}%
                    \lb@xdefcs{#2}{entrylist}{\lb@cs{#2}{entrylist}#1,}%
                    \lb@ifcs{#1}{fields}%
                           {}%
                           {\lb@ifcs{#2}{newentries}%
                                    {\lb@xdefcs{#2}{newentries}{%
                                     \lb@cs{#2}{newentries}#1,}}%
                                    {\lb@xdefcs{#2}{newentries}{#1,}}}}}%
          {\lb@gdefcs{#2}{entrycount}{1}%
           \lb@gdefcs{#1@#2}{entrynumber}{1}%
           \lb@gdefcs{#2}{entrylist}{#1,}%
           \lb@ifcs{#1}{fields}%
                   {}%
                   {\lb@ifcs{#2}{newentries}%
                            {\lb@xdefcs{#2}{newentries}{%
                               \lb@cs{#2}{newentries}#1,}}%
                            {\lb@xdefcs{#2}{newentries}{#1,}}}}%
  \lb@ifcs{#1}{fields}%
          {\lb@ifcs{#1}{done}%
                   {}%
                   {\lb@cs{#1}{done}%
                    \lb@storeentry{#1}}%
           \def\EntryKey{#1}%
           \let\lb@next\lb@first}%
          {\lb@ifcs{#1}{requested}%
                   {}%
                   {\lb@xdefcs{#1}{requested}{}%
                    \xdef\lb@mainlist{\lb@mainlist#1,}%
                    \global\lb@requestedtrue}%
           \let\lb@next\lb@second}%
  \lb@next
  }
\def\lb@first#1#2{#1}
\def\lb@second#1#2{#2}

%%% READING ENTRIES IN BIB FILES

\def\Preamble{}%
\def\lb@checktype#1#{%
  \lowercase{\def\lb@temp{#1}}%
  \ifx\lb@temp\lb@endoffile
    \let\lb@next\endinput
  \else
    \ifx\lb@next\lb@string
      \def\lb@next{\expandafter\lb@gotoat\lb@gobble}%
    \else
      \ifx\lb@temp\lb@comment
        \def\lb@next{\expandafter\lb@gotoat\lb@gobble}%
      \else
        \ifx\lb@temp\lb@preamble
          \xdef\Preamble{\lb@unexpanded\expandafter{\Preamble}#1}%
        \else
          \def\lb@next{\lb@getentry{#1}}%
        \fi
      \fi
    \fi
  \fi
  \lb@next
  }
\def\lb@getentry#1#2{%
  \lb@readentry{#1}#2,=\lb@eoe,%
  }
\def\lb@readentry#1#2,{%
  \lowercase{\def\lb@temp{#2}}%
  \lb@ifcs\lb@temp{requested}%
          {\let\lb@entrykey\lb@temp
           \lb@defcs\lb@entrykey{fields}{}%
           \lowercase{\lb@addfield{entrytype}{#1}}%
           \let\lb@next\lb@analyzeentry}%
          {\def\lb@next{\expandafter\lb@gotoat\lb@gobbletoeoe}}%
  \lb@next
  }
\def\lb@analyzeentry#1={%
  \lowercase{\def\lb@field{#1}}%
  \futurelet\lb@nextchar\lb@@analyzeentry
  }
\def\lb@@analyzeentry{%
  \ifx\lb@nextchar\lb@eoe
    \RetrieveFieldInFor{author}\lb@entrykey\lb@temp
    \ifx\lb@temp\lb@empty
      \RetrieveFieldInFor{editor}\lb@entrykey\lb@temp
      \unless\ifx\lb@temp\lb@empty
        \expandafter\lb@addvalue\expandafter{\lb@temp}{name}%
        \RetrieveFieldInFor{editornumber}\lb@entrykey\lb@temp
        \expandafter\lb@addvalue\expandafter{\lb@temp}{namenumber}%
        \lb@addfield{nametype}{editor}%
      \fi
    \else
      \expandafter\lb@addvalue\expandafter{\lb@temp}{name}%
       \RetrieveFieldInFor{authornumber}\lb@entrykey\lb@temp
       \expandafter\lb@addvalue\expandafter{\lb@temp}{namenumber}%
      \lb@addfield{nametype}{author}%
    \fi
    \lb@xdefcs{\lb@entrykey}{fields}{\lb@uncs\lb@entrykey{fields}}%
    \def\lb@next{\expandafter\lb@gotoat\lb@gobbletoeoe}%
  \else
    \lb@trim\lb@field
    \def\lb@next{\futurelet\lb@nextchar\lb@getvalue}%
  \fi
  \lb@next
  }
\def\lb@getvalue{%
  \if\noexpand\lb@nextchar\lb@space
    \def\lb@next{%
      \def\lb@next{\futurelet\lb@nextchar\lb@getvalue}%
      \afterassignment\lb@next\let\lb@nextchar=
      }%
  \else
    \if\noexpand\lb@nextchar"%
      \let\lb@next\lb@readvalue
    \else
      \if\noexpand\lb@nextchar\bgroup
        \let\lb@next\lb@@readvalue
      \else
        \let\lb@next\lb@@@readvalue
      \fi
    \fi
  \fi
  \lb@next
  }
\def\lb@readvalue"#1"#2,{\lb@dovalue{#1}}
\def\lb@@readvalue#1#2,{\lb@dovalue{#1}}
\def\lb@@@readvalue#1,{\lb@dovalue{#1}}
\def\lb@dovalue#1{%
  \ifx\lb@field\lb@author
    \def\lb@tempname{}%
    \lb@namecount=0
    \lb@splitnames#1 and lb@end and %
    \expandafter\lb@addvalue\expandafter{\lb@tempname}{author}%
    \expandafter\lb@addvalue\expandafter{\the\lb@namecount}{authornumber}%
  \else
    \ifx\lb@field\lb@editor
      \def\lb@tempname{}%
      \lb@namecount=0
      \lb@splitnames#1 and lb@end and %
      \lb@trim\lb@tempname
      \expandafter\lb@addvalue\expandafter{\lb@tempname}{editor}%
      \expandafter\lb@addvalue\expandafter{\the\lb@namecount}{editornumber}%
    \else
      \expandafter\lb@addfield\expandafter{\lb@field}{#1}%
    \fi
  \fi
  \lb@analyzeentry
  }

%%% ANALYZING NAMES

\newcount\lb@namecount
\newcount\lb@tempcount
\newif\iflb@von \newif\iflb@group
\def\lb@splitnames#1 and {%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@endstring
    \def\lb@name{}\def\lb@lastname{}%
    \def\lb@firstname{}\def\lb@von{}%
    \def\lb@junior{}\def\lb@temporary{}%
    \lb@vonfalse \advance\lb@namecount1
    \lb@scanname#1,\lb@eoe,\lb@eoe,%
    \expandafter\lb@splitnames
  \fi
  }
\def\lb@scanname#1,#2,#3,{%
  \def\lb@temp{#2}%
  \ifx\lb@temp\lb@eoe
    \let\lb@getrest\lb@withfirst
    \def\lb@next{\futurelet\lb@nextchar\lb@analyzename#1 \lb@eoe}%
  \else\let\lb@next\relax
    \let\lb@getrest\lb@nofirst
    \def\lb@temp{#3}%
    \ifx\lb@temp\lb@eoe
      \expandafter\def\expandafter\lb@firstname\expandafter{\romannumeral-`\:\noexpand#2}%
      \def\lb@next{%
        \def\lb@next{\futurelet\lb@nextchar\lb@analyzename#1 \lb@eoe}%
        \expandafter\lb@next\lb@commagobble
        }%
    \else
      \expandafter\def\expandafter\lb@junior\expandafter{\romannumeral-`\:\noexpand#2}%
      \expandafter\def\expandafter\lb@firstname\expandafter{\romannumeral-`\:\noexpand#3}%
      \def\lb@next{%
        \def\lb@next{\futurelet\lb@nextchar\lb@analyzename#1 \lb@eoe}%
        \expandafter\expandafter\expandafter\lb@next\expandafter\lb@commagobble\lb@commagobble
        }%
    \fi
  \fi\lb@next
  }
\def\lb@commagobble#1,{}%
\def\lb@analyzename#1{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \def\lb@next{\expandafter\lb@getlast\lb@name\lb@eoe}%
  \else
    \if\noexpand\lb@nextchar\bgroup
      \def\lb@next{\lb@grabword{{#1}}}%
    \else
      \def\lb@next{\lb@grabword#1}%
    \fi
  \fi\lb@next
  }
\def\lb@getlast#1{%
  \def\lb@lastname{#1}%
  \lb@trim\lb@lastname
  \lb@getrest
  }
\def\lb@grabword#1#2 {%
  \edef\lb@name{{\lb@unexpanded{#1#2}}\lb@unexpanded\expandafter{\lb@name}}%
  \futurelet\lb@nextchar\lb@analyzename
  }
\def\lb@checkbrace#1\lb@eoe{%
  \if\noexpand\lb@nextchar\bgroup
    \lb@grouptrue
  \else
    \lb@groupfalse
  \fi
  }
\def\lb@withfirst#1{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \expandafter\def\expandafter\lb@firstname\expandafter{\lb@temporary}%
    \let\lb@next\lb@storename
  \else
    \futurelet\lb@nextchar\lb@checkbrace#1\lb@eoe
    \let\lb@next\lb@withfirst
    \iflb@group
      \edef\lb@temporary{\lb@unexpanded{#1}%
      \unless\ifx\lb@temporary\lb@empty
        \lb@@space
        \lb@unexpanded\expandafter{\lb@temporary}%
      \fi}%
    \else
      \lb@getfirstletter#1 %
    \fi
  \fi\lb@next
  }
\def\lb@empty{}
\def\lb@getfirstletter#1#2 {%
  \ifnum\lccode`#1=`#1
    \iflb@von
      \edef\lb@von{\lb@unexpanded{#1#2}%
      \unless\ifx\lb@temporary\lb@empty
        \lb@@space\lb@unexpanded\expandafter{\lb@temporary}\lb@@space
      \fi
      \lb@unexpanded\expandafter{\lb@von}}%
      \def\lb@temporary{}%
    \else
      \lb@vontrue
      \edef\lb@lastname{%
        \unless\ifx\lb@temporary\lb@empty
          \lb@@space\lb@unexpanded\expandafter{\lb@temporary}%
        \fi
        \lb@unexpanded\expandafter{\lb@lastname}%
        }%
      \def\lb@temporary{}%
      \def\lb@von{#1#2}%
    \fi
  \else
    \edef\lb@temporary{%
      \lb@unexpanded{#1#2}%
      \unless\ifx\lb@temporary\lb@empty
        \lb@@space\lb@unexpanded\expandafter{\lb@temporary}%
      \fi}%
  \fi
  }%
\def\lb@nofirst#1{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \let\lb@next\lb@storename
  \else
    \futurelet\lb@nextchar\lb@checkbrace#1\lb@eoe
    \let\lb@next\lb@nofirst
    \iflb@von
      \edef\lb@von{%
        \lb@unexpanded{#1}%
        \unless\ifx\lb@von\lb@empty
          \lb@@space\lb@unexpanded\expandafter{\lb@von}%
        \fi}%
    \else
      \iflb@group
        \edef\lb@lastname{%
          \lb@unexpanded{#1}%
          \unless\ifx\lb@lastname\lb@empty
            \lb@@space\lb@unexpanded\expandafter{\lb@lastname}%
          \fi}%
      \else
        \lb@@getfirstletter#1 %
      \fi
    \fi
  \fi\lb@next
  }
\def\lb@@getfirstletter#1#2 {%
  \ifnum\lccode`#1=`#1
    \lb@vontrue
    \edef\lb@von{%
      \lb@unexpanded{#1#2}%
      \unless\ifx\lb@von\lb@empty
        \lb@@space\lb@unexpanded\expandafter{\lb@von}%
      \fi
    }%
  \else
    \edef\lb@lastname{%
      \lb@unexpanded{#1#2}%
        \unless\ifx\lb@lastname\lb@empty
          \lb@@space\lb@unexpanded\expandafter{\lb@lastname}%
        \fi
      }%
  \fi
  }
\def\lb@storename{%
  \edef\lb@tempname{%
    \unless\ifx\lb@tempname\lb@empty
      \lb@unexpanded\expandafter{\lb@tempname}%
    \fi
    {{\lb@unexpanded\expandafter{\lb@firstname}}%
    {\lb@unexpanded\expandafter{\lb@von}}%
    {\lb@unexpanded\expandafter{\lb@lastname}}%
    {\lb@unexpanded\expandafter{\lb@junior}}}%
    }%
  }

%%% SORTING

\def\SortingOrder#1#2{%
  \def\lb@sortingorder{}%
  \expandafter\lb@makesortingorder#1,\lb@eoe,%
%
  \edef\lb@tempnameorder{#2}%
  \def\lb@nameorder{}%
  \lb@namesortingorder#2\lb@eoe
  }
\def\lb@makesortingorder#1,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@eoe
    \unless\ifx\lb@temp\lb@empty
      \lb@ifcs{#1}{retrieve}%
              {}%
              {\lb@makeretrieve{#1}}%
      \edef\lb@sortingorder{\lb@sortingorder#1,}%
    \fi
    \expandafter\lb@makesortingorder
  \fi
  }
\def\lb@nameorderdef{\gdef\lb@nameorder_1_2_3_4}%
\def\lb@namesortingorder#1{%
  \ifx#1\lb@eoe
    \bgroup
    \catcode`\_=6
    \catcode`\@=11
      \scantokens\expandafter\expandafter\expandafter{%
        \expandafter\lb@nameorderdef\expandafter{\lb@nameorder}% A space is created here. Why?
        }%
    \egroup
    \let\lb@next\relax
  \else
    \let\lb@next\lb@namesortingorder
    \if#1f%
      \edef\lb@nameorder{\lb@unexpanded\expandafter{\lb@nameorder} _1}%
    \else\if#1v%
      \edef\lb@nameorder{\lb@unexpanded\expandafter{\lb@nameorder} _2}%
    \else\if#1l%
      \edef\lb@nameorder{\lb@unexpanded\expandafter{\lb@nameorder} _3}%
    \else\if#1j%
      \edef\lb@nameorder{\lb@unexpanded\expandafter{\lb@nameorder} _4}%
    \else
      \lb@error{`#1' is not valid in \SortingOrder}%
    \fi\fi\fi\fi
  \fi\lb@next
  }
\SortingOrder{}{}
\newcount\lb@sortnumber
\newif\iflb@tie \newif\iflb@puttie
\def\lb@tie{\lb@tie} \def\lb@endtie{\lb@endtie}
\def\lb@addtosorted#1{%
  \xdef\lb@sortedentries{\lb@unexpanded\expandafter{\lb@sortedentries}#1,}%
  }
\def\lb@@addtosorted#1{%
  \xdef\lb@sortedentries{\lb@unexpanded\expandafter{\lb@sortedentries}#1}%
  }
\def\lb@sort#1,{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \lb@addtosorted\lb@entrykey
    \let\lb@next\relax
  \else
    \ifx\lb@temp\lb@tie
      \lb@tietrue \lb@puttietrue
      \let\lb@next\lb@sort
    \else
      \ifx\lb@temp\lb@endtie
        \ifnum\lb@sortnumber=0
          \lb@addtosorted{\lb@entrykey,\noexpand\lb@endtie}%
          \let\lb@next\lb@finishlist
        \else
          \lb@addtosorted{\noexpand\lb@endtie}%
          \let\lb@next\lb@sort
        \fi
        \lb@tiefalse
      \else
        \def\lb@currententry{#1}%
        \expandafter\lb@sortby\lb@sortingorder,\lb@eoe,%
        \ifnum\lb@sortnumber=0
          \iflb@tie
            \iflb@puttie
              \lb@addtosorted{\noexpand\lb@tie}%
              \lb@puttiefalse
            \fi
            \ifnum\lb@cs{\lb@entrykey @\lb@currentlist}{entrynumber}<\lb@cs{#1@\lb@currentlist}{entrynumber}%
              \lb@addtosorted{\lb@entrykey,#1}%
              \let\lb@next\lb@finishlist
            \else
              \lb@addtosorted{#1}%
              \let\lb@next\lb@sort
            \fi
          \else
            \ifnum\lb@cs{\lb@entrykey @\lb@currentlist}{entrynumber}<\lb@cs{#1@\lb@currentlist}{entrynumber}%
              \lb@addtosorted{\noexpand\lb@tie,\lb@entrykey,#1,\noexpand\lb@endtie}%
            \else
              \lb@addtosorted{\noexpand\lb@tie,#1,\lb@entrykey,\noexpand\lb@endtie}%
            \fi
            \let\lb@next\lb@finishlist
          \fi
        \else
          \ifnum\lb@sortnumber\lb@sign0
            \lb@tiefalse
            \lb@addtosorted\lb@entrykey
            \iflb@puttie
              \lb@addtosorted{\noexpand\lb@tie}%
              \lb@puttiefalse
            \fi
            \lb@addtosorted{#1}%
            \let\lb@next\lb@finishlist
          \else
            \iflb@puttie
              \lb@addtosorted{\noexpand\lb@tie}%
              \lb@puttiefalse
            \fi
            \lb@addtosorted{#1}%
            \let\lb@next\lb@sort
          \fi
        \fi
      \fi
    \fi
  \fi
  \lb@next
  }
\def\lb@finishlist#1\lb@eoe,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@empty
    \lb@@addtosorted{\lb@unexpanded{#1}}%
  \fi
  }%
\def\lb@sign{<}%
\def\lb@sortby#1,{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@empty
    \let\lb@next\lb@sortby
  \else
    \ifx\lb@temp\lb@eoe
      \lb@sortnumber=0
      \let\lb@next\relax
    \else
      \bgroup
      \edef\lb@temp{#1}%
      \expandafter\lb@preparesort\lb@temp\lb@eoe
      \egroup
      \ifnum\lb@sortnumber=0
        \let\lb@next\lb@sortby
      \else
        \let\lb@next\lb@gobbletoeoe
      \fi
    \fi
  \fi
  \lb@next
  }
\def\lb@preparesort#1#2\lb@eoe{%
  \if#1-%
    \gdef\lb@sign{>}%
    \def\lb@temp{#2}%
  \else
    \gdef\lb@sign{<}%
    \def\lb@temp{#1#2}%
  \fi
  \expandafter\RetrieveFieldInFor\expandafter{\lb@temp}\lb@entrykey\lb@firstfield
  \expandafter\RetrieveFieldInFor\expandafter{\lb@temp}\lb@currententry\lb@secondfield
  \ifx\lb@temp\lb@author
    \lb@preparenames{author}\lb@entrykey\lb@firstfield
    \lb@preparenames{author}\lb@currententry\lb@secondfield
  \else\ifx\lb@temp\lb@editor
    \lb@preparenames{editor}\lb@entrykey\lb@firstfield
    \lb@preparenames{editor}\lb@currententry\lb@secondfield
  \else\ifx\lb@temp\lb@namestring
    \lb@preparenames{name}\lb@entrykey\lb@firstfield
    \lb@preparenames{name}\lb@currententry\lb@secondfield
  \else
    \catcode`\ =9
  \fi\fi\fi
  \expandafter\expandafter\expandafter\lb@compare
  \expandafter\lb@firstfield\expandafter\lb@eoe\lb@secondfield\lb@eoe
  }
\def\lb@preparenames#1#2#3{%
  \expandafter\RetrieveFieldInFor\expandafter{#1number}#2\lb@@temp
  \ifx\lb@@temp\lb@empty
    \def\lb@@temp{0}%
  \fi
  \expandafter\ifnum\lb@@temp=1
    \def\lb@@temp{}%
    \expandafter\lb@loopover\expandafter{#3}\lb@eoe
  \else
    \def\lb@@temp{}%
    \expandafter\lb@loopover#3\lb@eoe
  \fi
  \let#3\lb@@temp
  }
\def\lb@loopover#1{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \let\lb@next\relax
  \else
    \lb@makename#1%
    \let\lb@next\lb@loopover
  \fi\lb@next
  }
\def\lb@makename#1#2#3#4{%
  \edef\lb@@temp{%
    \lb@unexpanded\expandafter{\lb@@temp}
    \lb@unexpanded\expandafter{\lb@nameorder{#1}{#2}{#3}{#4}}%
    }%
  }
\def\lb@compare#1\lb@eoe#2\lb@eoe{%
  \catcode`\<=1 \catcode`\>=2
  \catcode`\{=13 \catcode`\}=13
  \catcode`\@=11
  \lb@changemacros
  \everyeof={}%
  \scantokens{%
    \let{\lb@empty \let}\lb@empty
    \lowercase<\global\lb@sortnumber=\lb@strcmp<#1><#2>>%
    }%
  }
\def\lb@changemacros{}%
\def\SortDef#1#2{%
  \edef\lb@changemacros{%
    \lb@unexpanded\expandafter{\lb@changemacros%
    \def#1{#2}}%
    }%
  }

%%% BIB FILES

\def\lb@readbib#1{%
  \iflb@requested
    \global\lb@requestedfalse
    \openin\lb@read=#1\relax
    \ifeof\lb@read
      \lb@error{I can't find file `#1'}%
    \else
      \bgroup
        \endlinechar=`\ %
        \catcode`\%=12
        \lb@makeendoffile
        \catcode`\@=12
        \expandafter\lb@gotoat\lb@input#1\relax
      \egroup
    \fi
    \expandafter\lb@checkentrylist\lb@mainlist\lb@eoe,%
  \fi
  }
\def\BibFile#1{%
  \lb@bibfile#1,\lb@eoe,%
  \iflb@requested
    \expandafter\lb@checkmissingentries\lb@mainlist\lb@eoe,%
  \fi
  }
{\catcode`\#=12 \gdef\hash{#}}
\def\lb@storelist#1,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@eoe
    \lb@ifcs{#1}{fields}%
            {\lb@storeentry{#1}}%
            {}%
    \expandafter\lb@storelist
  \fi
  }
\def\lb@bibfile#1,{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \let\lb@next\relax
  \else
    \let\lb@next\lb@bibfile
    \unless\ifx\@temp\lb@empty
      \def\lb@file{#1}%
      \lb@checkextension#1.\lb@eoe
      \lb@readbib\lb@file
    \fi
  \fi
  \lb@next
  }
\def\lb@checkextension#1.#2\lb@eoe{%
  \def\lb@temp{#2}%
  \ifx\lb@temp\lb@empty
    \def\lb@file{#1.bib}%
  \fi
  }
\bgroup
\catcode`\@=12
\expandafter\xdef\csname lb@gotoat\endcsname#1@{%
  \csname lb@unexpanded\endcsname\expandafter{\csname lb@checktype\endcsname}%
  }
\expandafter\gdef\csname lb@makeendoffile\endcsname{%
  \everyeof={@lb@endoffile{}}%
  }
\expandafter\gdef\csname lb@endoffile\endcsname{lb@endoffile}
\long\expandafter\gdef\csname lb@readlbr\endcsname#1@lb@endoffile{%
  \expandafter\gdef\csname lb@dolbr\endcsname{#1}}
\egroup
\def\lb@checkentrylist#1,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@eoe
    \lb@ifcs{#1}{fields}%
            {\lb@storeentry{#1}}%
            {\global\lb@requestedtrue}%
    \expandafter\lb@checkentrylist
  \fi
  }
\def\lb@checkmissingentries#1,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@eoe
    \lb@ifcs{#1}{fields}%
            {}%
            {\lb@error{Entry `#1' wasn't found}}%
    \expandafter\lb@checkmissingentries
  \fi
  }

%%% SORTING AND READING LISTS

\newif\iflb@resort
\def\SortList#1{%
  \def\lb@sortedentries{}%
  \def\lb@currentlist{#1}%
  \lb@ifcs{#1}{entrylist}%
          {\unless\ifx\lb@sortingorder\lb@empty
             \lb@resorttrue
             \expandafter\ifx\csname#1:lb@prevorder\endcsname\lb@sortingorder
               \expandafter\ifx\csname#1:lb@prevnameorder\endcsname\lb@tempnameorder
                 \lb@resortfalse
               \fi
             \fi
             \iflb@resort
               \bgroup
                 \tracingmacros0 \tracingcommands0 \tracingrestores0
                 \tracingscantokens0 \tracingassigns0 \tracingifs0
                 \tracinggroups0 \tracingnesting0
                 \def\lb@sortedentries{}%
                 \lb@apptocs\lb@sortlist{#1}{entrylist}\lb@eoe,%
               \egroup
             \else
               \def\lb@sortedentries{}%
               \lb@apptocs\lb@removeold{#1}{prevsorted}\lb@eoe,%
               \unless\ifx\lb@sortedentries\lb@empty
                 \let\lb@temp\lb@sortedentries
                 \def\lb@sortedentries{}%
                 \expandafter\lb@checkties\lb@temp\lb@tie,\lb@eoe\lb@endtie,%
               \fi
               \edef\lb@newentries{%
                 \lb@ifcs{#1}{newentries}%
                         {\lb@cs{#1}{newentries}}%
                         {}%
                   \lb@cs{#1}{prevoldnew}%
                   }%
                  \tracingmacros3 \tracingcommands3
                  \expandafter\lb@sortlist\lb@newentries\lb@eoe,%
             \fi
             \expandafter\let\csname#1:lb@entrylist\endcsname\lb@sortedentries
           \fi
           \WriteImmediateInfo{\noexpand\lb@makelist{#1}%
                        {\lb@ifcs{#1}{oldnew}%
                                 {\lb@cs{#1}{oldnew}}%
                                 {}}%
                        {\lb@sortingorder}{\lb@tempnameorder}%
                        {\lb@uncs{#1}{entrylist}}}}%
          {\lb@error{List `#1' doesn't exist}}%
  }
\def\lb@removeold#1,{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \let\lb@next\relax
  \else
    \let\lb@next\lb@removeold
    \ifx\lb@temp\lb@tie
      \lb@addtosorted{\noexpand\lb@tie}%
    \else
      \ifx\lb@temp\lb@endtie
        \lb@addtosorted{\noexpand\lb@endtie}%
      \else
        \lb@ifcs{#1@\lb@currentlist}{entrynumber}%
                {\lb@addtosorted{#1}}%
                {}%
      \fi
    \fi
  \fi\lb@next
  }
\newif\iflb@onetie
\def\lb@checkties#1\lb@tie,#2\lb@endtie,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@empty
    \lb@@addtosorted{#1}%
  \fi
  \def\lb@temp{#2}%
  \ifx\lb@temp\lb@eoe
    \let\lb@next\relax
  \else
    \let\lb@next\lb@checkties
    \unless\ifx\lb@temp\lb@empty
      \lb@countties#2\lb@eoe,%
      \iflb@onetie
        \lb@@addtosorted{#2}%
      \else
        \def\lb@templist{}%
        \lb@sortties#2\lb@eoe,%
        \let\lb@next\lb@checkties
        \lb@addtosorted{\noexpand\lb@tie,\lb@templist\noexpand\lb@endtie}%
      \fi
    \fi
  \fi\lb@next
  }
\def\lb@countties#1,#2,{%
  \def\lb@temp{#2}%
  \ifx\lb@temp\lb@eoe
    \lb@onetietrue
  \else
    \lb@onetiefalse
    \expandafter\lb@gobbletoeoe
  \fi
  }%
\def\lb@sortties#1,{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \let\lb@next\relax
  \else
    \let\lb@temp\lb@templist
    \def\lb@templist{}%
    \def\lb@entrykey{#1}%
    \expandafter\lb@@sortties\lb@temp\lb@eoe,%
    \let\lb@next\lb@sortties
  \fi\lb@next
  }
\def\lb@@sortties#1,{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \edef\lb@templist{\lb@templist\lb@entrykey,}%
    \let\lb@next\relax
  \else
    \ifnum\lb@cs{\lb@entrykey @\lb@currentlist}{entrynumber}<\lb@cs{#1@\lb@currentlist}{entrynumber}%
      \edef\lb@templist{\lb@templist\lb@entrykey,#1,}%
      \let\lb@next\lb@finishties
    \else
      \edef\lb@templist{\lb@templist#1,}%
      \let\lb@next\lb@@sortties
    \fi
  \fi
  \lb@next
  }
\def\lb@finishties#1\lb@eoe,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@empty
    \edef\lb@templist{\lb@templist#1}%
  \fi
  }%
\def\lb@sortlist#1,{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@eoe
    \lb@ifcs{#1}{fields}%
            {\def\lb@entrykey{#1}%
             \let\lb@temp\lb@sortedentries
             \def\lb@sortedentries{}%
             \expandafter\lb@sort\lb@temp\lb@eoe,}%
            {\lb@ifcs{\lb@currentlist}{oldnew}%
                     {\lb@xdefcs\lb@currentlist{oldnew}{%
                        \lb@cs{\lb@currentlist}{oldnew}#1,}}%
                     {\lb@xdefcs\lb@currentlist{oldnew}{#1,}}}%
    \expandafter\lb@sortlist
  \fi
  }
\newif\ifequalentry
\def\ReadList#1{%
  \lb@ifcs{#1}{entrylist}%
          {\let\lb@makecitename\MakeCiteName
           \let\MakeCiteName\MakeRefName
           \lb@apptocs\lb@loopoverentries{#1}{entrylist}\lb@eoe,%
           \let\MakeCiteName\lb@makecitename}%
          {\lb@error{List `#1' doesn't exist}}%
  }
\def\lb@loopoverentries#1,{%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@eoe
    \let\lb@next\relax
  \else
    \let\lb@next\lb@loopoverentries
    \ifx\lb@temp\lb@tie
      \equalentrytrue
    \else
      \ifx\lb@temp\lb@endtie
        \equalentryfalse
      \else
        \def\EntryKey{#1}%
        \MakeReference
        \let\lb@next\lb@loopoverentries
      \fi
    \fi
  \fi\lb@next
  }
\let\MakeReference\relax
\def\CheckEntry#1#2{%
  \ifcs{#1}{fields}%
       {\def\lb@next{#2}%
        \expandafter\lb@next}%
       {}%
  }
\def\ReadAuthor{%
  \ReadAuthorFor\EntryKey
  }
\def\ReadAuthorFor{%
  \lb@readandloop{author}%
  }%
\let\ReadAuthors\ReadAuthor
\let\ReadAuthorsFor\ReadAuthorFor
\def\ReadEditor{%
  \ReadEditorFor\EntryKey
  }
\def\ReadEditorFor{%
  \lb@readandloop{editor}%
  }%
\let\ReadEditors\ReadEditor
\let\ReadEditorsFor\ReadEditorFor
\def\ReadName{%
  \ReadNameFor\EntryKey
  }
\def\ReadNameFor{%
  \lb@readandloop{name}%
  }%
\let\ReadNames\ReadName
\let\ReadNamesFor\ReadNameFor
\newcount\NameCount
\def\lb@readandloop#1#2#3{%
  \def\lb@makerefname{#3}%
  \NameCount=0
  \RetrieveFieldInFor{#1number}{#2}\lb@temp
  \ifx\lb@temp\lb@empty
    \def\lb@temp{0}%
  \fi
  \ifcase\lb@temp\relax
  \or
    \RetrieveFieldInFor{#1}{#2}\lb@temp
    \expandafter\lb@loopovernames\expandafter{\lb@temp}\lb@eoe
  \else
    \RetrieveFieldInFor{#1}{#2}\lb@temp
    \expandafter\lb@loopovernames\lb@temp\lb@eoe
  \fi
  }  
\def\lb@loopovernames#1{%
  \def\lb@temp{#1}%
  \unless\ifx\lb@temp\lb@eoe
    \advance\NameCount1
    \lb@@loopovernames#1%
    \expandafter\lb@loopovernames
  \fi
  }
\def\lb@@loopovernames#1#2#3#4{%
  \iflb@abbreviate
    \def\Firstname{}%
    \lb@abbreviate#1 lb@end
  \else
    \def\Firstname{#1}%
  \fi
  \def\Von{#2}\def\Lastname{#3}\def\Junior{#4}%
  \lb@makerefname
  }
\newif\iflb@abbreviate
\def\lb@abbreviate#1 {%
  \def\lb@temp{#1}%
  \ifx\lb@temp\lb@endstring
    \lb@trim\Firstname
  \else
    \lb@checkhyphen#1--\lb@eoe,%
    \expandafter\lb@abbreviate
  \fi
  }
\def\lb@checkhyphen#1-#2-{%
  \lb@makeinitial#1\lb@eoe
  \edef\Firstname{\Firstname\lb@@space\lb@temp}%
  \def\lb@temp{#2}%
  \unless\ifx\lb@temp\lb@empty
    \lb@makeinitial#2\lb@eoe
    \edef\Firstname{\Firstname-\lb@temp}%
  \fi
  \lb@gobbletoeoe
  }
\def\lb@makeinitial#1#2\lb@eoe{%
  \def\lb@temp{#1\spacefactor1000.}%
  }
\def\AbbreviateFirstname{\lb@abbreviatetrue}

%%% EXTERNAL FILE

\def\lb@makelist#1#2#3#4#5{%
  \lb@defcs{#1}{prevoldnew}{#2}%
  \lb@defcs{#1}{prevorder}{#3}%
  \lb@defcs{#1}{prevnameorder}{#4}%
  \lb@defcs{#1}{prevsorted}{#5}%
  }%
\def\WriteImmediateInfo{%
  \immediate\write\lb@write
  }
\def\WriteInfo{%
  \write\lb@write
  }
\def\lb@storeentry#1{%
  \WriteImmediateInfo{%
    \noexpand\lb@defcs{#1}{fields}{\lb@uncs{#1}{fields}}%
    }%
  }
\newread\lb@read 
\openin\lb@read=\jobname.lbr
\ifeof\lb@read
  \closein\lb@read
\else
  \closein\lb@read
  \bgroup
    \lb@makeendoffile
    \expandafter\lb@readlbr\lb@input\jobname.lbr
  \egroup
\fi
\newwrite\lb@write
\immediate\openout\lb@write=\jobname.lbr
\lb@dolbr
\lb@restoreat
\endinput