summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/ltxmisc/sepnum.sty
blob: 5d81c61680e6a66e962a99a693e19b85dc25ffed (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
%% This is file `sepnum.sty' -- separated numbers
%%
%% (C) 1995, 1996, 2001 Bernd Raichle
%%                      Raichle@Informatik.Uni-Stuttgart.de
%%
%% This package provides a means of formatting numbers
%% with (a) a decimal separator different of the default (`.')
%% and (b) a separator (default `,') every three digits.
%%
%% First version of this package was posted to the newsgroup
%% `de.comp.tex' November 6th, 1995 (as `printnum.sty').
%%
%%
%% -- LICENSING CONDITION --
%%
%% This program may be redistributed and/or modified under the terms
%% of the LaTeX Project Public License, either version 1.0 of this
%% license, or (at your option) any later version.
%% The latest version of this license is in
%%   CTAN:macros/latex/base/lppl.txt.
%%
%%
%% -- USAGE --
%%
%% The macro
%%
%%    \sepnum{<c>}{<sbc>}{<sac>}{<number>}
%%
%% produces the <number> with the decimal point replaced by <c>
%% and the separator <sbc> every three digits in the integer part,
%% the separator <sac> every three digits in the fractional part
%% of <number>.  \sepnum is _fully expandable_ and _not_ fragile,
%% unless one of <c>, <sbc>, or <sac> is.
%%
%% Example:
%%   - a non realistic example to show the capabilities of \sepnum
%%     \sepnum{,}{*}{-}{1234.5678} => 1*234,567-8
%%   - to print a number inserting small spaces each three digits
%%     \sepnum{.}{\,}{\,}{1234.5678} => 1\,234.567\,8
%%
%%
%% Additionally this package provides
%%
%%   \sepnumform{<c>}{<sbc>}{<sac>}{<LaTeX-counter>}
%%
%% to use \sepnum with a LaTeX counter.
%%
%% Example (with a decimal comma in the print form \the<counter>):
%%   \newcounter{money}
%%   \renewcommand{\themoney}{\sepnumform{,}{.}{}{money}}
%%
%%   \setcounter{money}{-1000}
%%   You have \themoney{} left.
%%
%%
%% (For compatibility with `printnum.sty', the macros
%% \printnum{<number>} and \printnumKomma/\printnumTrenner
%% still exist.)
%%
%%
%% -- SEE ALSO --
%%
%% * Package ``comma.sty'' by David Carlisle which provides
%%   similar features for integers only, i.e. it misses support
%%   for the decimal point/comma in fixed point numbers.
%% * Package ``numprint.sty'' by Harald Harders supporting
%%   formatting of numbers given as 1234e5 in addition.
%%   
%%
\begingroup \expandafter\expandafter\expandafter\endgroup
\expandafter\ifx \csname ProvidesPackage\endcsname\relax \else
  \ProvidesPackage{sepnum}%
      [1996/09/05 v2.0 Insert a separator every three digits (br)]
\fi


%
% auxiliary macros for loops:
%
\newcommand{\sepnumReturnFi}{}
\newcommand{\sepnumReturnElseFi}{}
\newcommand{\sepnumReturnOrFi}{}

\def\sepnumReturnFi#1\fi{\fi #1}
\def\sepnumReturnElseFi#1\else#2\fi{\fi #1}
\def\sepnumReturnOrFi#1\or#2\fi{\fi #1}

%
% \sepnum@DoNothing #1
%    don't change #1 (removes braces around argument token list!)
%
\newcommand{\sepnum@DoNothing}[1]{#1}

%
% \sepnum@ReverseNumber #1 #2
%    reverses all tokens in #2 and calls #1{#2} afterwards
%
\newcommand{\sepnum@ReverseNumber}[2]{%
  \sepnum@DoReverseNumber{#1}{}#2\relax}
\newcommand{\sepnum@DoReverseNumber}[3]{%
  \ifx\relax#3\sepnumReturnElseFi{#1{#2}}%
  \else\sepnumReturnFi{\sepnum@DoReverseNumber{#1}{#3#2}}%
  \fi}

%
% \sepnum@ThreeDigits #1 #2 #3
%    reverses #3, inserts #1 every three tokens/digits
%    (you have to reverse #3 again using \sepnum@ReverseNumber!),
%    calls #2{#3} afterwards
%
\newcommand{\sepnum@ThreeDigits}[3]{%
  \sepnum@DoThreeDigits{#1}{#2}{}0#3\relax}
\newcommand{\sepnum@DoThreeDigits}[5]{%
  \ifx\relax#5\sepnumReturnElseFi{#2{#3}}%
  \else\sepnumReturnFi{%
    \ifcase#4\noexpand\sepnum@DoNothing{}% 0
      \sepnumReturnOrFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}1}%
    \or   % 1
      \sepnumReturnOrFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}2}%
    \or   % 2
      \sepnumReturnElseFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}3}%
    \else % 3  ==> insert #1
      \sepnumReturnFi{\sepnum@DoThreeDigits{#1}{#2}{#5#1#3}1}%
    \fi}%
  \fi}

%
% \sepnum@BeforeComma #1 #2
% \sepnum@AfterComma #1 #2
%    call \sepnum@ReverseNumber and \sepnum@DoThreeDigits
%    to correctly group the number #2 every three digits
%    either with or without reversing first or afterwards.
%
\newcommand{\sepnum@BeforeComma}[2]{%
  \sepnum@ReverseNumber{\sepnum@ThreeDigits{#1}\sepnum@DoNothing}{#2}}
\newcommand{\sepnum@AfterComma}[2]{%
  \sepnum@ThreeDigits{#1}{\sepnum@ReverseNumber\sepnum@DoNothing}{#2}}

%
%%%%%%%%%% USER MACRO %%%%%%%%%%
%
% \sepnum #1 #2 #3 #4
%
%    #1 decimal comma
%    #2 separator for three digits groups before comma
%    #3 separator for three digits groups after comma
%    #4 number
%
\newcommand{\sepnum}[4]{%
  \sepnum@Sign{#1}{#2}{#3}#4.\relax}
%
%    look first for sign `+' or `-', divide given number in
%    digits before and after `.', insert #1 as decimal comma
%    (in \sepnum@SepComma)
%
\newcommand{\sepnum@Sign}[4]{%
  \ifx\relax#4%
    0%
  \else \sepnumReturnFi{%
    \ifx -#4%
      \sepnumReturnElseFi{-\sepnum@SepComma{#1}{#2}{#3}}%
    \else \sepnumReturnFi{%
      \ifx +#4%
        \sepnumReturnElseFi{+\sepnum@SepComma{#1}{#2}{#3}}%
      \else
        \sepnumReturnFi{\sepnum@SepComma{#1}{#2}{#3}#4}%
      \fi}%
    \fi}%
  \fi}

\newcommand{\sepnum@SepComma}{}
\def\sepnum@SepComma#1#2#3#4.#5\relax{%
  \ifx$#4$0% no digits before `.' => add 0 for "0.xxx"
  \else \sepnumReturnFi{\sepnum@BeforeComma{#2}{#4}}\fi
  \ifx$#5$% no digits after `.'
  \else \sepnumReturnFi{#1\sepnum@DoSepComma{#3}#5\relax}\fi}

% remove `.' inserted in \sepnum
\def\sepnum@DoSepComma#1#2.#3\relax{%
  \sepnum@AfterComma{#1}{#2}}


%
%%%%%%%%%% USER MACRO %%%%%%%%%%
%
% \sepnumform #1 #2 #3 #4
%
%    like \sepnum but #4 is the name of a LaTeX counter
%
\newcommand{\sepnumform}[4]{%
  \expandafter\sepnum@doform
  \expandafter{\number\value{#4}}{#1}{#2}{#3}}
\newcommand{\sepnum@doform}[4]{%
  \sepnum{#2}{#3}{#4}{#1}}


%
%%%%%%%%%% USER MACRO %%%%%%%%%%
%
% For compatibility with `printnum.sty':
%
% \printnum #1
%
\newcommand{\printnum}[1]{%
  \sepnum{\printnumKomma}{\printnumTrenner}{\printnumTrenner}{#1}}
%
% Macros to specify comma and separator (default to get
% european style numbers, e.g. "123.456,78" for "123456.78"):
%
%   \printnumKomma    => ,
%   \printnumTrenner  => .
%
\newcommand{\printnumKomma}{,}
\newcommand{\printnumTrenner}{.}

\endinput
%%
%% End of file `sepnum.sty'.