| 1 |
|
| 2 |
\section{Number Printing} |
| 3 |
\label{pgfmath-numberprinting} |
| 4 |
|
| 5 |
{\emph{An extension by Christian Feuersänger}} |
| 6 |
|
| 7 |
\medskip |
| 8 |
\noindent |
| 9 |
\pgfname\ supports number printing in different styles and rounds to arbitrary |
| 10 |
precision. |
| 11 |
|
| 12 |
\begin{command}{\pgfmathprintnumber\marg{x}} |
| 13 |
Generates pretty-printed output for the (real) number \meta{x}. The input |
| 14 |
number \meta{x} is parsed using |\pgfmathfloatparsenumber| which allows |
| 15 |
arbitrary precision. |
| 16 |
|
| 17 |
Numbers are typeset in math mode using the current set of number printing |
| 18 |
options, see below. Optional arguments can also be provided using |
| 19 |
|\pgfmathprintnumber[|\meta{options}|]|\meta{x}. |
| 20 |
\end{command} |
| 21 |
|
| 22 |
\begin{command}{\pgfmathprintnumberto\marg{x}\marg{macro}} |
| 23 |
Returns the resulting number into \meta{macro} instead of typesetting it |
| 24 |
directly. |
| 25 |
\end{command} |
| 26 |
|
| 27 |
\begin{key}{/pgf/number format/fixed} |
| 28 |
Configures |\pgfmathprintnumber| to round the number to a fixed number of |
| 29 |
digits after the period, discarding any trailing zeros. |
| 30 |
% |
| 31 |
\begin{codeexample}[] |
| 32 |
\pgfkeys{/pgf/number format/.cd,fixed,precision=2} |
| 33 |
\pgfmathprintnumber{4.568}\hspace{1em} |
| 34 |
\pgfmathprintnumber{5e-04}\hspace{1em} |
| 35 |
\pgfmathprintnumber{0.1}\hspace{1em} |
| 36 |
\pgfmathprintnumber{24415.98123}\hspace{1em} |
| 37 |
\pgfmathprintnumber{123456.12345} |
| 38 |
\end{codeexample} |
| 39 |
|
| 40 |
See section~\ref{sec:number:styles} for how to change the appearance. |
| 41 |
\end{key} |
| 42 |
|
| 43 |
\begin{key}{/pgf/number format/fixed zerofill=\marg{boolean} (default true)} |
| 44 |
Enables or disables zero filling for any number drawn in fixed point |
| 45 |
format. |
| 46 |
% |
| 47 |
\begin{codeexample}[] |
| 48 |
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=2} |
| 49 |
\pgfmathprintnumber{4.568}\hspace{1em} |
| 50 |
\pgfmathprintnumber{5e-04}\hspace{1em} |
| 51 |
\pgfmathprintnumber{0.1}\hspace{1em} |
| 52 |
\pgfmathprintnumber{24415.98123}\hspace{1em} |
| 53 |
\pgfmathprintnumber{123456.12345} |
| 54 |
\end{codeexample} |
| 55 |
% |
| 56 |
This key affects numbers drawn with |fixed| or |std| styles (the latter |
| 57 |
only if no scientific format is chosen). |
| 58 |
% |
| 59 |
\begin{codeexample}[] |
| 60 |
\pgfkeys{/pgf/number format/.cd,std,fixed zerofill,precision=2} |
| 61 |
\pgfmathprintnumber{4.568}\hspace{1em} |
| 62 |
\pgfmathprintnumber{5e-05}\hspace{1em} |
| 63 |
\pgfmathprintnumber{1}\hspace{1em} |
| 64 |
\pgfmathprintnumber{123456.12345} |
| 65 |
\end{codeexample} |
| 66 |
|
| 67 |
See section~\ref{sec:number:styles} for how to change the appearance. |
| 68 |
\end{key} |
| 69 |
|
| 70 |
\begin{key}{/pgf/number format/sci} |
| 71 |
Configures |\pgfmathprintnumber| to display numbers in scientific format, |
| 72 |
that means sign, mantissa and exponent (basis~$10$). The mantissa is |
| 73 |
rounded to the desired |precision| (or |sci precision|, see below). |
| 74 |
% |
| 75 |
\begin{codeexample}[] |
| 76 |
\pgfkeys{/pgf/number format/.cd,sci,precision=2} |
| 77 |
\pgfmathprintnumber{4.568}\hspace{1em} |
| 78 |
\pgfmathprintnumber{5e-04}\hspace{1em} |
| 79 |
\pgfmathprintnumber{0.1}\hspace{1em} |
| 80 |
\pgfmathprintnumber{24415.98123}\hspace{1em} |
| 81 |
\pgfmathprintnumber{123456.12345} |
| 82 |
\end{codeexample} |
| 83 |
|
| 84 |
See section~\ref{sec:number:styles} for how to change the exponential |
| 85 |
display style. |
| 86 |
\end{key} |
| 87 |
|
| 88 |
\begin{key}{/pgf/number format/sci zerofill=\marg{boolean} (default true)} |
| 89 |
Enables or disables zero filling for any number drawn in scientific format. |
| 90 |
% |
| 91 |
\begin{codeexample}[] |
| 92 |
\pgfkeys{/pgf/number format/.cd,sci,sci zerofill,precision=2} |
| 93 |
\pgfmathprintnumber{4.568}\hspace{1em} |
| 94 |
\pgfmathprintnumber{5e-04}\hspace{1em} |
| 95 |
\pgfmathprintnumber{0.1}\hspace{1em} |
| 96 |
\pgfmathprintnumber{24415.98123}\hspace{1em} |
| 97 |
\pgfmathprintnumber{123456.12345} |
| 98 |
\end{codeexample} |
| 99 |
% |
| 100 |
As with |fixed zerofill|, this option does only affect numbers drawn in |
| 101 |
|sci| format (or |std| if the scientific format is chosen). |
| 102 |
|
| 103 |
See section~\ref{sec:number:styles} for how to change the exponential |
| 104 |
display style. |
| 105 |
\end{key} |
| 106 |
|
| 107 |
\begin{stylekey}{/pgf/number format/zerofill=\marg{boolean} (default true)} |
| 108 |
Sets both |fixed zerofill| and |sci zerofill| at once. |
| 109 |
\end{stylekey} |
| 110 |
|
| 111 |
\begin{keylist}{/pgf/number format/std,% |
| 112 |
/pgf/number format/std=\meta{lower e}, |
| 113 |
/pgf/number format/std=\meta{lower e}:\meta{upper e}% |
| 114 |
} |
| 115 |
Configures |\pgfmathprintnumber| to a standard algorithm. It chooses either |
| 116 |
|fixed| or |sci|, depending on the order of magnitude. Let $n=s \cdot m |
| 117 |
\cdot 10^e$ be the input number and $p$ the current precision. If $-p/2 \le |
| 118 |
e \le 4$, the number is displayed using |fixed| format. Otherwise, it is |
| 119 |
displayed using |sci| format. |
| 120 |
% |
| 121 |
\begin{codeexample}[] |
| 122 |
\pgfkeys{/pgf/number format/.cd,std,precision=2} |
| 123 |
\pgfmathprintnumber{4.568}\hspace{1em} |
| 124 |
\pgfmathprintnumber{5e-04}\hspace{1em} |
| 125 |
\pgfmathprintnumber{0.1}\hspace{1em} |
| 126 |
\pgfmathprintnumber{24415.98123}\hspace{1em} |
| 127 |
\pgfmathprintnumber{123456.12345} |
| 128 |
\end{codeexample} |
| 129 |
% |
| 130 |
The parameters can be customized using the optional integer argument(s): if |
| 131 |
$\text{\meta{lower e}} \le e \le \text{\meta{upper e}}$, the number is |
| 132 |
displayed in |fixed| format, otherwise in |sci| format. Note that |
| 133 |
\meta{lower e} should be negative for useful results. The precision used |
| 134 |
for the scientific format can be adjusted with |sci precision| if |
| 135 |
necessary. |
| 136 |
\end{keylist} |
| 137 |
|
| 138 |
\begin{keylist}{/pgf/number format/relative*=\meta{exponent base 10}} |
| 139 |
Configures |\pgfmathprintnumber| to format numbers relative to an order of |
| 140 |
magnitude, $10^r$, where $r$ is an integer number. |
| 141 |
|
| 142 |
This key addresses different use-cases. |
| 143 |
|
| 144 |
\paragraph{First use-case:} |
| 145 |
|
| 146 |
provide a unified format for a \emph{sequence} of numbers. Consider the |
| 147 |
following test: |
| 148 |
% |
| 149 |
\begin{codeexample}[] |
| 150 |
\pgfkeys{/pgf/number format/relative*={1}} |
| 151 |
\pgfmathprintnumber{6.42e-16}\hspace{1em} |
| 152 |
\pgfmathprintnumber{1.2}\hspace{1em} |
| 153 |
\pgfmathprintnumber{6}\hspace{1em} |
| 154 |
\pgfmathprintnumber{20.6}\hspace{1em} |
| 155 |
\pgfmathprintnumber{87} |
| 156 |
\end{codeexample} |
| 157 |
% |
| 158 |
\noindent With any other style, the |6.42e-16| would have been formatted as |
| 159 |
an isolated number. Here, it is rounded to |0| because when viewed relative |
| 160 |
to $10^1$ (the exponent $1$ is the argument for |relative|), it has no |
| 161 |
significant digits. |
| 162 |
% |
| 163 |
\begin{codeexample}[] |
| 164 |
\pgfkeys{/pgf/number format/relative*={2}} |
| 165 |
\pgfmathprintnumber{123.345}\hspace{1em} |
| 166 |
\pgfmathprintnumber{0.0012}\hspace{1em} |
| 167 |
\pgfmathprintnumber{0.0014}\hspace{1em} |
| 168 |
\end{codeexample} |
| 169 |
% |
| 170 |
\noindent The example above applies the initial |precision=2| to |123.345| |
| 171 |
-- relative to $100$. Two significant digits of |123.345| relative to $100$ |
| 172 |
are |123|. Note that the ``$2$ significant digits of |123.345|'' translates |
| 173 |
to ``round |1.2345| to $2$ digits'', which would yield |1.2300|. Similarly, |
| 174 |
the other two numbers are |0| compared to $100$ using the given |
| 175 |
|precision|. |
| 176 |
% |
| 177 |
\begin{codeexample}[] |
| 178 |
\pgfkeys{/pgf/number format/relative*={-3}} |
| 179 |
\pgfmathprintnumber{123.345}\hspace{1em} |
| 180 |
\pgfmathprintnumber{0.0012}\hspace{1em} |
| 181 |
\pgfmathprintnumber{0.0014}\hspace{1em} |
| 182 |
\end{codeexample} |
| 183 |
|
| 184 |
\paragraph{Second use-case:} |
| 185 |
|
| 186 |
improve rounding in the presence of \emph{inaccurate} numbers. Let us |
| 187 |
suppose that some limited-precision arithmetic resulted in the result |
| 188 |
|123456999| (like the |fpu| of \pgfname). You know that its precision is |
| 189 |
about five or six significant digits. And you want to provide a fixed point |
| 190 |
output. In this case, the trailing digits |....999| are a numerical |
| 191 |
artifact due to the limited precision. Use |relative*=3,precision=0| to |
| 192 |
eliminate the artifacts: |
| 193 |
% |
| 194 |
\begin{codeexample}[] |
| 195 |
\pgfkeys{/pgf/number format/.cd,relative*={3},precision=0} |
| 196 |
\pgfmathprintnumber{123456999}\hspace{1em} |
| 197 |
\pgfmathprintnumber{123456999.12} |
| 198 |
\end{codeexample} |
| 199 |
% |
| 200 |
\noindent Here, |precision=0| means that we inspect |123456.999| and round |
| 201 |
that number to $0$ digits. Finally, we move the period back to its initial |
| 202 |
position. Adding |relative style=fixed| results in fixed point output |
| 203 |
format: |
| 204 |
% |
| 205 |
\begin{codeexample}[] |
| 206 |
\pgfkeys{/pgf/number format/.cd,relative*={3},precision=0,relative style=fixed} |
| 207 |
\pgfmathprintnumber{123456999}\hspace{1em} |
| 208 |
\pgfmathprintnumber{123456999.12} |
| 209 |
\end{codeexample} |
| 210 |
% |
| 211 |
\noindent Note that there is another alternative for this use-case which is |
| 212 |
discussed later: the |fixed relative| style. |
| 213 |
% |
| 214 |
\begin{codeexample}[] |
| 215 |
\pgfkeys{/pgf/number format/.cd,fixed relative,precision=6} |
| 216 |
\pgfmathprintnumber{123456999}\hspace{1em} |
| 217 |
\pgfmathprintnumber{123456999.12} |
| 218 |
\end{codeexample} |
| 219 |
|
| 220 |
You might wonder why there is an asterisk in the key's name. The short |
| 221 |
answer is: there is also a \declareandlabel{/pgf/number format/relative} |
| 222 |
number printer which does unexpected things. The key |relative*| repairs |
| 223 |
this. Existing code will still use the old behavior. |
| 224 |
|
| 225 |
Technically, the key works as follows: as already explained above, |
| 226 |
|relative*=3| key applied to |123456999.12| moves the period by three |
| 227 |
positions and analyzes |123456.99912|. Mathematically speaking, we are |
| 228 |
given a number $x = \pm m \cdot 10^e$ and we attempt to apply |
| 229 |
|relative*=|$r$. The method then rounds $x / 10^r$ to |precision| digits. |
| 230 |
Afterwards, it multiplies the result by $10^r$ and typesets it. |
| 231 |
\end{keylist} |
| 232 |
|
| 233 |
\begin{stylekey}{/pgf/number format/every relative} |
| 234 |
A style which configures how the |relative| method finally displays its |
| 235 |
results. |
| 236 |
|
| 237 |
The initial configuration is |
| 238 |
% |
| 239 |
\begin{codeexample}[code only] |
| 240 |
\pgfkeys{/pgf/number format/every relative/.style=std} |
| 241 |
\end{codeexample} |
| 242 |
|
| 243 |
Note that rounding is turned off when the resulting style is being |
| 244 |
evaluated (since |relative| already rounded the number). |
| 245 |
|
| 246 |
Although supported, I discourage from using |fixed zerofill| or |
| 247 |
|sci zerofill| in this context -- it may lead to a suggestion of higher |
| 248 |
precision than is actually used (because |fixed zerofill| might simply add |
| 249 |
|.00| although there was a different information before |relative| rounded |
| 250 |
the result). |
| 251 |
\end{stylekey} |
| 252 |
|
| 253 |
\begin{key}{/pgf/number format/relative style=\marg{options}} |
| 254 |
The same as |every relative/.append style=|\marg{options}. |
| 255 |
\end{key} |
| 256 |
|
| 257 |
\begin{keylist}{/pgf/number format/fixed relative} |
| 258 |
Configures |\pgfmathprintnumber| to format numbers in a similar way to the |
| 259 |
|fixed| style, but the |precision| is interpreted relatively to the |
| 260 |
number's exponent. |
| 261 |
|
| 262 |
The motivation is to get the same rounding effect as for |sci|, but to |
| 263 |
display the number in the |fixed| style: |
| 264 |
% |
| 265 |
\begin{codeexample}[] |
| 266 |
\pgfkeys{/pgf/number format/.cd,fixed relative,precision=3} |
| 267 |
\pgfmathprintnumber{1000.0123}\hspace{1em} |
| 268 |
\pgfmathprintnumber{100.0567}\hspace{1em} |
| 269 |
\pgfmathprintnumber{0.000010003452}\hspace{1em} |
| 270 |
\pgfmathprintnumber{0.010073452}\hspace{1em} |
| 271 |
\pgfmathprintnumber{1.23567}\hspace{1em} |
| 272 |
\pgfmathprintnumber{1003.75}\hspace{1em} |
| 273 |
\pgfmathprintnumber{1006.75}\hspace{1em} |
| 274 |
\end{codeexample} |
| 275 |
|
| 276 |
The effect of |fixed relative| is that the number is rounded to |
| 277 |
\emph{exactly} the first \meta{precision} non-zero digits, no matter how |
| 278 |
many leading zeros the number might have. |
| 279 |
|
| 280 |
Use |fixed relative| if you want |fixed| and if you know that only the |
| 281 |
first $n$ digits are correct. Use |sci| if you need a scientific display |
| 282 |
style and only the first $n$ digits are correct. |
| 283 |
|
| 284 |
Note that |fixed relative| ignores the |fixed zerofill| flag. |
| 285 |
|
| 286 |
See also the |relative*| key. Note that the |relative=|\marg{exponent} key |
| 287 |
explicitly moves the period to some designated position before it attempts |
| 288 |
to round the number. Afterwards, it ``rounds from the right'', i.e.\ it |
| 289 |
rounds to that explicitly chosen digit position. In contrast to that, |
| 290 |
|fixed relative| ``rounds from the left'': it takes the \emph{first} |
| 291 |
non-zero digit, temporarily places the period after this digit, and rounds |
| 292 |
that number. The rounding style |fixed| leaves the period where it is, and |
| 293 |
rounds everything behind that digit. The |sci| style is similar to |
| 294 |
|fixed relative|. |
| 295 |
\end{keylist} |
| 296 |
|
| 297 |
\begin{key}{/pgf/number format/int detect} |
| 298 |
Configures |\pgfmathprintnumber| to detect integers automatically. If the |
| 299 |
input number is an integer, no period is displayed at all. If not, the |
| 300 |
scientific format is chosen. |
| 301 |
% |
| 302 |
\begin{codeexample}[] |
| 303 |
\pgfkeys{/pgf/number format/.cd,int detect,precision=2} |
| 304 |
\pgfmathprintnumber{15}\hspace{1em} |
| 305 |
\pgfmathprintnumber{20}\hspace{1em} |
| 306 |
\pgfmathprintnumber{20.4}\hspace{1em} |
| 307 |
\pgfmathprintnumber{0.01}\hspace{1em} |
| 308 |
\pgfmathprintnumber{0} |
| 309 |
\end{codeexample} |
| 310 |
% |
| 311 |
\end{key} |
| 312 |
|
| 313 |
\begin{command}{\pgfmathifisint\marg{number constant}\marg{true code}\marg{false code}} |
| 314 |
A command which does the same check as |int detect|, but it invokes |
| 315 |
\meta{true code} if the \meta{number constant} actually is an integer and |
| 316 |
the \meta{false code} if not. |
| 317 |
|
| 318 |
As a side-effect, |\pgfretval| will contain the parsed number, either in |
| 319 |
integer format or as parsed floating point number. |
| 320 |
|
| 321 |
The argument \meta{number constant} will be parsed with |
| 322 |
|\pgfmathfloatparsenumber|. |
| 323 |
% |
| 324 |
\begin{codeexample}[] |
| 325 |
15 \pgfmathifisint{15}{is an int: \pgfretval.}{is no int}\hspace{1em} |
| 326 |
15.5 \pgfmathifisint{15.5}{is an int: \pgfretval.}{is no int} |
| 327 |
\end{codeexample} |
| 328 |
% |
| 329 |
\end{command} |
| 330 |
|
| 331 |
\begin{key}{/pgf/number format/int trunc} |
| 332 |
Truncates every number to integers (discards any digit after the period). |
| 333 |
\begin{codeexample}[] |
| 334 |
\pgfkeys{/pgf/number format/.cd,int trunc} |
| 335 |
\pgfmathprintnumber{4.568}\hspace{1em} |
| 336 |
\pgfmathprintnumber{5e-04}\hspace{1em} |
| 337 |
\pgfmathprintnumber{0.1}\hspace{1em} |
| 338 |
\pgfmathprintnumber{24415.98123}\hspace{1em} |
| 339 |
\pgfmathprintnumber{123456.12345} |
| 340 |
\end{codeexample} |
| 341 |
% |
| 342 |
\end{key} |
| 343 |
|
| 344 |
\begin{key}{/pgf/number format/frac} |
| 345 |
Displays numbers as fractionals. |
| 346 |
% |
| 347 |
\begin{codeexample}[width=3cm,preamble={\usetikzlibrary{fpu}}] |
| 348 |
\pgfkeys{/pgf/number format/frac} |
| 349 |
\pgfmathprintnumber{0.333333333333333}\hspace{1em} |
| 350 |
\pgfmathprintnumber{0.5}\hspace{1em} |
| 351 |
\pgfmathprintnumber{2.133333333333325e-01}\hspace{1em} |
| 352 |
\pgfmathprintnumber{0.12}\hspace{1em} |
| 353 |
\pgfmathprintnumber{2.666666666666646e-02}\hspace{1em} |
| 354 |
\pgfmathprintnumber{-1.333333333333334e-02}\hspace{1em} |
| 355 |
\pgfmathprintnumber{7.200000000000000e-01}\hspace{1em} |
| 356 |
\pgfmathprintnumber{6.666666666666667e-02}\hspace{1em} |
| 357 |
\pgfmathprintnumber{1.333333333333333e-01}\hspace{1em} |
| 358 |
\pgfmathprintnumber{-1.333333333333333e-02}\hspace{1em} |
| 359 |
\pgfmathprintnumber{3.3333333}\hspace{1em} |
| 360 |
\pgfmathprintnumber{1.2345}\hspace{1em} |
| 361 |
\pgfmathprintnumber{1}\hspace{1em} |
| 362 |
\pgfmathprintnumber{-6} |
| 363 |
\end{codeexample} |
| 364 |
|
| 365 |
\begin{key}{/pgf/number format/frac TeX=\marg{\textbackslash macro} (initially \texttt{\textbackslash frac})} |
| 366 |
Allows to use a different implementation for |\frac| inside of the |
| 367 |
|frac| display type. |
| 368 |
\end{key} |
| 369 |
|
| 370 |
\begin{key}{/pgf/number format/frac denom=\meta{int} (initially empty)} |
| 371 |
Allows to provide a custom denominator for |frac|. |
| 372 |
% |
| 373 |
\begin{codeexample}[width=3cm,preamble={\usetikzlibrary{fpu}}] |
| 374 |
\pgfkeys{/pgf/number format/.cd,frac, frac denom=10} |
| 375 |
\pgfmathprintnumber{0.1}\hspace{1em} |
| 376 |
\pgfmathprintnumber{0.5}\hspace{1em} |
| 377 |
\pgfmathprintnumber{1.2}\hspace{1em} |
| 378 |
\pgfmathprintnumber{-0.6}\hspace{1em} |
| 379 |
\pgfmathprintnumber{-1.4}\hspace{1em} |
| 380 |
\end{codeexample} |
| 381 |
\end{key} |
| 382 |
% |
| 383 |
\begin{key}{/pgf/number format/frac whole=\mchoice{true,false} (initially true)} |
| 384 |
Configures whether complete integer parts shall be placed in front of |
| 385 |
the fractional part. In this case, the fractional part will be less |
| 386 |
then $1$. Use |frac whole=false| to avoid whole number parts. |
| 387 |
% |
| 388 |
\begin{codeexample}[width=3cm,preamble={\usetikzlibrary{fpu}}] |
| 389 |
\pgfkeys{/pgf/number format/.cd,frac, frac whole=false} |
| 390 |
\pgfmathprintnumber{20.1}\hspace{1em} |
| 391 |
\pgfmathprintnumber{5.5}\hspace{1em} |
| 392 |
\pgfmathprintnumber{1.2}\hspace{1em} |
| 393 |
\pgfmathprintnumber{-5.6}\hspace{1em} |
| 394 |
\pgfmathprintnumber{-1.4}\hspace{1em} |
| 395 |
\end{codeexample} |
| 396 |
\end{key} |
| 397 |
% |
| 398 |
\begin{key}{/pgf/number format/frac shift=\marg{integer} (initially 4)} |
| 399 |
In case you experience problems because of stability problems, try |
| 400 |
experimenting with a different |frac shift|. Higher shift values $k$ |
| 401 |
yield higher sensitivity to inaccurate data or inaccurate arithmetic. |
| 402 |
|
| 403 |
Technically, the following happens. If $r < 1$ is the fractional part |
| 404 |
of the mantissa, then a scale $i = 1/r \cdot 10^k$ is computed where |
| 405 |
$k$ is the shift; fractional parts of $i$ are neglected. The value |
| 406 |
$1/r$ is computed internally, its error is amplified. |
| 407 |
|
| 408 |
If you still experience stability problems, use |\usepackage{fp}| in |
| 409 |
your preamble. The |frac| style will then automatically employ the |
| 410 |
higher absolute precision of |fp| for the computation of $1/r$. |
| 411 |
\end{key} |
| 412 |
\end{key} |
| 413 |
|
| 414 |
\begin{key}{/pgf/number format/precision=\marg{number}} |
| 415 |
Sets the desired rounding precision for any display operation. For |
| 416 |
scientific format, this affects the mantissa. |
| 417 |
\end{key} |
| 418 |
|
| 419 |
\begin{key}{/pgf/number format/sci precision=\meta{number or empty} (initially empty)} |
| 420 |
Sets the desired rounding precision only for |sci| styles. |
| 421 |
|
| 422 |
Use |sci precision={}| to restore the initial configuration (which uses the |
| 423 |
argument provided to |precision| for all number styles). |
| 424 |
\end{key} |
| 425 |
|
| 426 |
\begin{key}{/pgf/number format/read comma as period=\mchoice{true,false} (initially false)} |
| 427 |
This is one of the few keys which allows to customize the number parser. If |
| 428 |
this switch is turned on, a comma is read just as a period. |
| 429 |
% |
| 430 |
\begin{codeexample}[] |
| 431 |
\pgfkeys{/pgf/number format/read comma as period} |
| 432 |
\pgfmathprintnumber{1234,56} |
| 433 |
\end{codeexample} |
| 434 |
% |
| 435 |
This is typically undesired as it can cause side-effects with math parsing |
| 436 |
instructions. However, it is supported to format input numbers or input |
| 437 |
tables. Consider |use comma| to typeset the result with a comma as well. |
| 438 |
% |
| 439 |
\begin{codeexample}[] |
| 440 |
\pgfkeys{/pgf/number format/.cd, |
| 441 |
read comma as period, |
| 442 |
use comma} |
| 443 |
\pgfmathprintnumber{1234,56} |
| 444 |
\end{codeexample} |
| 445 |
% |
| 446 |
\end{key} |
| 447 |
|
| 448 |
|
| 449 |
\subsection{Changing display styles}% |
| 450 |
\label{sec:number:styles} |
| 451 |
|
| 452 |
You can change the way how numbers are displayed. For example, if you use the |
| 453 |
`\texttt{fixed}' style, the input number is rounded to the desired precision |
| 454 |
and the current fixed point display style is used to typeset the number. The |
| 455 |
same is applied to any other format: first, rounding routines are used to get |
| 456 |
the correct digits, afterwards a display style generates proper \TeX-code. |
| 457 |
|
| 458 |
\begin{key}{/pgf/number format/set decimal separator=\marg{text}} |
| 459 |
Assigns \marg{text} as decimal separator for any fixed point numbers |
| 460 |
(including the mantissa in sci format). |
| 461 |
|
| 462 |
Use |\pgfkeysgetvalue{/pgf/number format/set decimal separator}\value| to |
| 463 |
get the current separator into |\value|. |
| 464 |
\end{key} |
| 465 |
|
| 466 |
\begin{stylekey}{/pgf/number format/dec sep=\marg{text}} |
| 467 |
Just another name for |set decimal separator|. |
| 468 |
\end{stylekey} |
| 469 |
|
| 470 |
\begin{key}{/pgf/number format/set thousands separator=\marg{text}} |
| 471 |
Assigns \marg{text} as thousands separator for any fixed point numbers |
| 472 |
(including the mantissa in sci format). |
| 473 |
% |
| 474 |
\begin{codeexample}[] |
| 475 |
\pgfkeys{/pgf/number format/.cd, |
| 476 |
fixed, |
| 477 |
fixed zerofill, |
| 478 |
precision=2, |
| 479 |
set thousands separator={}} |
| 480 |
\pgfmathprintnumber{1234.56} |
| 481 |
\end{codeexample} |
| 482 |
% |
| 483 |
\begin{codeexample}[] |
| 484 |
\pgfkeys{/pgf/number format/.cd, |
| 485 |
fixed, |
| 486 |
fixed zerofill, |
| 487 |
precision=2, |
| 488 |
set thousands separator={}} |
| 489 |
\pgfmathprintnumber{1234567890} |
| 490 |
\end{codeexample} |
| 491 |
|
| 492 |
\begin{codeexample}[] |
| 493 |
\pgfkeys{/pgf/number format/.cd, |
| 494 |
fixed, |
| 495 |
fixed zerofill, |
| 496 |
precision=2, |
| 497 |
set thousands separator={.}} |
| 498 |
\pgfmathprintnumber{1234567890} |
| 499 |
\end{codeexample} |
| 500 |
% |
| 501 |
\begin{codeexample}[] |
| 502 |
\pgfkeys{/pgf/number format/.cd, |
| 503 |
fixed, |
| 504 |
fixed zerofill, |
| 505 |
precision=2, |
| 506 |
set thousands separator={,}} |
| 507 |
\pgfmathprintnumber{1234567890} |
| 508 |
\end{codeexample} |
| 509 |
% |
| 510 |
\begin{codeexample}[] |
| 511 |
\pgfkeys{/pgf/number format/.cd, |
| 512 |
fixed, |
| 513 |
fixed zerofill, |
| 514 |
precision=2, |
| 515 |
set thousands separator={{{,}}}} |
| 516 |
\pgfmathprintnumber{1234567890} |
| 517 |
\end{codeexample} |
| 518 |
% |
| 519 |
The last example employs commas and disables the default comma-spacing. |
| 520 |
|
| 521 |
Use |\pgfkeysgetvalue{/pgf/number format/set thousands separator}\value| to |
| 522 |
get the current separator into |\value|. |
| 523 |
\end{key} |
| 524 |
|
| 525 |
\begin{stylekey}{/pgf/number format/1000 sep=\marg{text}} |
| 526 |
Just another name for |set thousands separator|. |
| 527 |
\end{stylekey} |
| 528 |
|
| 529 |
\begin{key}{/pgf/number format/1000 sep in fractionals=\marg{boolean} (initially false)} |
| 530 |
Configures whether the fractional part should also be grouped into groups |
| 531 |
of three digits. |
| 532 |
|
| 533 |
The value |true| will active the |1000 sep| for both, integer and |
| 534 |
fractional parts. The value |false| will active |1000 sep| only for the |
| 535 |
integer part. |
| 536 |
% |
| 537 |
\begin{codeexample}[] |
| 538 |
\pgfkeys{/pgf/number format/.cd, |
| 539 |
fixed, |
| 540 |
precision=999, |
| 541 |
set thousands separator={\,}, |
| 542 |
1000 sep in fractionals, |
| 543 |
} |
| 544 |
\pgfmathprintnumber{1234.1234567} |
| 545 |
\end{codeexample} |
| 546 |
% |
| 547 |
\begin{codeexample}[] |
| 548 |
\pgfkeys{/pgf/number format/.cd, |
| 549 |
fixed,fixed zerofill, |
| 550 |
precision=9, |
| 551 |
set thousands separator={\,}, |
| 552 |
1000 sep in fractionals, |
| 553 |
} |
| 554 |
\pgfmathprintnumber{1234.1234567} |
| 555 |
\end{codeexample} |
| 556 |
% |
| 557 |
\end{key} |
| 558 |
|
| 559 |
\begin{key}{/pgf/number format/min exponent for 1000 sep=\marg{number} (initially 0)} |
| 560 |
Defines the smallest exponent in scientific notation which is required to |
| 561 |
draw thousand separators. The exponent is the number of digits minus one, |
| 562 |
so $\meta{number}=4$ will use thousand separators starting with $1e4 = |
| 563 |
10000$. |
| 564 |
% |
| 565 |
\begin{codeexample}[] |
| 566 |
\pgfkeys{/pgf/number format/.cd, |
| 567 |
int detect, |
| 568 |
1000 sep={\,}, |
| 569 |
min exponent for 1000 sep=0} |
| 570 |
\pgfmathprintnumber{5000}; \pgfmathprintnumber{1000000} |
| 571 |
\end{codeexample} |
| 572 |
|
| 573 |
\begin{codeexample}[] |
| 574 |
\pgfkeys{/pgf/number format/.cd, |
| 575 |
int detect, |
| 576 |
1000 sep={\,}, |
| 577 |
min exponent for 1000 sep=4} |
| 578 |
\pgfmathprintnumber{1000}; \pgfmathprintnumber{5000} |
| 579 |
\end{codeexample} |
| 580 |
% |
| 581 |
\begin{codeexample}[] |
| 582 |
\pgfkeys{/pgf/number format/.cd, |
| 583 |
int detect, |
| 584 |
1000 sep={\,}, |
| 585 |
min exponent for 1000 sep=4} |
| 586 |
\pgfmathprintnumber{10000}; \pgfmathprintnumber{1000000} |
| 587 |
\end{codeexample} |
| 588 |
% |
| 589 |
\noindent A value of |0| disables this feature (negative values are |
| 590 |
ignored). |
| 591 |
\end{key} |
| 592 |
|
| 593 |
\begin{key}{/pgf/number format/use period} |
| 594 |
A predefined style which installs periods ``\texttt{.}'' as decimal |
| 595 |
separators and commas ``\texttt{,}'' as thousands separators. This style is |
| 596 |
the default. |
| 597 |
% |
| 598 |
\begin{codeexample}[] |
| 599 |
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use period} |
| 600 |
\pgfmathprintnumber{12.3456} |
| 601 |
\end{codeexample} |
| 602 |
% |
| 603 |
\begin{codeexample}[] |
| 604 |
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use period} |
| 605 |
\pgfmathprintnumber{1234.56} |
| 606 |
\end{codeexample} |
| 607 |
% |
| 608 |
\end{key} |
| 609 |
|
| 610 |
\begin{key}{/pgf/number format/use comma} |
| 611 |
A predefined style which installs commas ``\texttt{,}'' as decimal |
| 612 |
separators and periods ``\texttt{.}'' as thousands separators. |
| 613 |
% |
| 614 |
\begin{codeexample}[] |
| 615 |
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use comma} |
| 616 |
\pgfmathprintnumber{12.3456} |
| 617 |
\end{codeexample} |
| 618 |
% |
| 619 |
\begin{codeexample}[] |
| 620 |
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use comma} |
| 621 |
\pgfmathprintnumber{1234.56} |
| 622 |
\end{codeexample} |
| 623 |
% |
| 624 |
\end{key} |
| 625 |
|
| 626 |
\begin{key}{/pgf/number format/skip 0.=\marg{boolean} (initially false)} |
| 627 |
Configures whether numbers like $0.1$ shall be typeset as $.1$ or not. |
| 628 |
% |
| 629 |
\begin{codeexample}[] |
| 630 |
\pgfkeys{/pgf/number format/.cd, |
| 631 |
fixed, |
| 632 |
fixed zerofill,precision=2, |
| 633 |
skip 0.} |
| 634 |
\pgfmathprintnumber{0.56} |
| 635 |
\end{codeexample} |
| 636 |
% |
| 637 |
\begin{codeexample}[] |
| 638 |
\pgfkeys{/pgf/number format/.cd, |
| 639 |
fixed, |
| 640 |
fixed zerofill,precision=2, |
| 641 |
skip 0.=false} |
| 642 |
\pgfmathprintnumber{0.56} |
| 643 |
\end{codeexample} |
| 644 |
% |
| 645 |
\end{key} |
| 646 |
|
| 647 |
\begin{key}{/pgf/number format/showpos=\marg{boolean} (initially false)} |
| 648 |
Enables or disables the display of plus signs for non-negative numbers. |
| 649 |
% |
| 650 |
\begin{codeexample}[] |
| 651 |
\pgfkeys{/pgf/number format/showpos} |
| 652 |
\pgfmathprintnumber{12.345} |
| 653 |
\end{codeexample} |
| 654 |
|
| 655 |
\begin{codeexample}[] |
| 656 |
\pgfkeys{/pgf/number format/showpos=false} |
| 657 |
\pgfmathprintnumber{12.345} |
| 658 |
\end{codeexample} |
| 659 |
|
| 660 |
\begin{codeexample}[] |
| 661 |
\pgfkeys{/pgf/number format/.cd,showpos,sci} |
| 662 |
\pgfmathprintnumber{12.345} |
| 663 |
\end{codeexample} |
| 664 |
% |
| 665 |
\end{key} |
| 666 |
|
| 667 |
\begin{stylekey}{/pgf/number format/print sign=\marg{boolean}} |
| 668 |
A style which is simply an alias for |showpos=|\marg{boolean}. |
| 669 |
\end{stylekey} |
| 670 |
|
| 671 |
\begin{key}{/pgf/number format/sci 10e} |
| 672 |
Uses $m \cdot 10^e$ for any number displayed in scientific format. |
| 673 |
% |
| 674 |
\begin{codeexample}[] |
| 675 |
\pgfkeys{/pgf/number format/.cd,sci,sci 10e} |
| 676 |
\pgfmathprintnumber{12.345} |
| 677 |
\end{codeexample} |
| 678 |
% |
| 679 |
\end{key} |
| 680 |
|
| 681 |
\begin{key}{/pgf/number format/sci 10\textasciicircum e} |
| 682 |
The same as `|sci 10e|'. |
| 683 |
\end{key} |
| 684 |
|
| 685 |
\begin{key}{/pgf/number format/sci e} |
| 686 |
Uses the `$1e{+}0$' format which is generated by common scientific tools |
| 687 |
for any number displayed in scientific format. |
| 688 |
% |
| 689 |
\begin{codeexample}[] |
| 690 |
\pgfkeys{/pgf/number format/.cd,sci,sci e} |
| 691 |
\pgfmathprintnumber{12.345} |
| 692 |
\end{codeexample} |
| 693 |
% |
| 694 |
\end{key} |
| 695 |
|
| 696 |
\begin{key}{/pgf/number format/sci E} |
| 697 |
The same with an uppercase `\texttt{E}'. |
| 698 |
% |
| 699 |
\begin{codeexample}[] |
| 700 |
\pgfkeys{/pgf/number format/.cd,sci,sci E} |
| 701 |
\pgfmathprintnumber{12.345} |
| 702 |
\end{codeexample} |
| 703 |
% |
| 704 |
\end{key} |
| 705 |
|
| 706 |
\begin{key}{/pgf/number format/sci subscript} |
| 707 |
Typesets the exponent as subscript for any number displayed in scientific |
| 708 |
format. This style requires very little space. |
| 709 |
% |
| 710 |
\begin{codeexample}[] |
| 711 |
\pgfkeys{/pgf/number format/.cd,sci,sci subscript} |
| 712 |
\pgfmathprintnumber{12.345} |
| 713 |
\end{codeexample} |
| 714 |
% |
| 715 |
\end{key} |
| 716 |
|
| 717 |
\begin{key}{/pgf/number format/sci superscript} |
| 718 |
Typesets the exponent as superscript for any number displayed in scientific |
| 719 |
format. This style requires very little space. |
| 720 |
% |
| 721 |
\begin{codeexample}[] |
| 722 |
\pgfkeys{/pgf/number format/.cd,sci,sci superscript} |
| 723 |
\pgfmathprintnumber{12.345} |
| 724 |
\end{codeexample} |
| 725 |
% |
| 726 |
\end{key} |
| 727 |
|
| 728 |
\begin{key}{/pgf/number format/sci generic=\marg{keys}} |
| 729 |
Allows to define an own number style for the scientific format. Here, |
| 730 |
\meta{keys} can be one of the following choices (omit the long key prefix): |
| 731 |
|
| 732 |
\begin{key}{/pgf/number format/sci generic/mantissa sep=\marg{text} (initially empty)} |
| 733 |
Provides the separator between a mantissa and the exponent. It might be |
| 734 |
|\cdot|, for example, |
| 735 |
\end{key} |
| 736 |
% |
| 737 |
\begin{key}{/pgf/number format/sci generic/exponent=\marg{text} (initially empty)} |
| 738 |
Provides text to format the exponent. The actual exponent is available |
| 739 |
as argument |#1| (see below). |
| 740 |
\end{key} |
| 741 |
% |
| 742 |
\begin{codeexample}[] |
| 743 |
\pgfkeys{ |
| 744 |
/pgf/number format/.cd, |
| 745 |
sci, |
| 746 |
sci generic={mantissa sep=\times,exponent={10^{#1}}}} |
| 747 |
\pgfmathprintnumber{12.345}; |
| 748 |
\pgfmathprintnumber{0.00012345} |
| 749 |
\end{codeexample} |
| 750 |
% |
| 751 |
The \meta{keys} can depend on three parameters, namely on |#1| which is the |
| 752 |
exponent, |#2| containing the flags entity of the floating point number and |
| 753 |
|#3| is the (unprocessed and unformatted) mantissa. |
| 754 |
|
| 755 |
Note that |sci generic| is \emph{not} suitable to modify the appearance of |
| 756 |
fixed point numbers, nor can it be used to format the mantissa (which is |
| 757 |
typeset like fixed point numbers). Use |dec sep|, |1000 sep| and |
| 758 |
|print sign| to customize the mantissa. |
| 759 |
\end{key} |
| 760 |
|
| 761 |
\begin{key}{/pgf/number format/retain unit mantissa=\mchoice{true,false} (initially true)} |
| 762 |
Allows to omit a unit mantissa. |
| 763 |
% |
| 764 |
\begin{codeexample}[] |
| 765 |
\pgfkeys{ |
| 766 |
/pgf/number format/.cd, |
| 767 |
sci, retain unit mantissa=false} |
| 768 |
\pgfmathprintnumber{10.5}; |
| 769 |
\pgfmathprintnumber{10}; |
| 770 |
\pgfmathprintnumber{1010}; |
| 771 |
\pgfmathprintnumber[precision=1]{-1010}; |
| 772 |
\end{codeexample} |
| 773 |
% |
| 774 |
The feature is applied after rounding to the desired precision: if the |
| 775 |
remaining mantissa is equal to~$1$, it will be omitted. It applies to all |
| 776 |
styles involving the scientific format (including |std|). |
| 777 |
\end{key} |
| 778 |
|
| 779 |
\begin{key}{/pgf/number format/\protect\atmarktext dec sep mark=\marg{text}} |
| 780 |
Will be placed right before the place where a decimal separator belongs to. |
| 781 |
However, \marg{text} will be inserted even if there is no decimal |
| 782 |
separator. It is intended as place-holder for auxiliary routines to find |
| 783 |
alignment positions. |
| 784 |
|
| 785 |
This key should never be used to change the decimal separator! |
| 786 |
Use |dec sep| instead. |
| 787 |
\end{key} |
| 788 |
|
| 789 |
\begin{key}{/pgf/number format/\protect\atmarktext sci exponent mark=\marg{text}} |
| 790 |
Will be placed right before exponents in scientific notation. It is |
| 791 |
intended as place-holder for auxiliary routines to find alignment |
| 792 |
positions. |
| 793 |
|
| 794 |
This key should never be used to change the exponent! |
| 795 |
\end{key} |
| 796 |
|
| 797 |
\begin{key}{/pgf/number format/assume math mode=\marg{boolean} (default true)} |
| 798 |
Set this to |true| if you don't want any checks for math mode. The initial |
| 799 |
setting checks whether math mode is active using |\pgfutilensuremath| for |
| 800 |
each final number. |
| 801 |
|
| 802 |
Use |assume math mode=true| if you know that math mode is active. In that |
| 803 |
case, the final number is typeset as-is, no further checking is performed. |
| 804 |
\end{key} |
| 805 |
|
| 806 |
\begin{stylekey}{/pgf/number format/verbatim} |
| 807 |
A style which configures the number printer to produce verbatim text |
| 808 |
output, i.e., it doesn't contain \TeX\ macros. |
| 809 |
% |
| 810 |
\begin{codeexample}[preamble={\usetikzlibrary{fpu}}] |
| 811 |
\pgfkeys{ |
| 812 |
/pgf/fpu, |
| 813 |
/pgf/number format/.cd, |
| 814 |
sci, |
| 815 |
verbatim} |
| 816 |
\pgfmathprintnumber{12.345}; |
| 817 |
\pgfmathprintnumber{0.00012345}; |
| 818 |
\pgfmathparse{exp(15)} |
| 819 |
\pgfmathprintnumber{\pgfmathresult} |
| 820 |
\end{codeexample} |
| 821 |
% |
| 822 |
The style resets |1000 sep|, |dec sep|, |print sign|, |skip 0.| and sets |
| 823 |
|assume math mode|. Furthermore, it installs a |sci generic| format for |
| 824 |
verbatim output of scientific numbers. |
| 825 |
|
| 826 |
However, it will still respect |precision|, |fixed zerofill|, |
| 827 |
|sci zerofill| and the overall styles |fixed|, |sci|, |int detect| (and |
| 828 |
their variants). It might be useful if you intend to write output files. |
| 829 |
\end{stylekey} |
| 830 |
|
| 831 |
|
| 832 |
%-------------------------------------------------- |
| 833 |
% \subsubsection{Defining own display styles} |
| 834 |
% You can define own display styles, although this may require some insight into \TeX-programming. Here are two examples: |
| 835 |
% \begin{enumerate} |
| 836 |
% \item A new fixed point display style: The following code defines a new style named `\texttt{my own fixed point style}' which uses $1{\cdot}00$ instead of $1.00$. |
| 837 |
% \begin{lstlisting} |
| 838 |
% \def\myfixedpointstyleimpl#1.#2\relax{% |
| 839 |
% #1{\cdot}#2% |
| 840 |
% }% |
| 841 |
% \def\myfixedpointstyle#1{% |
| 842 |
% \pgfutilensuremath{% |
| 843 |
% \ifpgfmathfloatroundhasperiod |
| 844 |
% \expandafter\myfixedpointstyleimpl#1\relax |
| 845 |
% \else |
| 846 |
% #1% |
| 847 |
% \fi |
| 848 |
% }% |
| 849 |
% } |
| 850 |
% \pgfkeys{/my own fixed point style/.code={% |
| 851 |
% \let\pgfmathprintnumber@fixed@style=\myfixedpointstyle} |
| 852 |
% }% |
| 853 |
% \end{lstlisting} |
| 854 |
% You only need to overwrite the macro \lstinline!\pgfmathprintnumber@fixed@style!. This macro takes one argument (the result of any numerical computations). The \TeX-boolean \lstinline!\ifpgfmathfloatroundhasperiod! is true if and only if the input number contains a period. |
| 855 |
% |
| 856 |
% \item An example for a new scientific display style: |
| 857 |
% \begin{lstlisting} |
| 858 |
% % #1: |
| 859 |
% % 0 == '0' (the number is +- 0.0), |
| 860 |
% % 1 == '+', |
| 861 |
% % 2 == '-', |
| 862 |
% % 3 == 'not a number' |
| 863 |
% % 4 == '+ infinity' |
| 864 |
% % 5 == '- infinity' |
| 865 |
% % #2: the mantissa |
| 866 |
% % #3: the exponent |
| 867 |
% \def\myscistyle#1#2e#3\relax{% |
| 868 |
% ... |
| 869 |
% } |
| 870 |
% \pgfkeys{/my own sci style/.code={% |
| 871 |
% \let\pgfmathfloatrounddisplaystyle=\myscistyle}, |
| 872 |
% }% |
| 873 |
% \end{lstlisting} |
| 874 |
% \end{enumerate} |
| 875 |
%-------------------------------------------------- |