summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/akktex/akkcounterlabelpattern.sty
blob: 5a96ffc083f10b2e379a0fd0ca0eecbe3e6fb790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
%=====================================================================================================================================
% 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