%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
% This is file 'pagerange.sty', version 0.5.                                %
%                                                                           %
% This work may be distributed and/or modified under the conditions         %
% of the LaTeX Project Public License, either version 1.3 of this           %
% license or any later version. The latest version of this license          %
% is in http://www.latex-project.org/lppl.txt and version 1.3 or later      %
% is part of all distributions of LaTeX version 2005/12/01 or later.        %
%                                                                           %
% The LPPL maintenance status of this software is 'author-maintained'.      %
%                                                                           %
% This software is provided 'as it is', without warranty of any kind,       %
% either expressed or implied, including, but not limited to, the           %
% implied warranties of merchantability and fitness for a particular        %
% purpose.                                                                  %
%                                                                           %
% Copyright (c) 2010 Ahmed Musa (a.musa@rocketmail.com).                    %
%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
\begingroup
\catcode064 11\relax% @
\catcode123 01\relax% {
\catcode125 02\relax% }
\def\prg@tempa{\endgroup
  \edef\prg@restorecodes{%
    \catcode035 \the\catcode035\relax% #
    \catcode064 \the\catcode064\relax% @
    \catcode123 \the\catcode123\relax% {
    \catcode125 \the\catcode125\relax% }
    \catcode061 \the\catcode061\relax% =
    \catcode038 \the\catcode038\relax% &
  }%
}
\prg@tempa
\catcode061 12\relax% =
\catcode035 06\relax% #
\catcode064 11\relax% @
\catcode123 01\relax% {
\catcode125 02\relax% }
\catcode038 03\relax% &
\AtEndOfPackage{\prg@restorecodes}
\ProvidesPackage{pagerange}%
  [2010/02/04 v0.5 Split page ranges intelligently (Ahmed Musa)]
\NeedsTeXFormat{LaTeX2e}[1996/12/01]
\@ifpackageloaded{lastpage}{}{\RequirePackage{lastpage}[1994/06/25]}
\@ifpackageloaded{xkeyval}{}{\RequirePackage{xkeyval}[2008/08/13]}
\ifcase0%
  \ifx\eTeXversion\@undefined 1\else\ifnum\eTeXversion<\tw@ 1\fi\fi
\else
  \@latex@error{eTeX not loaded or old version.\MessageBreak
    This package requires eTeX version 2 or later.}\@ehc
\fi
\newcommand*\prg@ifdefinable[1]{%
  \edef\reserved@a{\expandafter\@gobble\string#1}%
  \begingroup
  \ifdefined#1%
    \aftergroup\@notdefinable
  \else
    \edef\reserved@b{\expandafter\@carcube\reserved@a xxx\@nil}%
    \ifx\reserved@b\@qend
      \aftergroup\@notdefinable
    \else
      \ifx\reserved@a\@qrelax
        \aftergroup\@notdefinable
      \else
        \aftergroup\@firstofone
      \fi
    \fi
  \fi
  \endgroup
}
\prg@ifdefinable\prg@protect@relax\relax
\prg@ifdefinable\prg@star@or@long\relax
\prg@ifdefinable\prg@pdef\relax
\prg@ifdefinable\prg@@def\relax
\def\prg@star@or@long#1{%
  \@ifstar{\let\prg@protect@relax\protected#1}%
    {\def\prg@protect@relax{\protected\long}#1}%
}
\let\prg@protect@relax\protected
\protected\def\prg@pdef{\prg@star@or@long\prg@@def}
\def\prg@@def#1{\prg@ifdefinable#1{\prg@protect@relax\def#1}}
\prg@pdef\prg@warn#1{\PackageWarningNoLine{pagerange}{#1}}
\prg@pdef\prg@err#1{\PackageError{pagerange}{#1}\@ehc}
\prg@ifdefinable\prg@tempcnta{\newcount\prg@tempcnta}
\prg@ifdefinable\prg@tempcntb{\newcount\prg@tempcntb}
\prg@ifdefinable\prg@tempcntc{\newcount\prg@tempcntc}
\def\prg@c@r#1#2{%
  \unless\ifx#2\null
    \prg@err{Inadmissible multiple arguments '\string#1\string#2'
      \MessageBreak for '\string\prg@ifdef' test}%
  \fi
}
\prg@ifdefinable\prg@ifdef\relax
\def\prg@ifdef#1{%
  \prg@c@r#1\null
  \csname @\ifdefined#1\expandafter first\else
    \expandafter second\fi oftwo\endcsname
}
\prg@ifdefinable\prg@ifundef\relax
\def\prg@ifundef#1{%
  \prg@ifdef{#1}{\csname @\ifx#1\relax\expandafter first\else
    \expandafter second\fi oftwo\endcsname}{\@firstoftwo}%
}
\def\prg@ifcsundef#1{\expandafter\prg@ifundef\csname#1\endcsname}
\prg@pdef*\prg@check@ifdef#1{%
  \prg@ifcsundef{#1}{}{%
    \prg@err{\expandafter\noexpand\csname#1\endcsname already defined}%
  }%
  \prg@check@ifdef
}
\prg@check@ifdef{pagerange}{pagestart}{pageend}{getpagenumber}%
  {pagerangeoptions}\iffalse\fi\fi\endcsname{}{}
\prg@pdef\prg@ifblank#1{\prg@ifbl@nk#1&&\@secondoftwo\@firstoftwo:}
\long\def\prg@ifbl@nk#1#2&#3#4#5:{#4}
\prg@pdef\prg@xifblank#1{\begingroup
  \protected@edef\prg@tempa{\endgroup\noexpand\prg@ifblank{#1}}%
  \prg@tempa
}
\prg@pdef\prg@ifmacro#1{%
  \begingroup
  \edef\prg@tempa{\endgroup
    \def\noexpand\getparameter####1\detokenize{macro}:####2&{%
      \noexpand\prg@ifblank{####2}{\noexpand\@secondoftwo}%
        {\noexpand\@firstoftwo}%
    }%
    \noexpand\expandafter\noexpand\getparameter%
    \noexpand\meaning\noexpand#1\detokenize{macro}:&%
  }\prg@tempa
}
\def\prg@ismacro@err#1{%
  \prg@err{%
    Illegal tie '#1'.\MessageBreak
    Note that the 'tie' has been expanded once\MessageBreak
    and may be different from what you submitted.\MessageBreak
    Perhaps you've used an active character as 'tie'
  }%
}
\define@cmdkey[KV]{pagerange}[prg@]{tie}[-]{%
  \edef\@tempa{\expandafter\noexpand\prg@tie}%
  \edef\prg@tie{\prg@tie}%
  \let\@@protect\protect\let\protect\@empty
  \expandafter\prg@ifmacro\expandafter{\prg@tie}{%
    \restore@protect\prg@ismacro@err\@tempa
  }{}%
  \restore@protect
}
\define@boolkey[KV]{pagerange}[prg@]{acceptempty}[true]{}
\define@cmdkey[KV]{pagerange}[prg@]{defaultfirstpage}[1]{}
\define@cmdkey[KV]{pagerange}[prg@]{defaultlastpage}%
  [\getpagenumber{LastPage}]{}
\DeclareOptionX*{\prg@warn{Unknown option '\CurrentOption' ignored}}
\ExecuteOptionsX<pagerange>{tie,defaultfirstpage,defaultlastpage}
\ProcessOptionsX*<pagerange>\relax
\long\def\pagerangeoptions#1{\setkeys[KV]{pagerange}{#1}}
\prg@pdef\prg@forall#1#2\do#3{%
  #3{#1}%
  \expandafter\ifx\expandafter\@empty\detokenize{#2}\@empty%
    \relax
  \else
    \prg@forall#2\do{#3}%
  \fi
}
\let\pagestart\@empty
\let\pageend\@empty
\prg@tempcntc=\z@
\prg@pdef*\prg@split@pagerange#1{%
  \def\reserved@a{#1}%
  \ifx\reserved@a\prg@tie
    \advance\prg@tempcntc\@ne
  \fi
  \ifnum\prg@tempcntc=\z@
    \g@addto@macro\pagestart{#1}%
  \else\ifnum\prg@tempcntc=\@ne
    \ifx\reserved@a\prg@tie\else
      \g@addto@macro\pageend{#1}%
    \fi
  \else\ifnum\prg@tempcntc>\@ne
    \prg@err{Multiple ties in page range.\MessageBreak
      Please use only one tie}%
  \fi\fi\fi
}
\prg@tempcnta=\z@\prg@tempcntb=\z@
\def\pagerange#1{%
  \let\pagestart\@empty
  \let\pageend\@empty
  \prg@tempcntc=\z@
  \prg@xifblank{#1}{%
    \ifprg@acceptempty
      \prg@warn{Blank page range supplied:\MessageBreak
        I've therefore set the range as 1-1 and continued}
      \let\pagestart\prg@defaultfirstpage
      \let\pageend\prg@defaultlastpage
    \else
      \prg@err{Blank page range.\MessageBreak
        If you really mean blank pagerange,\MessageBreak
        then also use 'acceptempty' boolean option
      }%
    \fi
  }{%
    \begingroup\edef\prg@tempa{\endgroup%
      \prg@forall#1\noexpand\do{\prg@split@pagerange}}%
    \prg@tempa
  }%
  \ifx\@empty\pagestart
    \ifnum\prg@tempcntc=\@ne
      \let\pagestart\prg@defaultfirstpage
    \fi
  \fi
  \prg@tempcnta=\number\pagestart\relax
  \ifx\@empty\pageend
    \prg@tempcntb=\@M
    \ifnum\prg@tempcntc=\@ne
      \let\pageend\prg@defaultlastpage
    \fi
  \else
    \prg@tempcntb=\number\pageend\relax
  \fi
  \ifnum\prg@tempcnta>\prg@tempcntb
    \prg@err{Inconsistent page range: Page \pageend\space > \pagestart}%
  \fi
}
\def\prg@carr#1#2#3\@nil{%
  \expandafter\ifx\expandafter\relax\detokenize{#3}\relax
    \prg@err{Problem with page reference}
  \fi
  #2%
}
\def\getpagenumber#1{%
  \prg@ifcsundef{r@#1}{\prg@defaultfirstpage}{%
    \expandafter\expandafter\expandafter\prg@carr
    \csname r@#1\endcsname\prg@defaultfirstpage\@nil
  }%
}

\endinput

%%% End of file pagerange.sty %%%