summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/currency/currency.dtx
blob: 4824ba50db01fa97236062555400b0365df4502b (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
% \iffalse meta-comment
%
% Copyright (C) 2018 by Antoine Lejay 
% -------------------------------------------------------
% 
% This file 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.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{currency.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01]
%<package>\ProvidesPackage{currency}
%<*package>
    [2018/10/22 v0.4 .dtx currency file]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{currency}
\usepackage{url}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{currency.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.1}{2016/05/25}{Initial version}
% \changes{v0.2}{2016/06/15}{Adding vXXX as an alias for dXXX}
% \changes{v0.3}{2017/07/04}{Dropping spurious space in the definition of cXXX}
% \changes{v0.4}{2018/10/22}{Change DeclareDocumentCommand for compability with TexLive 2018}
%
%
% \GetFileInfo{currency.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment}
% 
%
% \title{The \textsf{currency} package\thanks{This document
%   corresponds to \textsf{currency}~\fileversion, dated \filedate.}}
% \author{Antoine Lejay \\ \texttt{Antoine.Lejay@univ-lorraine.fr}}
%
% \maketitle
%
% 
% \DefineCurrency{EUR}{name={euro},plural-name={euros},symbol={\euro},iso={EUR},kind=iso}
% \DefineCurrency{USD}{name={dollar},plural-name={dollars},symbol={\$},iso={USD},kind=iso}
%
% \section{Introduction}
%
% This package creates macros for defined currencies which follow
% the ISO 4217 codes, with various formatting options for both
% the currency (code, symbols, names, ...) and the numbers (using \texttt{siunitx}).
% 
% The currency code ISO 4217 specifies the code of the currency as
% a three-letters code. The first two ones are the code of the country
% according to ISO 3166. The last one is the name of the currency name. 
%
% \section{Licence}
%
% 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
%   \url{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 Antoine Lejay.
%
% \section{Documentation and sources}
%
% This package is documented in \texttt{currency\_doc}.
%
% A source of this package is hosted in
% \begin{center}
% \url{https://github.com/antoinelejay/currency}
% \end{center}
% 
%
%
% \section{Implementation}
%
% \subsection{Dependencies}
%
%    \begin{macrocode}
\RequirePackage{siunitx}
\RequirePackage{pgfkeys}
\RequirePackage{etoolbox}
\RequirePackage{xparse}
\RequirePackage{expl3}
\RequirePackage{textcomp}
\RequirePackage{eurosym}
%    \end{macrocode}
% The unit could be printed before or after
%    \begin{macrocode}
\newif\ifcurrencynumber 
\pgfkeys{/currency/.cd,number/.is if=currencynumber,number=true}
\newif\ifprintbefore
\pgfkeys{/currency/pre/.is if=printbefore}
\pgfkeys{/currency/name/.initial=ZZZ}
\pgfkeys{/currency/symbol/.initial=\textcurrency}
%    \end{macrocode}
%
% Defines a style currency, which is general and empty. 
% The styles are then called in order 
%   - the currency style
%   - currency (initially empty)
%   - the options passed to the command

%    \begin{macrocode}
\pgfkeys{/currency/currency/.style={}}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand{\CurrencySetup}[1]{%
    \pgfkeys{/currency/currency/.style={#1}}%
}
\newcommand{\CurrencySetupAppend}[1]{%
    \pgfkeys{/currency/currency/.append style={#1}}%
}
%    \end{macrocode}
%
% \subsection{Names and symbols definitions}
%
% The commands \verb|\currencyName|, \verb|\currencyNames|, \verb|\currencyISO| and \verb|\currencySymbol|
% are used internally every time a currency is called to define the name, the plural, 
% the ISO code and the symbol associated to a currency.
%
%    \begin{macrocode}
\pgfkeys{/currency/name/.store in=\currencyName}
\pgfkeys{/currency/plural/.store in=\currencyNames}
\pgfkeys{/currency/plural/.initial=\currencyName s}
\pgfkeys{/currency/iso/.store in=\currencyISO}
\pgfkeys{/currency/symbol/.store in=\currencySymbol}
%    \end{macrocode}
%
% \subsection{Keys for formatting}
%
% Several keys are devoted to insert tokens before or after th unit 
% and the amount, set up the font, ...
%
%    \begin{macrocode}
% between the symbol and the num if before
\pgfkeys{/currency/pre-between/.initial={~}} 
% between the symbol and the num if after
\pgfkeys{/currency/post-between/.initial={~}} 
% tokens that are inserted before
\pgfkeys{/currency/before/.initial={}} 
% append to before
\pgfkeys{/currency/before+/.code={\pgfkeys{/currency/before/.append={#1}}}} 
% prepend to before
\pgfkeys{/currency/before</.code={\pgfkeys{/currency/before/.prefix={#1}}}} 
\pgfkeys{/currency/after/.initial={}} % tokens that are inserted after
% append to after
\pgfkeys{/currency/after+/.code={\pgfkeys{/currency/after/.append={#1}}}} 
% prepend to after
\pgfkeys{/currency/after</.code={\pgfkeys{/currency/after/.prefix={#1}}}} 
\pgfkeys{/currency/font/.initial={}} % for setting up the fonts
% append to the font definition
\pgfkeys{/currency/font+/.code={\pgfkeys{/currency/font/.append={#1}}}} 
% the prefix before the name
\pgfkeys{/currency/prefix/.initial={}} 
%    \end{macrocode}
%
% Several options are given for printing the kind of unit (\verb|iso|, 
% \verb|plural|, \verb|name|, \verb|symbol|).
%    \begin{macrocode}
%
\pgfkeys{/currency/kind/.is choice}
\pgfkeys{/currency/kind/iso/.code=%
    {\def\currencyunit{\currencyISO}}}
\pgfkeys{/currency/kind/plural/.code=%
    {\def\currencyunit{\currencyNames}\printbeforefalse}}
\pgfkeys{/currency/kind/name/.code=%
    {\def\currencyunit{\currencyName}\printbeforefalse}}
\pgfkeys{/currency/kind/symbol/.code=%
    {\def\currencyunit{\currencySymbol}}}
\pgfkeys{/currency/kind=iso}
%    \end{macrocode}
%
% Options are also define for printing the cents (\verb|true|, \verb|false|, \verb|always|).
%
%    \begin{macrocode}
\pgfkeys{/currency/cents/.is choice}
\pgfkeys{/currency/cents/true/.code=%
    {\sisetup{zero-decimal-to-integer=true}}}
\pgfkeys{/currency/cents/false/.code=%
    {\sisetup{zero-decimal-to-integer=true,round-precision=0}}}
\pgfkeys{/currency/cents/always/.code=%
    {\sisetup{zero-decimal-to-integer=false,round-integer-to-decimal=true}}}
\pgfkeys{/currency/cents=true}
%    \end{macrocode}
%
% \subsection{Dealing with unknown keys}
%
% The unknown keys are passed to the \verb|\num| command of \texttt{siunitx}, which use the \LaTeX3 syntax
% and not the pgf one. 
%
%    \begin{macrocode}
\ExplSyntaxOn
\pgfkeys{/currency/.unknown/.code={%
\edef\Unknown{\pgfkeyscurrentname=\pgfkeyscurrentvalue}%
\keys_set:nV {siunitx} {\Unknown}%
    }}%
\ExplSyntaxOff
%    \end{macrocode}
%
% \subsection{Setting the precision}
%
% The key \verb|base| is used to set the precision. 
% Its value is passed to the key \verb|round-precision| of \verb|siunitx|.
%
%    \begin{macrocode}
\ExplSyntaxOn
\pgfkeys{/currency/base/.code={%
\edef\SetPrecision{round-precision=\pgfkeyscurrentvalue}%
\keys_set:nV {siunitx} {\SetPrecision}%
    }}%
\ExplSyntaxOff
%    \end{macrocode}
%
% \subsection{Defining new currencies}
%
% The first argument is the name of the currency, normally its ISO symbol (3 letters).
% This commands defines two global commands.
%
% \begin{macro}{\displayCurrency} 
%
% This is a generic macro for displaying the currency symbol and amount. This macro does not aim
% at being called directly.
%
%
%    \begin{macrocode}
\ExplSyntaxOn
\DeclareDocumentCommand{\displayCurrency}{m o m}{
\begingroup%
% default parameters, to put outside
\sisetup{round-mode=places,round-precision=2}
\IfNoValueTF{#2}%
    {\pgfkeys{/currency/.cd,#1,currency}}%
    {\pgfkeys{/currency/.cd,#1,currency,#2}}%
\ifbool{currencynumber}%
    {\sisetup{parse-numbers=true}}%
    {\sisetup{parse-numbers=false}}%
\edef\value{#3}% store the value of the currency
%
\ifprintbefore%
% true clause / print the unit before
\pgfkeys{/currency/font}%
\pgfkeys{/currency/before}%
\text{\pgfkeys{/currency/prefix}\currencyunit}%
\pgfkeys{/currency/pre-between}%
\num{\value}\pgfkeys{/currency/after}%
\else% print the unit after 
\pgfkeys{/currency/font}%
\pgfkeys{/currency/before}%
\num{\value}%
\pgfkeys{/currency/post-between}%
\text{\pgfkeys{/currency/prefix}\currencyunit}%
\pgfkeys{/currency/after}%
\fi%
\endgroup%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\displayCurrencySymbol} 
%
% This is a generic macro for displaying the currency symbol. This macro does not aim
% at being called directly.
%
%    \begin{macrocode}
\DeclareDocumentCommand{\displayCurrencySymbol}{m o}{
\begingroup%
    \IfNoValueTF{#2}%
	{\pgfkeys{/currency/.cd,#1,currency}}%
	{\pgfkeys{/currency/.cd,#1,currency,#2}}%
    \pgfkeys{/currency/font}%
    \pgfkeys{/currency/before}%
    \text{\pgfkeys{/currency/prefix}\currencyunit}% 
	\pgfkeys{/currency/after}%
\endgroup%
}
%    \end{macrocode}
% \end{macro}
\ExplSyntaxOff
%
% \begin{macro}{\DefineCurrency} 
% Define the parameters for a new currency. The first parameter is 
% a three letters code of type \verb|XXX| (actually, it should by any suitable word).
% Global commands \verb|\dXXX|
% (for printing currency with an amount)
% and \verb|\cXXX| (for printing only the symbols) are then defined, as
% well as a style under the pgfkey \verb|/currency/XXX|.
%
%    \begin{macrocode}
\newcommand{\DefineCurrency}[2]{%
\pgfkeys{/currency/#1/.style={#2}}%%
% define the currency with digits as \dXXX
\csgdef{d#1}{\displayCurrency{#1}}
% end of \dXXX definition
% define the currency symbol as \cXXX with one optional argument
% \global\expandafter\DeclareDocumentCommand\csname c#1\endcsname{o}{%
\csgdef{c#1}{\displayCurrencySymbol{#1}}
% end of \cXXX definition
}%
%    \end{macrocode}
% \end{macro}
%
% \subsection{Predefined styles}
%
% Some styles are defined to shorten the typesetting.
%
%    \begin{macrocode}
\pgfkeys{/currency/@iso/.style={kind=iso}}
\pgfkeys{/currency/@symb/.style={kind=symbol}}
\pgfkeys{/currency/@sy/.style={kind=symbol}}
\pgfkeys{/currency/@name/.style={kind=name}}
\pgfkeys{/currency/@na/.style={kind=name}}
\pgfkeys{/currency/@plural/.style={kind=plural}}
\pgfkeys{/currency/@pl/.style={kind=plural}}
\pgfkeys{/currency/no-parse/.style={number=false}}
%    \end{macrocode}
%
% \Finale
\endinput