summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/numspell/numspell.sty
blob: ccd615f3963dfeedfb955ca1a4c6ad27d0a07e9a (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
%%
%% `numspell.sty'
%%
%% Package for spelling the non-negative cardinal and ordinal numbers (maximum 66 digits).
%%
%% Copyright 2017-2020 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}[2020/10/01]
\ProvidesPackage{numspell}[2020/11/11 v1.4 Package for spelling cardinal and ordinal numbers]

\RequirePackage{xstring,iflang}

\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
%% --------------------
%%
\newif\if@numspell@lang@notsupported@
\def\numspell@lang@check{%
    \@numspell@lang@notsupported@true%
    \def\numspell@langname{en}%
    \IfLanguageName{english}{\@numspell@lang@notsupported@false}{}%
    \IfLanguageName{british}{\@numspell@lang@notsupported@false}{}%
    \IfLanguageName{ukenglish}{\@numspell@lang@notsupported@false}{}%
    \IfLanguageName{UKenglish}{\@numspell@lang@notsupported@false}{}%
    \IfLanguageName{american}{\@numspell@lang@notsupported@false\numspell@US}{}%
    \IfLanguageName{usenglish}{\@numspell@lang@notsupported@false\numspell@US}{}%
    \IfLanguageName{USenglish}{\@numspell@lang@notsupported@false\numspell@US}{}%
    \IfLanguageName{magyar}{\@numspell@lang@notsupported@false\def\numspell@langname{hu}}{}%
    \IfLanguageName{hungarian}{\@numspell@lang@notsupported@false\def\numspell@langname{hu}}{}%
    \IfLanguageName{german}{\@numspell@lang@notsupported@false\def\numspell@langname{de}}{}%
    \IfLanguageName{ngerman}{\@numspell@lang@notsupported@false\def\numspell@langname{de}}{}%
    \IfLanguageName{french}{\@numspell@lang@notsupported@false\def\numspell@langname{fr}}{}%
    \IfLanguageName{italian}{\@numspell@lang@notsupported@false\def\numspell@langname{it}}{}%
    \if@numspell@lang@notsupported@\PackageWarning{numspell}{\languagename\space is not supported language in numspell}\fi%
}

%% -------------------------
%% Loading language packages
%% -------------------------
%%
\AddToHook{begindocument/before}{
\RequirePackage{numspell-english}
\ifdefined\datemagyar\RequirePackage{numspell-magyar}\fi%
\ifdefined\datehungarian\RequirePackage{numspell-magyar}\fi%
\ifdefined\dategerman\RequirePackage{numspell-german}\fi%
\ifdefined\datengerman\RequirePackage{numspell-german}\fi%
\ifdefined\datefrench\RequirePackage{numspell-french}\fi%
\ifdefined\dateitalian\RequirePackage{numspell-italian}\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