%%
%% This is file `fancylabel.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% fancylabel.dtx  (with options: `package')
%% 
%%  This is a generated file.
%% 
%%  Copyright (C) 2014 by Dominik Siegel
%% 
%%  This file may be distributed and/or modified under the
%%  conditions of the LaTeX Project Public License, either
%%  version 1.2 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.2 or later is part of all distributions of
%%  LaTeX version 1999/12/01 or later.
%% 
%%
%% To produce the documentation, do the following
%%
%%    latex fancylabel.dtx
%%    latex fancylabel.dtx
%%    makeindex -s gind.ist -o fancylabel.ind fancylabel.idx
%%    latex fancylabel.dtx
\NeedsTeXFormat{LaTeX2e}[2005/12/01]
\ProvidesPackage{fancylabel}[2014/07/15 v1.0 Referencing in a fancy way]
\RequirePackage{xkeyval}
\RequirePackage{ifthen}
\RequirePackage{suffix}
\newcounter{fancylabel}
\setcounter{fancylabel}{0}
\newcounter{fancysublabel}
\setcounter{fancysublabel}{0}
\presetkeys{fancylabel}{numbering=continuous,%
                      level=sub,%
                      IgnoreFamilies=false,%
                      sort=false,%
                      compress=false,%
                      CompressLink={-},%
                      CompressTwo=false,%
                      ListLastSublabelOnly=true,
                      ListSeparator={,\hskip-0.16667em{ }},%
                      ListLastLabelSeparator=\fancyref@ListSeparator,%
                      ListLastRefSeparator=\fancyref@ListSeparator,%
                      DisableFamilyCheck=false%
                      }{}
\newcommand{\fancyref@addtolist}[2]{%
    % #1=value to be added to the list #2
    % #2=list
    \ifthenelse{\equal{#1}{}}{}{%
        \ifthenelse{\equal{#2}{}}{}{%
            \g@addto@macro{#2}{,}%
        }%
        \g@addto@macro{#2}{#1}%
    }%
}
\newcommand{\fancyref@addtoprintlist}[2]{%
    % #1=value to be added to the list #2
    % #2=list
    \ifthenelse{\equal{#1}{}}{}{%
        \ifthenelse{\equal{#2}{}}{}{%
            \g@addto@macro{#2}{\fancyref@ListSeparator}%
        }%
        \g@addto@macro{#2}{#1}%
    }%
}
\define@key{fancylabel}{families}{%
    \fancyref@foreach{\fancyref@DefineFamily}{}{#1}%
}
\newcommand{\fancyref@ListOfFamilies}{}
\newcommand{\fancyref@DefineFamily}[2]{%
    % #1=empty
    % #2=family
    \@namedef{fancylabel@familystorage@#2}{0}%
    \@namedef{fancylabelformat#2}{#2-\arabic{fancylabel}}%
    \@namedef{fancyonlysublabelformat#2}{\alph{fancysublabel}}%
    \@namedef{fancysublabelformat#2}{#2-\arabic{fancylabel}\alph{fancysublabel}}%
    \@namedef{fancylabel@list@#2}{}%
    \fancyref@addtolist{#2}{\fancyref@ListOfFamilies}%
}
\@onlypreamble\fancyref@DefineFamily
\fancyref@DefineFamily{}{default}
\renewcommand{\fancylabelformatdefault}{\arabic{fancylabel}}
\renewcommand{\fancysublabelformatdefault}{\arabic{fancylabel}\alph{fancysublabel}}
\define@choicekey*+{fancylabel}{numbering}[\val\nr]{continuous,ondemand,referenced}{%
    % ##1=family
    % ##2=label
    % ##3=sublabel
    \ifcase\nr\relax%
        % numbering=continuous
        % Label will be set un the first time, \fancylabel{} is called
        % Order of labels depends on the appearance of fancylabel{}
        \newcommand{\fancyref@define@LabelUndef}[3]{%
            \fancyref@newlabel{##1}{##2}{##3}%
        }%
        \newcommand{\fancyref@show@LabelUndef}[3]{%
            \fancyref@showlabel{##1}{##2}{##3}%
        }%
        \newcommand{\fancyref@define@LabelDef}[3]{}%
        \newcommand{\fancyref@show@LabelDef}[3]{%
            \fancyref@showlabel{##1}{##2}{##3}%
        }%
        \newcommand{\fancyref@define@RefUndef}[3]{}%
        \newcommand{\fancyref@show@RefUndef}[3]{%
            \@ifundefined{fancyref@PrevRun@##1@##2@##3}{%
                \fancyref@labelerror{##2}{##3}%
                ???%
            }{%
                \fancyref@showlabel{##1}{##2}{##3}%
            }%
        }%
        \newcommand{\fancyref@define@RefDef}[3]{}%
        \newcommand{\fancyref@show@RefDef}[3]{%
            \fancyref@showlabel{##1}{##2}{##3}%
        }%
    \or%
        % numbering=ondemand
        % Only set label if it was asked for during the last run
        % Order of labels depends on the appearance of fancylabel{}
        \newcommand{\fancyref@define@LabelUndef}[3]{%
            \@ifundefined{fancyref@AskedPrevRun@##1@##2@##3}{}{%
                \fancyref@newlabel{##1}{##2}{##3}%
            }%
        }%
        \newcommand{\fancyref@show@LabelUndef}[3]{%
            \@ifundefined{fancyref@AskedPrevRun@##1@##2@##3}{}{%
                \fancyref@showlabel{##1}{##2}{##3}%
            }%
        }%
        \newcommand{\fancyref@define@LabelDef}[3]{}%
        \newcommand{\fancyref@show@LabelDef}[3]{%
            \fancyref@showlabel{##1}{##2}{##3}%
        }%
        \newcommand{\fancyref@define@RefUndef}[3]{}%
        \newcommand{\fancyref@show@RefUndef}[3]{%
            \@ifundefined{fancyref@PrevRun@##1@##2@##3}{%
                \fancyref@labelerror{##2}{##3}%
                ???%
            }{%
                \fancyref@showlabel{##1}{##2}{##3}%
            }%
        }%
        \newcommand{\fancyref@define@RefDef}[3]{}%
        \newcommand{\fancyref@show@RefDef}[3]{%
            \fancyref@showlabel{##1}{##2}{##3}%
        }%
    \or%
        % numbering=referenced
        % Only set label if it was asked for during the last run
        % Order of labels depends on the appearance of fancyref{}
        \newcommand{\fancyref@define@LabelUndef}[3]{}%
        \newcommand{\fancyref@show@LabelUndef}[3]{%
            \@ifundefined{fancyref@AskedPrevRun@##1@##2@##3}{}{%
                \fancyref@showlabel{##1}{##2}{##3}%
            }%
        }%
        \newcommand{\fancyref@define@LabelDef}[3]{}%
        \newcommand{\fancyref@show@LabelDef}[3]{%
            \fancyref@showlabel{##1}{##2}{##3}%
        }%
        \newcommand{\fancyref@define@RefUndef}[3]{%
            \fancyref@newlabel{##1}{##2}{##3}%
        }%
        \newcommand{\fancyref@show@RefUndef}[3]{%
            \@ifundefined{fancyref@PrevRun@##1@##2@##3}{%
                \fancyref@labelerror{##2}{##3}%
                ???%
            }{%
                \fancyref@showlabel{##1}{##2}{##3}%
            }%
        }%
        \newcommand{\fancyref@define@RefDef}[3]{%
            \fancyref@newlabel{##1}{##2}{##3}%
        }%
        \newcommand{\fancyref@show@RefDef}[3]{%
            \fancyref@showlabel{##1}{##2}{##3}% Show label
        }%
    \fi%
}{%
    \PackageError{fancylabel}{This value is not allowed. Allowed values are:%
                         'continuous'(default), 'ondemand', 'referenced'.}{}%
}
\define@choicekey*+{fancylabel}{level}[\val\nr]{sub,same}{%
    % ##1=family
    % ##2=label
    % ##3=sublabel
    \ifcase\nr\relax%
        % level=sub
        \DeclareRobustCommand\fancysublabel[3][default]{{%
            \fancysubreftrue%
            \fancyref@subrefminusfalse%
            \@fancylabel{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysublabel-[3][default]{{%
            \fancysubreftrue%
            \fancyref@subrefminustrue%
            \@fancylabel{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysublabel*[3][default]{{%
            \renewcommand{\@@fancylabel@show}[2]{}%
            \fancysubreftrue%
            \fancyref@subrefminusfalse%
            \@fancylabel{##1}{##2}{##3}%
        }}%
        \DeclareRobustCommand\fancysubref[3][default]{{%
            \fancysubreftrue%
            \fancyref@subrefminusfalse%
            \@fancyref{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysubref-[3][default]{{%
            \fancysubreftrue%
            \fancyref@subrefminustrue%
            \@fancyref{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysubref*[3][default]{{%
            \renewcommand{\@@fancylabel@show}[2]{}%
            \fancysubreftrue%
            \fancyref@subrefminusfalse%
            \@fancyref{##1}{##2}{##3}%
        }}%
    \or%
        % level=same
        \DeclareRobustCommand\fancysublabel[3][default]{{%
            \fancysubreffalse%
            \fancyref@subrefminusfalse%
            \@fancylabel{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysublabel-[3][default]{{%
            \fancysubreffalse%
            \fancyref@subrefminustrue%
            \@fancylabel{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysublabel*[3][default]{{%
            \renewcommand{\@@fancylabel@show}[2]{}%
            \fancysubreffalse%
            \fancyref@subrefminusfalse%
            \@fancylabel{##1}{##2}{##3}%
        }}%
        \DeclareRobustCommand\fancysubref[3][default]{{%
            \fancysubreffalse%
            \fancyref@subrefminusfalse%
            \@fancyref{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysubref-[3][default]{{%
            \fancysubreffalse%
            \fancyref@subrefminustrue%
            \@fancyref{##1}{##2}{##3}%
        }}%
        \WithSuffix\DeclareRobustCommand\fancysubref*[3][default]{{%
            \renewcommand{\@@fancylabel@show}[2]{}%
            \fancysubreffalse%
            \fancyref@subrefminusfalse%
            \@fancyref{##1}{##2}{##3}%
        }}%
    \fi%
}{%
    \PackageError{fancylabel}{This value is not allowed. Allowed values are:%
                         'sub'(default), 'same'.}{}%
}
\newif\iffancyref@subrefminus\fancyref@subrefminusfalse
\define@boolkey{fancylabel}{IgnoreFamilies}{}
\define@boolkey{fancylabel}{sort}{}
\define@boolkey{fancylabel}{compress}{}
\define@key{fancylabel}{CompressLink}{\def\fancyref@CompressLink{#1}}
\define@boolkey{fancylabel}{CompressTwo}{}
\define@boolkey{fancylabel}{ListLastSublabelOnly}{}
\define@key{fancylabel}{ListSeparator}{\edef\fancyref@ListSeparator{#1}}
\define@key{fancylabel}{ListLastLabelSeparator}{\edef\fancyref@ListLastLabelSeparator{#1}}
\define@key{fancylabel}{ListLastRefSeparator}{\edef\fancyref@ListLastRefSeparator{#1}}
\define@boolkey{fancylabel}{DisableFamilyCheck}{}
\newif\iffancyref@rerunerror\fancyref@rerunerrorfalse % ReRun error
\newif\iffancysubref\fancysubreffalse % Subref or Ref
\AtEndDocument{%
    \iffancyref@rerunerror%
        \PackageWarning{fancylabel}{Fancylabels may have changed. %
                                  Please run latex again.}%
    \fi%
}
\newcommand{\fancyref@labelerror}[2]{%
    % #1=label
    % #2=sublabel
    \ifthenelse{\equal{#2}{}}{%
        \PackageWarning{fancylabel}{Undefined fancylabel "#1".}%
    }{%
        \PackageWarning{fancylabel}{Undefined fancysublabel "#1@#2".}%
    }%
    \global\fancyref@rerunerrortrue%
}
\DeclareRobustCommand\fancylabel[2][default]{{%
    % #1=family
    % #2=label or list of labels
    \fancysubreffalse%
    \@fancylabel{#1}{#2}{}%
}}
\WithSuffix\DeclareRobustCommand\fancylabel*[2][default]{{%
    % #1=family
    % #2=label or list of labels
    \renewcommand{\@@fancylabel@show}[2]{}%
    \fancysubreffalse%
    \@fancylabel{#1}{#2}{}%
}}
\DeclareRobustCommand\fancyref[2][default]{{%
    % #1=family
    % #2=label or list of labels
    \@fancyref{#1}{#2}{}%
}}
\WithSuffix\DeclareRobustCommand\fancyref*[2][default]{{%
    % #1=family
    % #2=label or list of labels
    \renewcommand{\@@fancylabel@show}[2]{}%
    \@fancyref{#1}{#2}{}%
}}
\newcommand{\fancylabelNewLabelEvent}{}
\newcommand{\fancylabelShowLabelEvent}{}
\newcommand{\fancylabelShowRefEvent}{}
\newcommand{\fancylabelResetCounter}[1]{%
    % #1=family
    \@ifundefined{fancylabel@familystorage@#1}{%
        \PackageError{fancylabel}{No fancylabel family '#1' defined.}{}%
    }{%
        \@namedef{fancylabel@familystorage@#1}{0}%
    }%
}
\newif\iffancyref@showlabel@FirstSublabelInList%
\fancyref@showlabel@FirstSublabelInListtrue%
\newcommand{\@fancylabel}[3]{%
    % #1=family
    % #2=label or list of labels
    % #3=sublabel or list of sublabels
    \@fancylabelANDref{#1}{#2}{#3}%
    % Define labels
    \expandafter\fancyref@@sort\expandafter{\@fancylabel@list}{\@@fancylabel@define}%
    % Print labels
    \setcounter{fancyref@sort@x}{0}%
    \expandafter\fancyref@@sort\expandafter{\fancyref@compress@list}{\@@fancylabel@show}%
    \fancyref@print@list%
}
\newcommand{\@fancylabelANDref}[3]{%
    % #1=family
    % #2=label or list of labels
    % #3=sublabel or list of sublabels
    \def\fancyref@sort@list{}%
    \def\fancyref@sort@labellist@unsorted{}%
    \def\fancyref@compress@list{}%
    \def\fancyref@print@list{}%
    \ifthenelse{\equal{#3}{}}{%
        % true => List of labels
        \newcommand{\@fancylabel@list}{#2}%
        \newcommand{\@@fancylabel@define}[2]{%
            % ##1=
            % ##2=label (from list)
            \@@@fancylabel@define{#1}{##2}{#3}%
        }%
        \newcommand{\@@fancyref@define}[2]{%
            % ##1=
            % ##2=label (from list)
            \@@@fancyref@define{#1}{##2}{#3}%
        }%
        \def\fancyref@@@@@sort{%
            fancyref@PrevRun@#1@\fancyref@@sort@value @%
        }%
        \def\fancyref@@@compress{%
            fancyref@PrevRun@#1@\fancyref@@compress@value @%
        }%
        \newcommand{\fancyref@@@compress@entry}{%
            {#1}{\fancyref@@compress@value}{}%
        }%
    }{%
        % false => List of sublabels
        \newcommand{\@fancylabel@list}{#3}%
        \newcommand{\@@fancylabel@define}[2]{%
            % ##1=
            % ##2=sublabel (from list)
            \@@@fancylabel@define{#1}{#2}{##2}%
        }%
        \newcommand{\@@fancyref@define}[2]{%
            % ##1=
            % ##2=sublabel (from list)
            \@@@fancyref@define{#1}{#2}{##2}%
        }%
        \def\fancyref@@@@@sort{%
            fancyref@PrevRun@#1@#2@\fancyref@@sort@value%
        }%
        \def\fancyref@@@compress{%
            fancyref@PrevRun@#1@#2@\fancyref@@compress@value%
        }%
        \newcommand{\fancyref@@@compress@entry}{%
            {#1}{#2}{\fancyref@@compress@value}%
        }%
    }%
    % Sort
    \fancyref@sort{\@fancylabel@list}%
    % Compress
    \fancyref@compress{\fancyref@sort@list}%
    % Add the so far undefined labels to this list
    \ifthenelse{\equal{\fancyref@sort@labellist@unsorted}{}}{}{%
        \KV@fancylabel@compressfalse%
        \fancyref@compress{\fancyref@sort@labellist@unsorted}%
        \expandafter\fancyref@addtolist\expandafter{\fancyref@sort@labellist@unsorted}%
                                                   {\fancyref@sort@list}%
    }%
}
\newcommand{\@@@fancylabel@define}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \@ifundefined{fancylabel@familystorage@#1}{%
        \PackageError{fancylabel}{No fancylabel family '#1' defined. %
                                Use package option families for definition.}{}%
    }{%
        \@ifundefined{fancyref@ThisRun@#1@#2@#3}{%
            \fancyref@define@LabelUndef{#1}{#2}{#3}%
        }{%
            \fancyref@define@LabelDef{#1}{#2}{#3}%
        }%
    }%
}
\newcommand{\@@fancylabel@show}[2]{%
    % #1=
    % #2={<family>}{<label>}{<sublabel>},{<family>}{<label>}{<sublabel>}
    \@@fancylabelANDref@show%
    % Get first and second item
    \expandafter\fancyref@@sort\expandafter{#2}{\@@fancylabel@@show}%
    % Produce the output
    \edef\fancyref@ListLastSeparator{\fancyref@ListLastLabelSeparator}%
    \@@fancylabelANDref@show@%
    \expandafter\@@@fancylabel@show\@@fancylabel@@show@begin%
    \@@fancylabelANDref@show@@%
    % Add the \fancylabelShowLabelEvent if the label is not empty
    \ifthenelse{\equal{\fancyref@print@list}{}}{}{%
       \g@addto@macro{\fancyref@print@list}{\fancylabelShowLabelEvent}%
    }%
}%
\newcommand{\@@fancyref@show}[2]{%
    % #1=
    % #2={<family>}{<label>}{<sublabel>},{<family>}{<label>}{<sublabel>}
    \@@fancylabelANDref@show%
    % Get first and second item
    \expandafter\fancyref@@sort\expandafter{#2}{\@@fancylabel@@show}%
    % Produce the output
    \edef\fancyref@ListLastSeparator{\fancyref@ListLastRefSeparator}%
    \@@fancylabelANDref@show@%
    \expandafter\@@@fancyref@show\@@fancylabel@@show@begin%
    \@@fancylabelANDref@show@@%
    % Add the \fancylabelShowRefEvent
    % Add the \fancylabelShowLabelEvent if the label is not empty
    \ifthenelse{\equal{\fancyref@print@list}{}}{}{%
       \g@addto@macro{\fancyref@print@list}{\fancylabelShowRefEvent}%
    }%
}%
\newcommand{\@@fancylabelANDref@show}{%
    \edef\@@fancylabel@@show@begin{}%
    \edef\@@fancylabel@@show@begin@entry{}%
    \edef\@@fancylabel@@show@end{}%
    \edef\@@fancylabel@@show@end@entry{}%
    \edef\@@fancylabel@@show@entry{}%
}%
\newcommand{\@@fancylabelANDref@show@}{%
    \stepcounter{fancyref@sort@x}%
    \ifthenelse{\thefancyref@sort@x = \@@fancylabel@@show@LengthOfPrintList}{}{%
        % This is the last item of the list
        \ifthenelse{\equal{\@@fancylabel@@show@end}{}}{%
            \edef\fancyref@ListSeparator{\fancyref@ListLastSeparator}%
        }{}%
    }%
}%
\newcommand{\@@fancylabelANDref@show@@}{%
    \ifthenelse{\equal{@@fancylabel@@show@entry}{}}{}{%
        % \fancyref@showlabel has been executed
        % this means a label will be printed
        \edef\@@fancylabel@@show@begin@entry{\@@fancylabel@@show@entry}%
        \ifthenelse{\equal{\@@fancylabel@@show@end}{}}{}{%
            \expandafter\@@@fancylabel@show\@@fancylabel@@show@end%
            \edef\@@fancylabel@@show@end@entry{\@@fancylabel@@show@entry}%
        }%
        % Add to list
        \ifthenelse{\equal{\@@fancylabel@@show@end@entry}{}}{%
            % No compressing
            \edef\fancyref@print@list@entry{\@@fancylabel@@show@begin@entry}%
        }{%
            % Compressing
            \edef\fancyref@print@list@entry{\@@fancylabel@@show@begin@entry%
                                            \fancyref@CompressLink%
                                            \@@fancylabel@@show@end@entry}%
        }%
        \expandafter\fancyref@addtoprintlist\expandafter{%
            \fancyref@print@list@entry%
        }{%
            \fancyref@print@list%
        }%
    }%
}%
\newcommand{\@@fancylabel@@show}[2]{%
    % #1=
    % #2={<family>}{<label>}{<sublabel>}
    \ifthenelse{\equal{\@@fancylabel@@show@begin}{}}{%
        \edef\@@fancylabel@@show@begin{#2}%
    }{%
        \edef\@@fancylabel@@show@end{#2}%
    }%
}
\newcommand{\@@@fancylabel@show}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \@ifundefined{fancylabel@familystorage@#1}{%
        \PackageError{fancylabel}{No fancylabel family '#1' defined. %
                                Use package option families for definition.}{}%
    }{%
        \@ifundefined{fancyref@ThisRun@#1@#2@#3}{%
            \fancyref@show@LabelUndef{#1}{#2}{#3}%
        }{%
            \fancyref@show@LabelDef{#1}{#2}{#3}%
        }%
    }%
}
\newcommand{\@@@fancyref@show}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \@ifundefined{fancylabel@familystorage@#1}{%
        \PackageError{fancylabel}{No fancylabel family '#1' defined. %
                                Use package option families for definition.}{}%
    }{%
        \@ifundefined{fancyref@ThisRun@#1@#2@#3}{%
            \fancyref@show@RefUndef{#1}{#2}{#3}%
        }{%
            \fancyref@show@RefDef{#1}{#2}{#3}%
        }%
    }%
}
\newcommand{\@fancyref}[3]{%
    % #1=family
    % #2=label or list of labels
    % #3=sublabel or list of sublabels
    \@fancylabelANDref{#1}{#2}{#3}%
    % Define labels
    \expandafter\fancyref@@sort\expandafter{\@fancylabel@list}{\@@fancyref@define}%
    % Print labels
    \setcounter{fancyref@sort@x}{0}%
    \expandafter\fancyref@@sort\expandafter{\fancyref@compress@list}{\@@fancyref@show}%
    \fancyref@print@list%
}
\newcommand{\@@fancyref}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \@ifundefined{fancyref@AskedThisRun@#1@#2@#3}{%
        \global\@namedef{fancyref@AskedThisRun@#1@#2@#3}{#2}%
        \immediate\write\@auxout{%
            \string\fancyref@aux{fancyref@AskedPrevRun@#1@#2@#3}{#2}%
        }%
    }{}%
    \@@@fancyref{#1}{#2}{#3}%
}
\newcommand{\@@@fancyref@define}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \@ifundefined{fancyref@AskedThisRun@#1@#2@#3}{%
        \global\@namedef{fancyref@AskedThisRun@#1@#2@#3}{#2}%
        \immediate\write\@auxout{%
            \string\fancyref@aux{fancyref@AskedPrevRun@#1@#2@#3}{#2}%
        }%
    }{}%
    \@ifundefined{fancyref@PrevRun@#1@#2@#3}{%
        \fancyref@define@RefUndef{#1}{#2}{#3}%
    }{%
        \fancyref@define@RefDef{#1}{#2}{#3}%
    }%
}
\newcommand{\fancyref@newlabel}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \@ifundefined{fancyref@ThisRun@#1@#2@#3}{%
        \ifKV@fancylabel@DisableFamilyCheck%
        \else%
            \expandafter\fancyref@CheckOtherFamilies%
            \expandafter{\fancyref@ListOfFamilies}{#1}{#2}%
            \iffancyref@@@@CheckOtherFamilies@error%
                \PackageWarning{fancylabel}{Label '#2' has been used in %
                                            multiple families.}%
            \fi%
        \fi%
        % Subref or Ref
        \iffancysubref% Subref
            \fancyref@@newsublabel{#1}{#2}{#3}%
        \else% Ref
            \fancyref@@newlabel{#1}{#2}{#3}%
        \fi%
    }{}%
    % Show Package Warning if labels have changed since the last run
    \@ifundefined{fancyref@PrevRun@#1@#2@#3}{%
        \global\fancyref@rerunerrortrue%
    }{%
        \ifthenelse{\equal{\@nameuse{fancyref@ThisRun@#1@#2@#3}}%
                          {\@nameuse{fancyref@PrevRun@#1@#2@#3}}}{}{%
            \global\fancyref@rerunerrortrue%
        }%
    }%
}
\newcommand{\fancyref@@newlabel}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \ifKV@fancylabel@IgnoreFamilies%
        \setcounter{fancylabel}{\@nameuse{fancylabel@familystorage@default}}%
        \stepcounter{fancylabel}%
        \expandafter\xdef\csname fancylabel@familystorage@default\endcsname{\thefancylabel}%
    \else%
        \setcounter{fancylabel}{\@nameuse{fancylabel@familystorage@#1}}%
        \stepcounter{fancylabel}%
        \expandafter\xdef\csname fancylabel@familystorage@#1\endcsname{\thefancylabel}%
    \fi%
    \immediate\write\@auxout{%
        \string\fancyref@aux{fancyref@PrevRun@#1@#2@#3}{\thefancylabel}%
    }%
    \immediate\write\@auxout{%
        \string\fancyref@addtolist{\string{#2\string}}{\string\fancylabel@list@#1}%
    }%
    \expandafter\xdef\csname fancyref@ThisRun@#1@#2@#3\endcsname{\thefancylabel}%
    % Uses specified Event following the newlabel command
    \fancylabelNewLabelEvent%
}
\newcommand{\fancyref@@newsublabel}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \@ifundefined{fancyref@ThisRun@#1@#2@}{%
        \fancyref@@newlabel{#1}{#2}{}%
    }{}%
    \@ifundefined{fancyref@highestsub@#1@#2}{%
        \setcounter{fancysublabel}{0}%
    }{%
        \setcounter{fancysublabel}{\@nameuse{fancyref@highestsub@#1@#2}}%
    }%
    \stepcounter{fancysublabel}%
    \immediate\write\@auxout{%
        \string\fancyref@aux{fancyref@PrevRun@#1@#2@#3}{\thefancysublabel}%
    }%
    \expandafter\xdef\csname fancyref@ThisRun@#1@#2@#3\endcsname{\thefancysublabel}%
    \expandafter\xdef\csname fancyref@highestsub@#1@#2\endcsname{\thefancysublabel}%
}
\newcommand{\fancyref@showlabel}[3]{%
    % #1=family
    % #2=label
    % #3=sublabel
    \iffancysubref% \fancysubref and level=sub
        \@ifundefined{fancyref@PrevRun@#1@#2@}{%
            \fancyref@rerunerrortrue%
        }{%
            \setcounter{fancylabel}{\@nameuse{fancyref@PrevRun@#1@#2@}}%
        }%
        \@ifundefined{fancyref@PrevRun@#1@#2@#3}{%
            \fancyref@rerunerrortrue%
        }{%
            \setcounter{fancysublabel}{\@nameuse{fancyref@PrevRun@#1@#2@#3}}%
        }%
        \iffancyref@subrefminus% minus => only subref without ref
            \ifKV@fancylabel@IgnoreFamilies%
                \edef\fancylabelthisformat{\@nameuse{fancyonlysublabelformatdefault}}%
            \else%
                \edef\fancylabelthisformat{\@nameuse{fancyonlysublabelformat#1}}%
            \fi%
        \else% Label and Sublabel
            \iffancyref@showlabel@FirstSublabelInList%
                % This is the very first label
                \ifKV@fancylabel@IgnoreFamilies%
                    \edef\fancylabelthisformat{\@nameuse{fancysublabelformatdefault}}%
                \else%
                    \edef\fancylabelthisformat{\@nameuse{fancysublabelformat#1}}%
                \fi%
                \fancyref@showlabel@FirstSublabelInListfalse%
            \else%
                % Any label after the first one
                \ifKV@fancylabel@ListLastSublabelOnly%
                    % Output: 1a-c,d
                    \ifKV@fancylabel@IgnoreFamilies%
                        \edef\fancylabelthisformat{\@nameuse{fancyonlysublabelformatdefault}}%
                    \else%
                        \edef\fancylabelthisformat{\@nameuse{fancyonlysublabelformat#1}}%
                    \fi%
                \else%
                    % Output: 1a-1c,1d
                    \ifKV@fancylabel@IgnoreFamilies%
                        \edef\fancylabelthisformat{\@nameuse{fancysublabelformatdefault}}%
                    \else%
                        \edef\fancylabelthisformat{\@nameuse{fancysublabelformat#1}}%
                    \fi%
                \fi%
            \fi%
        \fi%
    \else% \fancyref or \fancysubref and level=same
        \@ifundefined{fancyref@PrevRun@#1@#2@#3}{%
            \fancyref@rerunerrorfalse%
        }{%
            \setcounter{fancylabel}{\@nameuse{fancyref@PrevRun@#1@#2@#3}}%
        }%
        \ifKV@fancylabel@IgnoreFamilies%
            \edef\fancylabelthisformat{\@nameuse{fancylabelformatdefault}}%
        \else%
            \edef\fancylabelthisformat{\@nameuse{fancylabelformat#1}}%
        \fi%
    \fi%
    \edef\@@fancylabel@@show@entry{\fancylabelthisformat}%
}
\newcommand{\fancyref@CheckOtherFamilies}[3]{%
    % #1=list of families
    % #2=current family
    % #3=label
    \fancyref@foreach{\fancyref@@CheckOtherFamilies{#3}}{#2}{#1}%
}
\newcommand{\fancyref@@CheckOtherFamilies}[3]{%
    % #1=label
    % #2=current family
    % #3=family (from list)
    \xdef\fancyref@helper{\csname fancylabel@list@#3\endcsname}%
    \ifthenelse{\equal{#2}{#3}}{}{%
        \expandafter\fancyref@@@CheckOtherFamilies\expandafter{\fancyref@helper}{#1}%
    }%
}
\newcommand{\fancyref@@@CheckOtherFamilies}[2]{%
    % #1=list of labels
    % #2=label
    \fancyref@foreach{\fancyref@@@@CheckOtherFamilies}{#2}{#1}%
}
\newif\iffancyref@@@@CheckOtherFamilies@error
\fancyref@@@@CheckOtherFamilies@errorfalse
\newcommand{\fancyref@@@@CheckOtherFamilies}[2]{%
    % #1=label
    % #2=label (from list)
    \ifthenelse{\equal{#1}{#2}}{%
        \fancyref@@@@CheckOtherFamilies@errortrue%
    }{}%
}
\newcommand{\fancyref@aux}[2]{% Helps to read the aux file
    \global\@namedef{#1}{#2}%
}
%%% Thanks to
%%% http://stackoverflow.com/questions/2402354/split-comma-separated-parameters-in-latex
\def\fancyref@foreach#1#2#3{%
    \fancyref@@test@foreach{#1}{#2}#3,\@end@token%
}

\def\fancyref@@swallow#1{}

\def\fancyref@@test@foreach#1#2{%
    \@ifnextchar\@end@token%
        {\fancyref@@swallow}%
        {\fancyref@@foreach{#1}{#2}}%
}

\def\fancyref@@foreach#1#2#3,#4\@end@token{%
    #1{#2}{#3}%
    \fancyref@@test@foreach{#1}{#2}#4\@end@token%
}
\newcounter{fancyref@sort@x}
\setcounter{fancyref@sort@x}{0}
\newcounter{fancyref@sort@y}
\setcounter{fancyref@sort@y}{0}
\newcommand{\fancyref@sort}[1]{%
    % #1=comma-separated list of labels or sublabels
    \ifKV@fancylabel@sort%
        % Generate a list of all values
        \def\fancyref@sort@valuelist@sorted{}%
        \setcounter{fancyref@sort@x}{0}%
        \expandafter\fancyref@@sort\expandafter{#1}{\fancyref@@@sort}%
        % Generate a sorted list of values
        \edef\fancyref@sort@arraylength{\thefancyref@sort@x}%
        \setcounter{fancyref@sort@x}{0}%
        \whiledo{\thefancyref@sort@x < \fancyref@sort@arraylength}{%
            \setcounter{fancyref@sort@y}{0}%
            % Set \fancyref@sort@smallest@temp the first list entry that is not 0
            \whiledo{\thefancyref@sort@y < \fancyref@sort@arraylength}{%
                \ifthenelse{\@nameuse{fancyref@sort@valuelist@\thefancyref@sort@y} = 0}{%
                    \stepcounter{fancyref@sort@y}%
                }{%
                    \edef\fancyref@sort@smallest@temp{\thefancyref@sort@y}%
                    \setcounter{fancyref@sort@y}{\fancyref@sort@arraylength}%
                }%
            }%
            \setcounter{fancyref@sort@y}{0}%
            % Find the smallest value in the list
            \whiledo{\thefancyref@sort@y < \fancyref@sort@arraylength}{%
                \ifthenelse{\@nameuse{fancyref@sort@valuelist@\thefancyref@sort@y} = 0}{}{%
                    \ifthenelse{\@nameuse{fancyref@sort@valuelist@\thefancyref@sort@y} < %
                                \@nameuse{fancyref@sort@valuelist@\fancyref@sort@smallest@temp}}{%
                        \edef\fancyref@sort@smallest@temp{\thefancyref@sort@y}%
                    }{}%
                }%
                \stepcounter{fancyref@sort@y}%
            }%
            % Add this smallest value to the list
            \edef\fancyref@sort@smallest{%
                \@nameuse{fancyref@sort@valuelist@\fancyref@sort@smallest@temp}%
            }%
            \expandafter\fancyref@addtolist\expandafter{\fancyref@sort@smallest}%
                                                       {\fancyref@sort@valuelist@sorted}%
            \expandafter\edef\csname fancyref@sort@valuelist@\fancyref@sort@smallest@temp\endcsname{0}%
            \stepcounter{fancyref@sort@x}%
        }%
        % Generate a sorted list of labels
        \ifthenelse{\equal{\fancyref@sort@valuelist@sorted}{}}{}{%
            \expandafter\fancyref@@sort\expandafter{\fancyref@sort@valuelist@sorted}{\fancyref@@@@sort}%
        }%
    \else%
        \expandafter\fancyref@addtolist\expandafter{#1}{\fancyref@sort@list}%
    \fi%
}
\newcommand{\fancyref@@sort}[2]{%
    % #1=comma-separated list
    % #2=function to be executed on this list
    \fancyref@foreach{#2}{}{#1}%
}
\newcommand{\fancyref@@@sort}[2]{%
    % #1=
    % #2=label (from list)
    \def\fancyref@@sort@value{#2}%
    \@ifundefined{\fancyref@@@@@sort}{%
        \fancyref@addtolist{#2}{\fancyref@sort@labellist@unsorted}%
    }{%
        \expandafter\edef\csname fancyref@sort@labellist@\@nameuse{\fancyref@@@@@sort}\endcsname{#2}%
        \expandafter\edef\csname fancyref@sort@valuelist@\thefancyref@sort@x\endcsname{%
            \@nameuse{\fancyref@@@@@sort}%
        }%
        \stepcounter{fancyref@sort@x}%
    }%
}%
\newcommand{\fancyref@@@@sort}[2]{%
    % #1=
    % #2=value (from sorted list of values)
    \fancyref@addtolist{\@nameuse{fancyref@sort@labellist@#2}}{\fancyref@sort@list}%
}
\newcommand{\fancyref@compress}[1]{%
    % #1=list of labels or sublabels
    \def\fancyref@compress@begin{}%
    \def\fancyref@compress@begin@entry{}%
    \def\fancyref@compress@end{}%
    \def\fancyref@compress@end@entry{}%
    \setcounter{fancyref@sort@y}{0}%
    \ifKV@fancylabel@compress%
        \ifthenelse{\equal{#1}{}}{}{%
            % Compress List
            \expandafter\fancyref@@sort\expandafter{#1}{\fancyref@@compress}%
            % Add last entry
            \fancyref@@@@compress@stop%
        }%
    \else%
        \expandafter\fancyref@@sort\expandafter{#1}{\fancyref@@nocompress}%
    \fi%
    \edef\@@fancylabel@@show@LengthOfPrintList{\thefancyref@sort@y}%
}
\newcommand{\fancyref@@compress}[2]{%
    % #1=
    % #2=value (from sorted list of values)
    \def\fancyref@@compress@value{#2}%
    \ifthenelse{\equal{\fancyref@compress@begin}{}}{%
        % Start compressing
        \fancyref@@@@compress@start%
    }{%
        \stepcounter{fancyref@sort@x}%
        \ifthenelse{\@nameuse{\fancyref@@@compress} = \thefancyref@sort@x}{%
            % Continue compressing
            \fancyref@@@@compress@continue%
        }{%
            % Stop compressing and begin a new compressing
            \fancyref@@@@compress@stop%
        }%
    }%
}
\newcommand{\fancyref@@@@compress@start}{%
    \edef\fancyref@compress@begin{\fancyref@@@compress}%
    \edef\fancyref@compress@begin@entry{\fancyref@@@compress@entry}%
    \setcounter{fancyref@sort@x}{\@nameuse{\fancyref@compress@begin}}%
}
\newcommand{\fancyref@@@@compress@continue}{%
    \edef\fancyref@compress@end{\fancyref@@@compress}%
    \edef\fancyref@compress@end@entry{\fancyref@@@compress@entry}%
}
\newcommand{\fancyref@@@@compress@stop}{%
    \ifKV@fancylabel@CompressTwo%
        % Compress even if difference is only 1
        \edef\fancyref@compress@listentry{{{\fancyref@compress@begin@entry},{\fancyref@compress@end@entry}}}%
        \expandafter\fancyref@addtolist\expandafter{\fancyref@compress@listentry}{\fancyref@compress@list}%
    \else%
        % Compress if difference > 1
        \setcounter{fancyref@sort@x}{\@nameuse{\fancyref@compress@begin}}%
        \stepcounter{fancyref@sort@x}%
        % Avoid empty \fancyref@compress@end
        \ifthenelse{\equal{\fancyref@compress@end}{}}{%
            \edef\fancyref@compress@listentry{{{\fancyref@compress@begin@entry},{\fancyref@compress@end@entry}}}%
            \expandafter\fancyref@addtolist\expandafter{\fancyref@compress@listentry}{\fancyref@compress@list}%
        }{%
            \ifthenelse{\@nameuse{\fancyref@compress@end} = \thefancyref@sort@x}{%
                % Difference = 1
                \edef\fancyref@compress@listentry{{{\fancyref@compress@begin@entry},{}}}%
                \expandafter\fancyref@addtolist\expandafter{\fancyref@compress@listentry}{\fancyref@compress@list}%
                \edef\fancyref@compress@listentry{{{\fancyref@compress@end@entry},{}}}%
                \expandafter\fancyref@addtolist\expandafter{\fancyref@compress@listentry}{\fancyref@compress@list}%
            }{%
                % Difference > 1
                \edef\fancyref@compress@listentry{%
                    {{\fancyref@compress@begin@entry},{\fancyref@compress@end@entry}}%
                }%
                \expandafter\fancyref@addtolist\expandafter{\fancyref@compress@listentry}{\fancyref@compress@list}%
            }%
        }%
    \fi%
    \edef\fancyref@compress@begin{\fancyref@@@compress}%
    \edef\fancyref@compress@begin@entry{\fancyref@@@compress@entry}%
    \edef\fancyref@compress@end{}%
    \edef\fancyref@compress@end@entry{}%
    \setcounter{fancyref@sort@x}{\@nameuse{\fancyref@compress@begin}}%
    \stepcounter{fancyref@sort@y}%
}
\newcommand{\fancyref@@nocompress}[2]{%
    % #1=
    % #2=value (from sorted list of values)
    \stepcounter{fancyref@sort@y}%
    \def\fancyref@@compress@value{#2}%
    \edef\fancyref@compress@listentry{{{\fancyref@@@compress@entry},{}}}%
    \expandafter\fancyref@addtolist\expandafter{\fancyref@compress@listentry}{\fancyref@compress@list}%
}
\ProcessOptionsX<fancylabel>
\endinput
%%
%% End of file `fancylabel.sty'.
