| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Copyright (C) 2018 by Antoine Lejay |
| 4 |
% ------------------------------------------------------- |
| 5 |
% |
| 6 |
% This file may be distributed and/or modified under the |
| 7 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 8 |
% of this license or (at your option) any later version. |
| 9 |
% The latest version of this license is in: |
| 10 |
% |
| 11 |
% http://www.latex-project.org/lppl.txt |
| 12 |
% |
| 13 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 14 |
% version 2005/12/01 or later. |
| 15 |
% |
| 16 |
% \fi |
| 17 |
% |
| 18 |
% \iffalse |
| 19 |
%<*driver> |
| 20 |
\ProvidesFile{currency.dtx} |
| 21 |
%</driver> |
| 22 |
%<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01] |
| 23 |
%<package>\ProvidesPackage{currency} |
| 24 |
%<*package> |
| 25 |
[2018/10/22 v0.4 .dtx currency file] |
| 26 |
%</package> |
| 27 |
% |
| 28 |
%<*driver> |
| 29 |
\documentclass{ltxdoc} |
| 30 |
\usepackage{currency} |
| 31 |
\usepackage{url} |
| 32 |
\EnableCrossrefs |
| 33 |
\CodelineIndex |
| 34 |
\RecordChanges |
| 35 |
\begin{document} |
| 36 |
\DocInput{currency.dtx} |
| 37 |
\PrintChanges |
| 38 |
\PrintIndex |
| 39 |
\end{document} |
| 40 |
%</driver> |
| 41 |
% \fi |
| 42 |
% |
| 43 |
% \CheckSum{0} |
| 44 |
% |
| 45 |
% \CharacterTable |
| 46 |
% {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 |
| 47 |
% 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 |
| 48 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 49 |
% Exclamation \! Double quote \" Hash (number) \# |
| 50 |
% Dollar \$ Percent \% Ampersand \& |
| 51 |
% Acute accent \' Left paren \( Right paren \) |
| 52 |
% Asterisk \* Plus \+ Comma \, |
| 53 |
% Minus \- Point \. Solidus \/ |
| 54 |
% Colon \: Semicolon \; Less than \< |
| 55 |
% Equals \= Greater than \> Question mark \? |
| 56 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 57 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 58 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 59 |
% Right brace \} Tilde \~} |
| 60 |
% |
| 61 |
% |
| 62 |
% \changes{v0.1}{2016/05/25}{Initial version} |
| 63 |
% \changes{v0.2}{2016/06/15}{Adding vXXX as an alias for dXXX} |
| 64 |
% \changes{v0.3}{2017/07/04}{Dropping spurious space in the definition of cXXX} |
| 65 |
% \changes{v0.4}{2018/10/22}{Change DeclareDocumentCommand for compability with TexLive 2018} |
| 66 |
% |
| 67 |
% |
| 68 |
% \GetFileInfo{currency.dtx} |
| 69 |
% |
| 70 |
% \DoNotIndex{\newcommand,\newenvironment} |
| 71 |
% |
| 72 |
% |
| 73 |
% \title{The \textsf{currency} package\thanks{This document |
| 74 |
% corresponds to \textsf{currency}~\fileversion, dated \filedate.}} |
| 75 |
% \author{Antoine Lejay \\ \texttt{[email protected]}} |
| 76 |
% |
| 77 |
% \maketitle |
| 78 |
% |
| 79 |
% |
| 80 |
% \DefineCurrency{EUR}{name={euro},plural-name={euros},symbol={\euro},iso={EUR},kind=iso} |
| 81 |
% \DefineCurrency{USD}{name={dollar},plural-name={dollars},symbol={\$},iso={USD},kind=iso} |
| 82 |
% |
| 83 |
% \section{Introduction} |
| 84 |
% |
| 85 |
% This package creates macros for defined currencies which follow |
| 86 |
% the ISO 4217 codes, with various formatting options for both |
| 87 |
% the currency (code, symbols, names, ...) and the numbers (using \texttt{siunitx}). |
| 88 |
% |
| 89 |
% The currency code ISO 4217 specifies the code of the currency as |
| 90 |
% a three-letters code. The first two ones are the code of the country |
| 91 |
% according to ISO 3166. The last one is the name of the currency name. |
| 92 |
% |
| 93 |
% \section{Licence} |
| 94 |
% |
| 95 |
% This work may be distributed and/or modified under the |
| 96 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 97 |
% of this license or (at your option) any later version. |
| 98 |
% The latest version of this license is in |
| 99 |
% \url{http://www.latex-project.org/lppl.txt} |
| 100 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 101 |
% version 2005/12/01 or later. |
| 102 |
% |
| 103 |
% This work has the LPPL maintenance status `maintained'. |
| 104 |
% |
| 105 |
% The Current Maintainer of this work is Antoine Lejay. |
| 106 |
% |
| 107 |
% \section{Documentation and sources} |
| 108 |
% |
| 109 |
% This package is documented in \texttt{currency\_doc}. |
| 110 |
% |
| 111 |
% A source of this package is hosted in |
| 112 |
% \begin{center} |
| 113 |
% \url{https://github.com/antoinelejay/currency} |
| 114 |
% \end{center} |
| 115 |
% |
| 116 |
% |
| 117 |
% |
| 118 |
% \section{Implementation} |
| 119 |
% |
| 120 |
% \subsection{Dependencies} |
| 121 |
% |
| 122 |
% \begin{macrocode} |
| 123 |
\RequirePackage{siunitx} |
| 124 |
\RequirePackage{pgfkeys} |
| 125 |
\RequirePackage{etoolbox} |
| 126 |
\RequirePackage{xparse} |
| 127 |
\RequirePackage{expl3} |
| 128 |
\RequirePackage{textcomp} |
| 129 |
\RequirePackage{eurosym} |
| 130 |
% \end{macrocode} |
| 131 |
% The unit could be printed before or after |
| 132 |
% \begin{macrocode} |
| 133 |
\newif\ifcurrencynumber |
| 134 |
\pgfkeys{/currency/.cd,number/.is if=currencynumber,number=true} |
| 135 |
\newif\ifprintbefore |
| 136 |
\pgfkeys{/currency/pre/.is if=printbefore} |
| 137 |
\pgfkeys{/currency/name/.initial=ZZZ} |
| 138 |
\pgfkeys{/currency/symbol/.initial=\textcurrency} |
| 139 |
% \end{macrocode} |
| 140 |
% |
| 141 |
% Defines a style currency, which is general and empty. |
| 142 |
% The styles are then called in order |
| 143 |
% - the currency style |
| 144 |
% - currency (initially empty) |
| 145 |
% - the options passed to the command |
| 146 |
|
| 147 |
% \begin{macrocode} |
| 148 |
\pgfkeys{/currency/currency/.style={}} |
| 149 |
% \end{macrocode} |
| 150 |
% |
| 151 |
% \begin{macrocode} |
| 152 |
\newcommand{\CurrencySetup}[1]{% |
| 153 |
\pgfkeys{/currency/currency/.style={#1}}% |
| 154 |
} |
| 155 |
\newcommand{\CurrencySetupAppend}[1]{% |
| 156 |
\pgfkeys{/currency/currency/.append style={#1}}% |
| 157 |
} |
| 158 |
% \end{macrocode} |
| 159 |
% |
| 160 |
% \subsection{Names and symbols definitions} |
| 161 |
% |
| 162 |
% The commands \verb|\currencyName|, \verb|\currencyNames|, \verb|\currencyISO| and \verb|\currencySymbol| |
| 163 |
% are used internally every time a currency is called to define the name, the plural, |
| 164 |
% the ISO code and the symbol associated to a currency. |
| 165 |
% |
| 166 |
% \begin{macrocode} |
| 167 |
\pgfkeys{/currency/name/.store in=\currencyName} |
| 168 |
\pgfkeys{/currency/plural/.store in=\currencyNames} |
| 169 |
\pgfkeys{/currency/plural/.initial=\currencyName s} |
| 170 |
\pgfkeys{/currency/iso/.store in=\currencyISO} |
| 171 |
\pgfkeys{/currency/symbol/.store in=\currencySymbol} |
| 172 |
% \end{macrocode} |
| 173 |
% |
| 174 |
% \subsection{Keys for formatting} |
| 175 |
% |
| 176 |
% Several keys are devoted to insert tokens before or after th unit |
| 177 |
% and the amount, set up the font, ... |
| 178 |
% |
| 179 |
% \begin{macrocode} |
| 180 |
% between the symbol and the num if before |
| 181 |
\pgfkeys{/currency/pre-between/.initial={~}} |
| 182 |
% between the symbol and the num if after |
| 183 |
\pgfkeys{/currency/post-between/.initial={~}} |
| 184 |
% tokens that are inserted before |
| 185 |
\pgfkeys{/currency/before/.initial={}} |
| 186 |
% append to before |
| 187 |
\pgfkeys{/currency/before+/.code={\pgfkeys{/currency/before/.append={#1}}}} |
| 188 |
% prepend to before |
| 189 |
\pgfkeys{/currency/before</.code={\pgfkeys{/currency/before/.prefix={#1}}}} |
| 190 |
\pgfkeys{/currency/after/.initial={}} % tokens that are inserted after |
| 191 |
% append to after |
| 192 |
\pgfkeys{/currency/after+/.code={\pgfkeys{/currency/after/.append={#1}}}} |
| 193 |
% prepend to after |
| 194 |
\pgfkeys{/currency/after</.code={\pgfkeys{/currency/after/.prefix={#1}}}} |
| 195 |
\pgfkeys{/currency/font/.initial={}} % for setting up the fonts |
| 196 |
% append to the font definition |
| 197 |
\pgfkeys{/currency/font+/.code={\pgfkeys{/currency/font/.append={#1}}}} |
| 198 |
% the prefix before the name |
| 199 |
\pgfkeys{/currency/prefix/.initial={}} |
| 200 |
% \end{macrocode} |
| 201 |
% |
| 202 |
% Several options are given for printing the kind of unit (\verb|iso|, |
| 203 |
% \verb|plural|, \verb|name|, \verb|symbol|). |
| 204 |
% \begin{macrocode} |
| 205 |
% |
| 206 |
\pgfkeys{/currency/kind/.is choice} |
| 207 |
\pgfkeys{/currency/kind/iso/.code=% |
| 208 |
{\def\currencyunit{\currencyISO}}} |
| 209 |
\pgfkeys{/currency/kind/plural/.code=% |
| 210 |
{\def\currencyunit{\currencyNames}\printbeforefalse}} |
| 211 |
\pgfkeys{/currency/kind/name/.code=% |
| 212 |
{\def\currencyunit{\currencyName}\printbeforefalse}} |
| 213 |
\pgfkeys{/currency/kind/symbol/.code=% |
| 214 |
{\def\currencyunit{\currencySymbol}}} |
| 215 |
\pgfkeys{/currency/kind=iso} |
| 216 |
% \end{macrocode} |
| 217 |
% |
| 218 |
% Options are also define for printing the cents (\verb|true|, \verb|false|, \verb|always|). |
| 219 |
% |
| 220 |
% \begin{macrocode} |
| 221 |
\pgfkeys{/currency/cents/.is choice} |
| 222 |
\pgfkeys{/currency/cents/true/.code=% |
| 223 |
{\sisetup{zero-decimal-to-integer=true}}} |
| 224 |
\pgfkeys{/currency/cents/false/.code=% |
| 225 |
{\sisetup{zero-decimal-to-integer=true,round-precision=0}}} |
| 226 |
\pgfkeys{/currency/cents/always/.code=% |
| 227 |
{\sisetup{zero-decimal-to-integer=false,round-integer-to-decimal=true}}} |
| 228 |
\pgfkeys{/currency/cents=true} |
| 229 |
% \end{macrocode} |
| 230 |
% |
| 231 |
% \subsection{Dealing with unknown keys} |
| 232 |
% |
| 233 |
% The unknown keys are passed to the \verb|\num| command of \texttt{siunitx}, which use the \LaTeX3 syntax |
| 234 |
% and not the pgf one. |
| 235 |
% |
| 236 |
% \begin{macrocode} |
| 237 |
\ExplSyntaxOn |
| 238 |
\pgfkeys{/currency/.unknown/.code={% |
| 239 |
\edef\Unknown{\pgfkeyscurrentname=\pgfkeyscurrentvalue}% |
| 240 |
\keys_set:nV {siunitx} {\Unknown}% |
| 241 |
}}% |
| 242 |
\ExplSyntaxOff |
| 243 |
% \end{macrocode} |
| 244 |
% |
| 245 |
% \subsection{Setting the precision} |
| 246 |
% |
| 247 |
% The key \verb|base| is used to set the precision. |
| 248 |
% Its value is passed to the key \verb|round-precision| of \verb|siunitx|. |
| 249 |
% |
| 250 |
% \begin{macrocode} |
| 251 |
\ExplSyntaxOn |
| 252 |
\pgfkeys{/currency/base/.code={% |
| 253 |
\edef\SetPrecision{round-precision=\pgfkeyscurrentvalue}% |
| 254 |
\keys_set:nV {siunitx} {\SetPrecision}% |
| 255 |
}}% |
| 256 |
\ExplSyntaxOff |
| 257 |
% \end{macrocode} |
| 258 |
% |
| 259 |
% \subsection{Defining new currencies} |
| 260 |
% |
| 261 |
% The first argument is the name of the currency, normally its ISO symbol (3 letters). |
| 262 |
% This commands defines two global commands. |
| 263 |
% |
| 264 |
% \begin{macro}{\displayCurrency} |
| 265 |
% |
| 266 |
% This is a generic macro for displaying the currency symbol and amount. This macro does not aim |
| 267 |
% at being called directly. |
| 268 |
% |
| 269 |
% |
| 270 |
% \begin{macrocode} |
| 271 |
\ExplSyntaxOn |
| 272 |
\DeclareDocumentCommand{\displayCurrency}{m o m}{ |
| 273 |
\begingroup% |
| 274 |
% default parameters, to put outside |
| 275 |
\sisetup{round-mode=places,round-precision=2} |
| 276 |
\IfNoValueTF{#2}% |
| 277 |
{\pgfkeys{/currency/.cd,#1,currency}}% |
| 278 |
{\pgfkeys{/currency/.cd,#1,currency,#2}}% |
| 279 |
\ifbool{currencynumber}% |
| 280 |
{\sisetup{parse-numbers=true}}% |
| 281 |
{\sisetup{parse-numbers=false}}% |
| 282 |
\edef\value{#3}% store the value of the currency |
| 283 |
% |
| 284 |
\ifprintbefore% |
| 285 |
% true clause / print the unit before |
| 286 |
\pgfkeys{/currency/font}% |
| 287 |
\pgfkeys{/currency/before}% |
| 288 |
\text{\pgfkeys{/currency/prefix}\currencyunit}% |
| 289 |
\pgfkeys{/currency/pre-between}% |
| 290 |
\num{\value}\pgfkeys{/currency/after}% |
| 291 |
\else% print the unit after |
| 292 |
\pgfkeys{/currency/font}% |
| 293 |
\pgfkeys{/currency/before}% |
| 294 |
\num{\value}% |
| 295 |
\pgfkeys{/currency/post-between}% |
| 296 |
\text{\pgfkeys{/currency/prefix}\currencyunit}% |
| 297 |
\pgfkeys{/currency/after}% |
| 298 |
\fi% |
| 299 |
\endgroup% |
| 300 |
} |
| 301 |
% \end{macrocode} |
| 302 |
% \end{macro} |
| 303 |
% |
| 304 |
% \begin{macro}{\displayCurrencySymbol} |
| 305 |
% |
| 306 |
% This is a generic macro for displaying the currency symbol. This macro does not aim |
| 307 |
% at being called directly. |
| 308 |
% |
| 309 |
% \begin{macrocode} |
| 310 |
\DeclareDocumentCommand{\displayCurrencySymbol}{m o}{ |
| 311 |
\begingroup% |
| 312 |
\IfNoValueTF{#2}% |
| 313 |
{\pgfkeys{/currency/.cd,#1,currency}}% |
| 314 |
{\pgfkeys{/currency/.cd,#1,currency,#2}}% |
| 315 |
\pgfkeys{/currency/font}% |
| 316 |
\pgfkeys{/currency/before}% |
| 317 |
\text{\pgfkeys{/currency/prefix}\currencyunit}% |
| 318 |
\pgfkeys{/currency/after}% |
| 319 |
\endgroup% |
| 320 |
} |
| 321 |
% \end{macrocode} |
| 322 |
% \end{macro} |
| 323 |
\ExplSyntaxOff |
| 324 |
% |
| 325 |
% \begin{macro}{\DefineCurrency} |
| 326 |
% Define the parameters for a new currency. The first parameter is |
| 327 |
% a three letters code of type \verb|XXX| (actually, it should by any suitable word). |
| 328 |
% Global commands \verb|\dXXX| |
| 329 |
% (for printing currency with an amount) |
| 330 |
% and \verb|\cXXX| (for printing only the symbols) are then defined, as |
| 331 |
% well as a style under the pgfkey \verb|/currency/XXX|. |
| 332 |
% |
| 333 |
% \begin{macrocode} |
| 334 |
\newcommand{\DefineCurrency}[2]{% |
| 335 |
\pgfkeys{/currency/#1/.style={#2}}%% |
| 336 |
% define the currency with digits as \dXXX |
| 337 |
\csgdef{d#1}{\displayCurrency{#1}} |
| 338 |
% end of \dXXX definition |
| 339 |
% define the currency symbol as \cXXX with one optional argument |
| 340 |
% \global\expandafter\DeclareDocumentCommand\csname c#1\endcsname{o}{% |
| 341 |
\csgdef{c#1}{\displayCurrencySymbol{#1}} |
| 342 |
% end of \cXXX definition |
| 343 |
}% |
| 344 |
% \end{macrocode} |
| 345 |
% \end{macro} |
| 346 |
% |
| 347 |
% \subsection{Predefined styles} |
| 348 |
% |
| 349 |
% Some styles are defined to shorten the typesetting. |
| 350 |
% |
| 351 |
% \begin{macrocode} |
| 352 |
\pgfkeys{/currency/@iso/.style={kind=iso}} |
| 353 |
\pgfkeys{/currency/@symb/.style={kind=symbol}} |
| 354 |
\pgfkeys{/currency/@sy/.style={kind=symbol}} |
| 355 |
\pgfkeys{/currency/@name/.style={kind=name}} |
| 356 |
\pgfkeys{/currency/@na/.style={kind=name}} |
| 357 |
\pgfkeys{/currency/@plural/.style={kind=plural}} |
| 358 |
\pgfkeys{/currency/@pl/.style={kind=plural}} |
| 359 |
\pgfkeys{/currency/no-parse/.style={number=false}} |
| 360 |
% \end{macrocode} |
| 361 |
% |
| 362 |
% \Finale |
| 363 |
\endinput |