Debian Codegraph @

texmf-dist/tex/latex/sepnum/sepnum.sty

raw
1 %% This is file `sepnum.sty' -- separated numbers
2 %%
3 %% (C) 1995, 1996, 2001 Bernd Raichle
4 %% [email protected]
5 %%
6 %% This package provides a means of formatting numbers
7 %% with (a) a decimal separator different of the default (`.')
8 %% and (b) a separator (default `,') every three digits.
9 %%
10 %% First version of this package was posted to the newsgroup
11 %% `de.comp.tex' November 6th, 1995 (as `printnum.sty').
12 %%
13 %%
14 %% -- LICENSING CONDITION --
15 %%
16 %% This program may be redistributed and/or modified under the terms
17 %% of the LaTeX Project Public License, either version 1.0 of this
18 %% license, or (at your option) any later version.
19 %% The latest version of this license is in
20 %% CTAN:macros/latex/base/lppl.txt.
21 %%
22 %%
23 %% -- USAGE --
24 %%
25 %% The macro
26 %%
27 %% \sepnum{<c>}{<sbc>}{<sac>}{<number>}
28 %%
29 %% produces the <number> with the decimal point replaced by <c>
30 %% and the separator <sbc> every three digits in the integer part,
31 %% the separator <sac> every three digits in the fractional part
32 %% of <number>. \sepnum is _fully expandable_ and _not_ fragile,
33 %% unless one of <c>, <sbc>, or <sac> is.
34 %%
35 %% Example:
36 %% - a non realistic example to show the capabilities of \sepnum
37 %% \sepnum{,}{*}{-}{1234.5678} => 1*234,567-8
38 %% - to print a number inserting small spaces each three digits
39 %% \sepnum{.}{\,}{\,}{1234.5678} => 1\,234.567\,8
40 %%
41 %%
42 %% Additionally this package provides
43 %%
44 %% \sepnumform{<c>}{<sbc>}{<sac>}{<LaTeX-counter>}
45 %%
46 %% to use \sepnum with a LaTeX counter.
47 %%
48 %% Example (with a decimal comma in the print form \the<counter>):
49 %% \newcounter{money}
50 %% \renewcommand{\themoney}{\sepnumform{,}{.}{}{money}}
51 %%
52 %% \setcounter{money}{-1000}
53 %% You have \themoney{} left.
54 %%
55 %%
56 %% (For compatibility with `printnum.sty', the macros
57 %% \printnum{<number>} and \printnumKomma/\printnumTrenner
58 %% still exist.)
59 %%
60 %%
61 %% -- SEE ALSO --
62 %%
63 %% * Package ``comma.sty'' by David Carlisle which provides
64 %% similar features for integers only, i.e. it misses support
65 %% for the decimal point/comma in fixed point numbers.
66 %% * Package ``numprint.sty'' by Harald Harders supporting
67 %% formatting of numbers given as 1234e5 in addition.
68 %%
69 %%
70 \begingroup \expandafter\expandafter\expandafter\endgroup
71 \expandafter\ifx \csname ProvidesPackage\endcsname\relax \else
72 \ProvidesPackage{sepnum}%
73 [1996/09/05 v2.0 Insert a separator every three digits (br)]
74 \fi
75
76
77 %
78 % auxiliary macros for loops:
79 %
80 \newcommand{\sepnumReturnFi}{}
81 \newcommand{\sepnumReturnElseFi}{}
82 \newcommand{\sepnumReturnOrFi}{}
83
84 \def\sepnumReturnFi#1\fi{\fi #1}
85 \def\sepnumReturnElseFi#1\else#2\fi{\fi #1}
86 \def\sepnumReturnOrFi#1\or#2\fi{\fi #1}
87
88 %
89 % \sepnum@DoNothing #1
90 % don't change #1 (removes braces around argument token list!)
91 %
92 \newcommand{\sepnum@DoNothing}[1]{#1}
93
94 %
95 % \sepnum@ReverseNumber #1 #2
96 % reverses all tokens in #2 and calls #1{#2} afterwards
97 %
98 \newcommand{\sepnum@ReverseNumber}[2]{%
99 \sepnum@DoReverseNumber{#1}{}#2\relax}
100 \newcommand{\sepnum@DoReverseNumber}[3]{%
101 \ifx\relax#3\sepnumReturnElseFi{#1{#2}}%
102 \else\sepnumReturnFi{\sepnum@DoReverseNumber{#1}{#3#2}}%
103 \fi}
104
105 %
106 % \sepnum@ThreeDigits #1 #2 #3
107 % reverses #3, inserts #1 every three tokens/digits
108 % (you have to reverse #3 again using \sepnum@ReverseNumber!),
109 % calls #2{#3} afterwards
110 %
111 \newcommand{\sepnum@ThreeDigits}[3]{%
112 \sepnum@DoThreeDigits{#1}{#2}{}0#3\relax}
113 \newcommand{\sepnum@DoThreeDigits}[5]{%
114 \ifx\relax#5\sepnumReturnElseFi{#2{#3}}%
115 \else\sepnumReturnFi{%
116 \ifcase#4\noexpand\sepnum@DoNothing{}% 0
117 \sepnumReturnOrFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}1}%
118 \or % 1
119 \sepnumReturnOrFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}2}%
120 \or % 2
121 \sepnumReturnElseFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}3}%
122 \else % 3 ==> insert #1
123 \sepnumReturnFi{\sepnum@DoThreeDigits{#1}{#2}{#5#1#3}1}%
124 \fi}%
125 \fi}
126
127 %
128 % \sepnum@BeforeComma #1 #2
129 % \sepnum@AfterComma #1 #2
130 % call \sepnum@ReverseNumber and \sepnum@DoThreeDigits
131 % to correctly group the number #2 every three digits
132 % either with or without reversing first or afterwards.
133 %
134 \newcommand{\sepnum@BeforeComma}[2]{%
135 \sepnum@ReverseNumber{\sepnum@ThreeDigits{#1}\sepnum@DoNothing}{#2}}
136 \newcommand{\sepnum@AfterComma}[2]{%
137 \sepnum@ThreeDigits{#1}{\sepnum@ReverseNumber\sepnum@DoNothing}{#2}}
138
139 %
140 %%%%%%%%%% USER MACRO %%%%%%%%%%
141 %
142 % \sepnum #1 #2 #3 #4
143 %
144 % #1 decimal comma
145 % #2 separator for three digits groups before comma
146 % #3 separator for three digits groups after comma
147 % #4 number
148 %
149 \newcommand{\sepnum}[4]{%
150 \sepnum@Sign{#1}{#2}{#3}#4.\relax}
151 %
152 % look first for sign `+' or `-', divide given number in
153 % digits before and after `.', insert #1 as decimal comma
154 % (in \sepnum@SepComma)
155 %
156 \newcommand{\sepnum@Sign}[4]{%
157 \ifx\relax#4%
158 0%
159 \else \sepnumReturnFi{%
160 \ifx -#4%
161 \sepnumReturnElseFi{-\sepnum@SepComma{#1}{#2}{#3}}%
162 \else \sepnumReturnFi{%
163 \ifx +#4%
164 \sepnumReturnElseFi{+\sepnum@SepComma{#1}{#2}{#3}}%
165 \else
166 \sepnumReturnFi{\sepnum@SepComma{#1}{#2}{#3}#4}%
167 \fi}%
168 \fi}%
169 \fi}
170
171 \newcommand{\sepnum@SepComma}{}
172 \def\sepnum@SepComma#1#2#3#4.#5\relax{%
173 \ifx$#4$0% no digits before `.' => add 0 for "0.xxx"
174 \else \sepnumReturnFi{\sepnum@BeforeComma{#2}{#4}}\fi
175 \ifx$#5$% no digits after `.'
176 \else \sepnumReturnFi{#1\sepnum@DoSepComma{#3}#5\relax}\fi}
177
178 % remove `.' inserted in \sepnum
179 \def\sepnum@DoSepComma#1#2.#3\relax{%
180 \sepnum@AfterComma{#1}{#2}}
181
182
183 %
184 %%%%%%%%%% USER MACRO %%%%%%%%%%
185 %
186 % \sepnumform #1 #2 #3 #4
187 %
188 % like \sepnum but #4 is the name of a LaTeX counter
189 %
190 \newcommand{\sepnumform}[4]{%
191 \expandafter\sepnum@doform
192 \expandafter{\number\value{#4}}{#1}{#2}{#3}}
193 \newcommand{\sepnum@doform}[4]{%
194 \sepnum{#2}{#3}{#4}{#1}}
195
196
197 %
198 %%%%%%%%%% USER MACRO %%%%%%%%%%
199 %
200 % For compatibility with `printnum.sty':
201 %
202 % \printnum #1
203 %
204 \newcommand{\printnum}[1]{%
205 \sepnum{\printnumKomma}{\printnumTrenner}{\printnumTrenner}{#1}}
206 %
207 % Macros to specify comma and separator (default to get
208 % european style numbers, e.g. "123.456,78" for "123456.78"):
209 %
210 % \printnumKomma => ,
211 % \printnumTrenner => .
212 %
213 \newcommand{\printnumKomma}{,}
214 \newcommand{\printnumTrenner}{.}
215
216 \endinput
217 %%
218 %% End of file `sepnum.sty'.