summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/pgfplots/pgfplotshelpers.sty
blob: 203f16379ee8f65e63a992fc8d34a06827906acc (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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Low-level helper methods for string parsing and number conversion.
%
% This file comes as part of the pgfplots-package.
%
% Copyright 2007/2008 by Christian Feuersänger.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{pgfplotshelpers}[2008/31/01 Version 0.9]

\def\prettyprintnumberprecision{2}

\def\pgfmathlog@tonumber#1pt{#1}

% Invokes command #1 with #2 without the 'pt' suffix.
\def\invokepgf@math@one#1#2{%
	%\edef\pgfmath@resulttemp{\pgf@sys@tonumber{#2}}%
	%\expandafter#1\expandafter{\pgfmath@resulttemp}%
	\edef\pgfmath@resulttemp{{\pgf@sys@tonumber{#2}}}%
	\expandafter#1\pgfmath@resulttemp%
}

% Expands #2 using \edef and invokes #1 with the resulting string.
%
% Example:
%   \pgfmath@y=7.9pt
%   \pgfmathlog@invoke@expanded\pgfmathexp@{{\pgf@sys@tonumber{\pgfmath@y}}}%
% will invoke
%   \pgfmathexp@{7.9}
\def\pgfmathlog@invoke@expanded#1#2{%
	\edef\pgfmath@resulttemp{#2}%
	\expandafter#1\pgfmath@resulttemp
}

% Expands the NUMBER (without units) to a pretty printed string argument: rounded to
% several digits, discard any suffix zeros etc.
%
% It uses \prettyprintnumberprecision digits after the comma.
\def\prettyprintnumber#1{%
	\begingroup
%\tracingmacros=2\tracingcommands=2
	\edef\argument{#1}%
	\expandafter\prettyprintnumber@impl\argument.\END
%\tracingmacros=0\tracingcommands=0
	\endgroup
}

\def\prettyprintnumber@impl#1.#2\END{%
	% This implementation is really bad.
	% I hope I find time to get something better...
	\def\tmp{#2}%
	\ifx\tmp\empty
		% there was no '.' in the original arg
		#1
	\else
		% ok, there is a dot in the arg.
		% 1. ROUND
		% 2. DISCARD ZEROS
		\toksdef\tokens=2
		\let\i=\c@pgf@counta
		\let\lastdigit=\c@pgf@countb
		\let\numberAsDIM=\pgfmath@x
		\let\zeroFilledNum=\pgfmath@y
		\i=0
		\tokens={}%
		\def\D{#2}%
		\def\DOT{.}%
		\ifx\D\DOT
			% the number '#1.'
			\let\D=\empty
		\else
			\expandafter\prettyprintnumber@impl@getfirst
				\prettyprintnumberprecision
				\of#2%
				\to\tokens%
				\nextto\D
				\count\i
		\fi
		%\message{Pretty print: first <= \prettyprintnumberprecision\ digits after period: \the\tokens, (strlen=\the\i), next rounding-relevant-digit: '\D'}%
		\ifx\D\empty
			\numberAsDIM=#1\expandafter.\the\tokens pt
			% Nothing to do - we have our desired precision.
		\else
			\lastdigit\expandafter=\D\relax
%		\message{D=\D!}%
			\ifnum\lastdigit>4
				\numberAsDIM=#1\expandafter.\expandafter\the\tokens\D pt
				% round up:
				% compute numberAsDIM += 0.0000[10-digit]
				\advance\lastdigit by-10
				\multiply\lastdigit by-1
				\tokens={0.}%
				\loop
				\ifnum\i>0
					\tokens=\expandafter{\the\tokens0}%
					\advance\i by-1
				\repeat
				\edef\D{\the\tokens\the\lastdigit pt}%
				\expandafter\zeroFilledNum\D
				\ifdim\numberAsDIM<0pt
					\advance\numberAsDIM by-\zeroFilledNum
				\else
					\advance\numberAsDIM by\zeroFilledNum
				\fi
			\else
				\numberAsDIM=#1\expandafter.\the\tokens pt
			\fi
		\fi
		%\message{Pretty print: Result after rounding = \the\numberAsDIM / zeroFilledNum = \the\zeroFilledNum [only defined on up-rounding}%
		\edef\D{\pgf@sys@tonumber{\numberAsDIM}}%
		\expandafter\prettyprintnumber@onlyrelevant@digits\D\to\D
		% FIXME : I could return '\D' as macro!? May thats a better
		% way...
		\D
	\fi
}

% This macro takes the first \prettyprintnumberprecision digits after
% the period and squeezes away any trailing zeros.
%
% If no digits after the period remain, the period will be discarded
% as well.
\def\prettyprintnumber@onlyrelevant@digits#1.#2\to#3{%
	\begingroup
	\toksdef\tokens=2
	\let\i=\c@pgf@counta
	\let\maxDigits=\c@pgf@countc
	\let\numsuffixZeros=\c@pgf@countb
	\tokens={#1.}%
	\i=0
	\expandafter\maxDigits\prettyprintnumberprecision
	\numsuffixZeros=0
	\prettyprintnumber@onlyrelevant@digits@leftshift#2\to#3
	\ifnum\numsuffixZeros>0
		\ifnum\numsuffixZeros=\i
			\tokens={#1}%
		\else
			\maxDigits=\i
			\advance\maxDigits by-\numsuffixZeros
			\tokens={#1.}%
			\i=0
			\prettyprintnumber@onlyrelevant@digits@leftshift#2\to#3
		\fi
	\fi
	\xdef\pgfplotshelpersTMP{\the\tokens}%
	\endgroup
	\let#3=\pgfplotshelpersTMP
}


% shifts the input argument until either no more input is left or
% \maxDigits entries have been acquired.
%
% It also updates \i (will contain the number of acquired elements)
% and \numsuffixZeros (the number of zeros at the tail of #3 on
% output).
\def\prettyprintnumber@onlyrelevant@digits@leftshift#1#2\to#3{%
	\ifnum\i<\maxDigits
		\tokens=\expandafter{\the\tokens#1}%
		\advance\i by1
		\ifnum#1=0
			\advance\numsuffixZeros by1
		\else
			\numsuffixZeros=0
		\fi
		\def\tmp{#2}%
		\ifx\tmp\empty
		\else
			\prettyprintnumber@onlyrelevant@digits@leftshift#2\to#3
		\fi
	\fi
}

\def\prettyprintnumber@impl@getfirst#1\of#2#3.\to#4\nextto#5\count#6{%
	\edef#5{#3}%
	#4=\expandafter{\the#4#2}%
	\advance#6 by1
	\ifx#5\empty
	\else
		\ifnum#6<#1
			\prettyprintnumber@impl@getfirst #1\of#3.\to#4\nextto#5\count{#6}%
		\else
			\prettyprintnumber@impl@firstof#3\to#5
		\fi
	\fi
}

\def\prettyprintnumber@impl@firstof#1#2\to#3{\def#3{#1}}%