summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/numspell/numspell.sty
blob: 3fd2fcc178889dba247b1fcb9d500b49e6fc34e8 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
%%
%% `numspell.sty'
%%
%% Package for spelling the non-negative cardinal and ordinal numbers (maximum 66 digits).
%%
%% Copyright 2017- by Tibor Tomacs
%%
%% 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 (at your option) 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.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Tibor Tomacs.
%%
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{numspell}[2017/09/30 v1.2 Package for spelling cardinal and ordinal numbers]

\RequirePackage{xstring}
\RequirePackage{pdftexcmds}
\RequirePackage{etoolbox}

\newcounter{numspell@counter@tempa}
\newcounter{numspell@counter@tempb}
\newcounter{numspell@counter@tempc}
\newcounter{numspell@groupcount}

%% -------------------------------
%% \numspell@group@digits{<group>} (<group>=000,001,...,999)
%% -------------------------------
%% It generates the following parameters:
%%
%% \numspell@group@digit@i   = 1st digit
%% \numspell@group@digit@ii  = 2nd digit
%% \numspell@group@digit@iii = 3rd digit
%% 
\def\numspell@group@digits#1{%
    \StrChar{#1}{1}[\numspell@group@digit@i]%
    \StrChar{#1}{2}[\numspell@group@digit@ii]%
    \StrChar{#1}{3}[\numspell@group@digit@iii]%
}

%% --------------------------------
%% \numspell@groups{<num1>}{<num2>} (<num1>*10^<num2>=0,1,...,999999999999...9 (66 digits)=10^67-1)
%% --------------------------------
%% It generates the following parameters:
%%
%% \numspell@num                = <num1>*10^<num2>
%% \numspell@numlength          = number of \numspell@num digits
%% \numspell@fullnum            = (66-\numspell@numlength) pieces 0, then \numspell@num
%% \numspell@group@i            = 64th, 65th and 66th digits of \numspell@fullnum --> [group1]
%% \numspell@group@ii           = 61th, 62th and 63th digits of \numspell@fullnum --> [group2]
%% \numspell@group@iii          = 58th, 59th and 60th digits of \numspell@fullnum --> [group3]
%% ...                         ...                                                    ...
%% \numspell@group@xxii         = 1st, 2nd and 3rd digits of \numspell@fullnum    --> [group22]
%% \numspell@group@max          = maximum <number>, which such that, [group<number>] is positive
%% \numspell@group@min          = minimum <number>, which such that, [group<number>] is positive
%% \numspell@group@<num>@before = number of positive groups, whose serial number is less then <num> (<num>=ii,iii,...,xxii)
%%
\def\numspell@groups#1#2{%
    \def\numspell@num{#1}%
    \setcounter{numspell@counter@tempa}{0}%
    \@whilenum\value{numspell@counter@tempa}<#2%
        \do{%
            \stepcounter{numspell@counter@tempa}%
            \g@addto@macro\numspell@num{0}%
        }%
    \def\numspell@fullnum{}%
    \StrLen{\numspell@num}[\numspell@numlength]%
    \ifnum\numspell@numlength>66\@latexerr{Number too big (\numspell@numlength\space digits). Maximum 66 digits}{}\fi%
    \setcounter{numspell@counter@tempa}{66}%
    \addtocounter{numspell@counter@tempa}{-\numspell@numlength}%
    \setcounter{numspell@counter@tempb}{0}%
    \@whilenum\value{numspell@counter@tempb}<\value{numspell@counter@tempa}%
        \do{%
            \stepcounter{numspell@counter@tempb}%
            \g@addto@macro\numspell@fullnum{0}%
        }%
    \g@addto@macro\numspell@fullnum{\numspell@num}%
    \StrMid{\numspell@fullnum}{64}{66}[\numspell@group@i]%
    \StrMid{\numspell@fullnum}{61}{63}[\numspell@group@ii]%
    \StrMid{\numspell@fullnum}{58}{60}[\numspell@group@iii]%
    \StrMid{\numspell@fullnum}{55}{57}[\numspell@group@iv]%
    \StrMid{\numspell@fullnum}{52}{54}[\numspell@group@v]%
    \StrMid{\numspell@fullnum}{49}{51}[\numspell@group@vi]%
    \StrMid{\numspell@fullnum}{46}{48}[\numspell@group@vii]%
    \StrMid{\numspell@fullnum}{43}{45}[\numspell@group@viii]%
    \StrMid{\numspell@fullnum}{40}{42}[\numspell@group@ix]%
    \StrMid{\numspell@fullnum}{37}{39}[\numspell@group@x]%
    \StrMid{\numspell@fullnum}{34}{36}[\numspell@group@xi]%
    \StrMid{\numspell@fullnum}{31}{33}[\numspell@group@xii]%
    \StrMid{\numspell@fullnum}{28}{30}[\numspell@group@xiii]%
    \StrMid{\numspell@fullnum}{25}{27}[\numspell@group@xiv]%
    \StrMid{\numspell@fullnum}{22}{24}[\numspell@group@xv]%
    \StrMid{\numspell@fullnum}{19}{21}[\numspell@group@xvi]%
    \StrMid{\numspell@fullnum}{16}{18}[\numspell@group@xvii]%
    \StrMid{\numspell@fullnum}{13}{15}[\numspell@group@xviii]%
    \StrMid{\numspell@fullnum}{10}{12}[\numspell@group@xix]%
    \StrMid{\numspell@fullnum}{7}{9}[\numspell@group@xx]%
    \StrMid{\numspell@fullnum}{4}{6}[\numspell@group@xxi]%
    \StrMid{\numspell@fullnum}{1}{3}[\numspell@group@xxii]%
    \def\numspell@group@max{0}%
    \setcounter{numspell@counter@tempa}{0}%
    \@whilenum\value{numspell@counter@tempa}<22%
        \do{%
            \stepcounter{numspell@counter@tempa}%
            \ifnum\csname numspell@group@\roman{numspell@counter@tempa}\endcsname>0%
                \edef\numspell@group@max{\thenumspell@counter@tempa}%
            \fi%
        }%
    \def\numspell@group@min{0}%
    \setcounter{numspell@counter@tempa}{23}%
    \@whilenum\value{numspell@counter@tempa}>1%
        \do{%
            \addtocounter{numspell@counter@tempa}{-1}%
            \ifnum\csname numspell@group@\roman{numspell@counter@tempa}\endcsname>0%
                \edef\numspell@group@min{\thenumspell@counter@tempa}%
            \fi%
        }%
    \setcounter{numspell@counter@tempa}{1}%
    \@whilenum\value{numspell@counter@tempa}<22%
        \do{%
            \stepcounter{numspell@counter@tempa}%
            \setcounter{numspell@counter@tempb}{1}%
            \setcounter{numspell@counter@tempc}{0}%
            \@whilenum\value{numspell@counter@tempb}<\value{numspell@counter@tempa}%
                \do{%
                    \ifnum\csname numspell@group@\roman{numspell@counter@tempb}\endcsname>0%
                        \stepcounter{numspell@counter@tempc}%
                    \fi%
                    \stepcounter{numspell@counter@tempb}%
                }%
                \expandafter\protected@edef\csname numspell@group@\roman{numspell@counter@tempa}@before\endcsname{\thenumspell@counter@tempc}%
        }%
}

%% --------------------
%% \numspell@{<string>}
%% --------------------
%% E.g. \def\thenumspell{}\numspell@{one}\numspell@{ hundred}\thenumspell --> 'one hundred'
%%
\def\thenumspell{}
\def\numspell@#1{\g@addto@macro\thenumspell{#1}}

%% ---------------------
%% \numspellsave{<name>}
%% ---------------------
%% E.g. \def\thenumspell{one}\numspellsave{foo}\thenumspellfoo --> 'one'
%%
\DeclareRobustCommand*{\numspellsave}[1]{\expandafter\protected@xdef\csname thenumspell#1\endcsname{\thenumspell}\ignorespaces}

%% -------------------------------
%% \numspelldashspace{<length>}
%% -------------------------------
%% It determinates the maximal flexibility of the spaces around the dashes. Default: 2pt
%% E.g. \numspelldashspace{5pt} --> \numspell@dash@ = \numspell@{\nobreak\hskip0pt plus5pt-\hskip0pt plus5pt}
%%
\def\numspell@dash@@#1{\leavevmode\nobreak\hskip0pt plus#1-\hskip0pt plus#1\relax}
\def\numspelldashspace#1{\def\numspell@dash@{\numspell@{\numspell@dash@@{#1}}}\ignorespaces}
\numspelldashspace{2pt}

%% -------------------------
%% \numspell[<num2>]{<num1>} (Default <num2> is 0)
%% -------------------------
%% \numspell@num@spell@<lang>{<num1>}{<num2>}\thenumspell
%%
%% --------------------------
%% \numspell*[<num2>]{<num1>}
%% --------------------------
%% It works like \numspell, but \thenumspell will not be expanded.
%%
\newcommand{\numspell@output}[2][0]{\csname numspell@num@spell@\numspell@langname\endcsname{#2}{#1}\thenumspell}
\newcommand{\numspell@@output}[2][0]{\csname numspell@num@spell@\numspell@langname\endcsname{#2}{#1}\ignorespaces}
\DeclareRobustCommand*{\numspell}{\numspell@lang@check\@ifstar{\numspell@@output}{\numspell@output}}

%% -------------------------
%% \Numspell[<num2>]{<num1>} (Default <num2> is 0)
%% -------------------------
%% \numspell@num@spell@<lang>{<num1>}{<num2>}\numspell@uppercase\thenumspell
%%
%% --------------------------
%% \Numspell*[<num2>]{<num1>}
%% --------------------------
%% It works like \Numspell, but \thenumspell will not be expanded.
%%
\newcommand{\Numspell@output}[2][0]{\csname numspell@num@spell@\numspell@langname\endcsname{#2}{#1}%
                                    \csname numspell@uppercase@\numspell@langname\endcsname\thenumspell}
\newcommand{\Numspell@@output}[2][0]{\csname numspell@num@spell@\numspell@langname\endcsname{#2}{#1}%
                                     \csname numspell@uppercase@\numspell@langname\endcsname\ignorespaces}
\DeclareRobustCommand*{\Numspell}{\numspell@lang@check\@ifstar{\Numspell@@output}{\Numspell@output}}

%% ----------------------------
%% \ordnumspell[<num2>]{<num1>} (Default <num2> is 0)
%% ----------------------------
%% \numspell@ordnum@spell@<lang>{<num1>}{<num2>}\thenumspell
%%
%% -----------------------------
%% \ordnumspell*[<num2>]{<num1>}
%% -----------------------------
%% It works like \ordnumspell, but \thenumspell will not be expanded.
%%
\newcommand{\ordnumspell@output}[2][0]{\csname numspell@ordnum@spell@\numspell@langname\endcsname{#2}{#1}\thenumspell}
\newcommand{\ordnumspell@@output}[2][0]{\csname numspell@ordnum@spell@\numspell@langname\endcsname{#2}{#1}\ignorespaces}
\DeclareRobustCommand*{\ordnumspell}{\numspell@lang@check\@ifstar{\ordnumspell@@output}{\ordnumspell@output}}

%% ----------------------------
%% \Ordnumspell[<num2>]{<num1>} (Default <num2> is 0)
%% ----------------------------
%% \numspell@ordnum@spell@<lang>{<num1>}{<num2>}\numspell@uppercase\thenumspell
%%
%% -----------------------------
%% \Ordnumspell*[<num2>]{<num1>}
%% -----------------------------
%% It works like \Ordnumspell, but \thenumspell will not be expanded.
%%
\newcommand{\Ordnumspell@output}[2][0]{\csname numspell@ordnum@spell@\numspell@langname\endcsname{#2}{#1}%
                                       \csname numspell@uppercase@\numspell@langname\endcsname\thenumspell}
\newcommand{\Ordnumspell@@output}[2][0]{\csname numspell@ordnum@spell@\numspell@langname\endcsname{#2}{#1}%
                                        \csname numspell@uppercase@\numspell@langname\endcsname\ignorespaces}
\DeclareRobustCommand*{\Ordnumspell}{\numspell@lang@check\@ifstar{\Ordnumspell@@output}{\Ordnumspell@output}}

%% --------------------
%% \numspell@lang@check
%% --------------------
%% If english is active, then \numspell@langname will be 'en'.
%% If magyar is active, then \numspell@langname will be 'hu'.
%% If german or ngerman is active, then \numspell@langname will be 'de'.
%% If french is active, then \numspell@langname will be 'fr'.
%% If italian is active, then \numspell@langname will be 'it'.
%% Otherwise error.
%%
\newif\if@numspell@lang@notsupported@
\def\numspell@lang@check{%
    \@numspell@lang@notsupported@true%
    \ifnum\pdf@strcmp{\languagename}{english}=\z@\@numspell@lang@notsupported@false\def\numspell@langname{en}\fi%
    \ifnum\pdf@strcmp{\languagename}{magyar}=\z@\@numspell@lang@notsupported@false\def\numspell@langname{hu}\fi%
    \ifnum\pdf@strcmp{\languagename}{german}=\z@\@numspell@lang@notsupported@false\def\numspell@langname{de}\fi%
    \ifnum\pdf@strcmp{\languagename}{ngerman}=\z@\@numspell@lang@notsupported@false\def\numspell@langname{de}\fi%
    \ifnum\pdf@strcmp{\languagename}{french}=\z@\@numspell@lang@notsupported@false\def\numspell@langname{fr}\fi%
    \ifnum\pdf@strcmp{\languagename}{italian}=\z@\@numspell@lang@notsupported@false\def\numspell@langname{it}\fi%
    \if@numspell@lang@notsupported@\@latexerr{'numspell.sty' error! Not supported language: \languagename}{}\fi%
}

%% -------------------------
%% Loading language packages
%% -------------------------
%% If babel or polyglossia loads english, then numspell loads numspell-english.sty.
%% If babel or polyglossia loads magyar, then numspell loads numspell-magyar.sty.
%% If babel or polyglossia loads german or ngerman, then numspell loads numspell-german.sty.
%% If babel or polyglossia loads french, then numspell loads numspell-french.sty.
%% If babel or polyglossia loads italian, then numspell loads numspell-italian.sty.
%% Otherwise numspell loads numspell-english.sty.
%%
\AtEndPreamble{%
\newif\if@numspell@lang@loaded@
\bbl@iflanguage\languagename{\expandafter\ifx\csname dateenglish\endcsname\relax\else\RequirePackage{numspell-english}\@numspell@lang@loaded@true\fi}%
\bbl@iflanguage\languagename{\expandafter\ifx\csname datemagyar\endcsname\relax\else\RequirePackage{numspell-magyar}\@numspell@lang@loaded@true\fi}%
\bbl@iflanguage\languagename{\expandafter\ifx\csname dategerman\endcsname\relax\else\RequirePackage{numspell-german}\@numspell@lang@loaded@true\fi}%
\bbl@iflanguage\languagename{\expandafter\ifx\csname datengerman\endcsname\relax\else\RequirePackage{numspell-german}\@numspell@lang@loaded@true\fi}%
\bbl@iflanguage\languagename{\expandafter\ifx\csname datefrench\endcsname\relax\else\RequirePackage{numspell-french}\@numspell@lang@loaded@true\fi}%
\bbl@iflanguage\languagename{\expandafter\ifx\csname dateitalian\endcsname\relax\else\RequirePackage{numspell-italian}\@numspell@lang@loaded@true\fi}%
\if@numspell@lang@loaded@\else\RequirePackage{numspell-english}\fi%
}

%% ---------------------------------
%% Instructions for language package
%% ---------------------------------
%%
%% If the language name is <language> in babel or polyglossia,
%% then the name of the language package file will be numspell-<language>.sty (e.g. numspell-english.sty).
%%
%% Extend the following commands in the numspell.sty:
%% \AtEndPreamble
%% \numspell@lang@check (Use the \def\numspell@langname{<lang>}, where <lang> is abbreviation of the <language>, e.g. 'en' in case 'english'.)
%%
%% Define the following commands in numspell-<language>.sty:
%% \numspell@uppercase@<lang>
%% \numspell@group@<num>@<lang>@name
%% \numspell@ordgroup@<num>@<lang>@name
%% \numspell@group@spell@<lang>
%% \numspell@ordgroup@spell@<lang>
%% \numspell@num@spell@<lang>
%% \numspell@ordnum@spell@<lang>
%% (See <lang> at \numspell@lang@check.)
%%
%% Put into the \numspell@{...} command the words of the spelling (e.g. \numspell@{one}).
%%
%% Type the non-ascii characters as LaTeX-commands: \'{a}, \'{e}, \ss{}, etc.
%%
%% If the first character is non-ascii, then place it within braces (see \numspell@uppercase@<lang>) (e.g. {\"{o}}t).
%%
%% Do not use \numspell@{-} as dash! Instead of it: \numspell@dash@ (see \numspelldashspace).
%%
%% Do not use the following form: \def\foo{text} ... \numspell@{\foo}
%% Instead of it:                 \def\numspell@foo@<lang>{\numspell@{text}} ... \numspell@foo@<lang>

\endinput