%=====================================================================================================================================
% Package akkcounterlabelpattern by Igor Akkerman (c) 2004
% Package providing easy counter label creation using a specified pattern.
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License (latest version).
%
% The latest version of this license can be found at
%     http://www.latex-project.org/lppl.txt
%
% This file may only be distributed together with the full 'AkkTeX' bundle.
%=====================================================================================================================================
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{akkcounterlabelpattern}[2006/10/25 1.0.2 by Igor Akkerman (c)2004-2006 Package providing easy counter label creation using a specified pattern.]
%=====================================================================================================================================

%=====================================================================================================================================
% This package is providing simple means for creating counter labels with a
% user-defined pattern.
%
% Usually, for displaying counters in a LaTeX document,
% you would need to use the commands "\arabic{mycounter}", "\Alph{mycounter}" etc.
%
% This package provides an easier way to define how a counter should be displayed. 
% A pattern like "{Element }1" or "{Part }A" can be specified to define the label's format,
% instead of "Element \arabic{mycounter}" or "Part \Alph{mycounter}".
%=====================================================================================================================================

\makeatletter

\newtoks \akk@counterlabel%

% -------------------------------------------------------------------
% \createcounterlabel {<countername>} {<labelname>} {<labelpattern>}
%
% Creates a new command \<labelname> expanding to the label specified
% by <labelpattern> using the counter specified by <countername>.
%
% Examples:
% \createcounterlabel {chapter}{chapternumber}{{Chapter }1}
% creates the new command \chapternumber that will expand to
%     "Chapter 1", "Chapter 2", "Chapter 3", ...
% where "1", "2", "3", ... are the current values of the counter
% "chapter".
% It is a shorter version of
% \renewcommand {\thechapter} {\arabic{chapter}}
% \newcommand {\chapternumber} {Chapter \thechapter}
% 
% \createcounterlabel {part}{partnumber}{{Part }A}
% creates the new command \partnumber that will expand to
%     "Part A", "Part B", "Part C", ...
% where A, B, C, ... are the uppercase letter representants of the
% current values of the counter "part".
% It is a shorter version of
% \renewcommand {\thepart} {\alph{part}}
% \newcommand {\partnumber} {Part \thepart}
% -------------------------------------------------------------------
\newcommand \createcounterlabel[3] {%
    \def\akk@countername{#1}%
    \akk@counterlabel{}%
    \let \akk@counterpatternNumber ?%
    \akk@parsePatternToken@ #3\relax%
    \ifx\akk@counterpatternNumber?%
        \@warning{%
            [counterlabelpattern] The counter will not be printed.^^J%
            [counterlabelpattern] The label is: '\the\akk@counterlabel'%
        }%
    \fi%
    \expandafter%
        \def \csname #2\endcsname {\the\akk@counterlabel}%
    \expandafter%
        \let \csname the\akk@countername\endcsname \akk@counterpatternNumber%
}

\def \akk@parsePatternToken@ {%
    % set \akk@patterntoken to <next token in <labelpattern> >,
    % expand \akk@processPatternToken@ \akk@patterntoken
    \futurelet\akk@patterntoken\akk@processPatternToken@%
}

\def \akk@processPatternToken@ {%
    \ifx \akk@patterntoken A%
        \def\akk@processcommand{\akk@processLabel\Alph}%
    \else \ifx \akk@patterntoken a%
        \def\akk@processcommand{\akk@processLabel\alph}%
    \else \ifx \akk@patterntoken i%
        \def\akk@processcommand{\akk@processLabel\roman}%
    \else \ifx \akk@patterntoken I%
        \def\akk@processcommand{\akk@processLabel\Roman}%
    \else \ifx \akk@patterntoken 1%
        \def\akk@processcommand{\akk@processLabel\arabic}%
    \else \ifx \akk@patterntoken \@sptoken% (= ' ')
        \let\akk@processcommand\akk@processSpace%
    \else \ifx \akk@patterntoken \bgroup% (= '{')
        \let\akk@processcommand\akk@processGroup%
    \else \ifx \akk@patterntoken\relax%
        \let\akk@processcommand\@gobble%
    \else%
        \let\akk@processcommand\akk@processOther%
        \counterlabelpatternTokenHook
    \fi\fi\fi\fi\fi\fi\fi\fi%
    \akk@processcommand%
}

% \akk@processLabel<\Alph|\alph|\Roman|\roman|\arabic><A|a|I|i|1>
% Actually, the second parameter is ignored.
\def \akk@processLabel#1#2{%
    \ifx\akk@counterpatternNumber?%
    \else%
        \errmessage{%
            [counterlabelpattern] The counter must be unique in the label.%
        }%
    \fi%
    \edef \akk@counterpatternNumber {\noexpand#1{\akk@countername}}%
    \akk@counterlabel \expandafter{\the\akk@counterlabel\csname the\akk@countername\endcsname}%
    \akk@parsePatternToken@%
}

\def \akk@processSpace {%
    \afterassignment\akk@processSp@ce%
    \let\akk@dummy= % (don't remove the blank before %)
}

\def \akk@processSp@ce {%
    \akk@counterlabel \expandafter{\the\akk@counterlabel\space}%
    \akk@parsePatternToken@%
}

\def \akk@processGroup#1{%
    \akk@counterlabel \expandafter{\the\akk@counterlabel{#1}}%
    \akk@parsePatternToken@%
}

\def \akk@processOther#1{%
    \akk@counterlabel%
    \expandafter{\the\akk@counterlabel#1}%
    \akk@parsePatternToken@%
}

\providecommand\counterlabelpatternTokenHook{}

\makeatother
