Debian Codegraph @

texmf-dist/tex/generic/pgfplots/libs/tikzlibrarypgfplots.units.code.tex

raw
1 %--------------------------------------------
2 %
3 % Package pgfplots
4 %
5 % Provides a user-friendly interface to create function plots (normal
6 % plots, semi-logplots and double-logplots).
7 %
8 % It is based on Till Tantau's PGF package.
9 %
10 % Copyright 2009 by Nick Papior Andersen.
11 %
12 % This program is free software: you can redistribute it and/or modify
13 % it under the terms of the GNU General Public License as published by
14 % the Free Software Foundation, either version 3 of the License, or
15 % (at your option) any later version.
16 %
17 % This program is distributed in the hope that it will be useful,
18 % but WITHOUT ANY WARRANTY; without even the implied warranty of
19 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 % GNU General Public License for more details.
21 %
22 % You should have received a copy of the GNU General Public License
23 % along with this program. If not, see <http://www.gnu.org/licenses/>.
24 %
25 %--------------------------------------------
26
27 %
28 % This file contains the implementation for using units in labels.
29 %
30 % Default the unit setting system isn't used, It is thus required to use the key "use units".
31 %
32 % Programming Structure:
33 %
34 % 1. All associated keys are first set. This includes some prefix values of the unit system.
35 % In example one can redefine prefix and postfix such that units are shown on a per-user setting.
36 %
37 % 2. The other main thing is scaling of the dimension. If one uses units one might not
38 % have scaled the data. For this the keys "scaled <axis> ticks" are used at a base 10.
39 % Currently it is not provided to scale in other bases.
40 % FIXME Adding of other bases.
41 %
42 % 3. Some predefined SI prefixes are specified which should minimize mistakes.
43 % For example the key "change <axis> base,<axis> SI prefix=kilo" allows the
44 % <axis> to be rescaled according to the above at base 10 with a factor of -3 and sets the
45 % unit prefix to "k".
46 % FIXME Adding other basis systems than SI. For example the addition of CGS units.
47
48 \newif\ifpgfplots@units@change@x@base
49 \newif\ifpgfplots@units@change@y@base
50 \newif\ifpgfplots@units@change@z@base
51
52 %
53 % Constants for writing the unit marking
54 %
55 \def\pgfplots@units@marking@pre{}
56 \def\pgfplots@units@marking@post{}
57
58 \pgfkeys{%
59 /pgfplots/use units/.is if=pgfplots@units@use,%
60 /pgfplots/use units=true,% So when called from \usepgfplotslibrary{units} it's automatically 'on'
61 %
62 % Setup basic x settings
63 %
64 /pgfplots/x unit/.initial={},%
65 /pgfplots/x unit prefix/.initial={},%
66 /pgfplots/change x base/.is if=pgfplots@units@change@x@base,%
67 /pgfplots/change x base=false,%
68 %
69 % Setup basic y settings
70 %
71 /pgfplots/y unit/.initial={},%
72 /pgfplots/y unit prefix/.initial={},%
73 /pgfplots/change y base/.is if=pgfplots@units@change@y@base,%
74 /pgfplots/change y base=false,%
75 %
76 % Setup basic z settings
77 %
78 /pgfplots/z unit/.initial={},%
79 /pgfplots/z unit prefix/.initial={},%
80 /pgfplots/change z base/.is if=pgfplots@units@change@z@base,%
81 /pgfplots/change z base=false,%
82 %
83 % default markings of units. I.e. the surroundings of the unit:
84 % [unit markings=curly braces] yields {<unit>}
85 %
86 /pgfplots/unit marking pre/.store in={\pgfplots@units@marking@pre},%
87 /pgfplots/unit marking post/.store in={\pgfplots@units@marking@post},%
88 /pgfplots/unit markings/.is choice,%
89 /pgfplots/unit markings/parenthesis/.style={/pgfplots/unit marking pre={\left(},/pgfplots/unit marking post={\right)}},%
90 /pgfplots/unit markings/square braces/.style={/pgfplots/unit marking pre={\left[},/pgfplots/unit marking post={\right]}},%
91 /pgfplots/unit markings/curly braces/.style={/pgfplots/unit marking pre={\left\{},/pgfplots/unit marking post={\right\}}},%
92 /pgfplots/unit markings/slash space/.style={/pgfplots/unit marking pre={/$\space$},/pgfplots/unit marking post={\relax}},%
93 /pgfplots/unit markings=square braces,%
94 %
95 % The code used to type units, and the base change code.
96 %
97 /pgfplots/unit code/.code 2 args={\mathrm{#1#2}},%
98 /pgfplots/axis base prefix/.code args={axis #1 base #2 prefix #3}{%
99 \csname ifpgfplots@units@change@#1@base\endcsname%
100 \pgfkeys{/pgfplots/#1\space unit prefix={#3},%
101 /pgfplots/scaled #1\space ticks={base 10:#2},%
102 /pgfplots/#1tick scale label code/.code={\relax}%
103 }%
104 \else%
105 \pgfkeys{/pgfplots/#1\space unit prefix={#3}}%
106 \fi%
107 },%
108 %
109 % x SI prefixes
110 %
111 /pgfplots/x SI prefix/.is choice,%
112 /pgfplots/x SI prefix/yocto/.style={/pgfplots/axis base prefix={axis x base 24 prefix y}},%
113 /pgfplots/x SI prefix/zepto/.style={/pgfplots/axis base prefix={axis x base 21 prefix z}},%
114 /pgfplots/x SI prefix/atto/.style={/pgfplots/axis base prefix={axis x base 18 prefix a}},%
115 /pgfplots/x SI prefix/femto/.style={/pgfplots/axis base prefix={axis x base 15 prefix f}},%
116 /pgfplots/x SI prefix/pico/.style={/pgfplots/axis base prefix={axis x base 12 prefix p}},%
117 /pgfplots/x SI prefix/nano/.style={/pgfplots/axis base prefix={axis x base 9 prefix n}},%
118 /pgfplots/x SI prefix/micro/.style={/pgfplots/axis base prefix={axis x base 6 prefix \mu}},%
119 /pgfplots/x SI prefix/milli/.style={/pgfplots/axis base prefix={axis x base 3 prefix m}},%
120 /pgfplots/x SI prefix/centi/.style={/pgfplots/axis base prefix={axis x base 2 prefix c}},%
121 /pgfplots/x SI prefix/deci/.style={/pgfplots/axis base prefix={axis x base 1 prefix d}},%
122 /pgfplots/x SI prefix/deca/.style={/pgfplots/axis base prefix={axis x base -1 prefix da}},%
123 /pgfplots/x SI prefix/hecto/.style={/pgfplots/axis base prefix={axis x base -2 prefix h}},%
124 /pgfplots/x SI prefix/kilo/.style={/pgfplots/axis base prefix={axis x base -3 prefix k}},%
125 /pgfplots/x SI prefix/mega/.style={/pgfplots/axis base prefix={axis x base -6 prefix M}},%
126 /pgfplots/x SI prefix/giga/.style={/pgfplots/axis base prefix={axis x base -9 prefix G}},%
127 /pgfplots/x SI prefix/tera/.style={/pgfplots/axis base prefix={axis x base -12 prefix T}},%
128 /pgfplots/x SI prefix/peta/.style={/pgfplots/axis base prefix={axis x base -15 prefix P}},%
129 /pgfplots/x SI prefix/exa/.style={/pgfplots/axis base prefix={axis x base -18 prefix E}},%
130 /pgfplots/x SI prefix/zetta/.style={/pgfplots/axis base prefix={axis x base -21 prefix Z}},%
131 /pgfplots/x SI prefix/yotta/.style={/pgfplots/axis base prefix={axis x base -24 prefix Y}},%
132 %
133 % y SI prefixes
134 %
135 /pgfplots/y SI prefix/.is choice,%
136 /pgfplots/y SI prefix/yocto/.style={/pgfplots/axis base prefix={axis y base 24 prefix y}},%
137 /pgfplots/y SI prefix/zepto/.style={/pgfplots/axis base prefix={axis y base 21 prefix z}},%
138 /pgfplots/y SI prefix/atto/.style={/pgfplots/axis base prefix={axis y base 18 prefix a}},%
139 /pgfplots/y SI prefix/femto/.style={/pgfplots/axis base prefix={axis y base 15 prefix f}},%
140 /pgfplots/y SI prefix/pico/.style={/pgfplots/axis base prefix={axis y base 12 prefix p}},%
141 /pgfplots/y SI prefix/nano/.style={/pgfplots/axis base prefix={axis y base 9 prefix n}},%
142 /pgfplots/y SI prefix/micro/.style={/pgfplots/axis base prefix={axis y base 6 prefix \mu}},%
143 /pgfplots/y SI prefix/milli/.style={/pgfplots/axis base prefix={axis y base 3 prefix m}},%
144 /pgfplots/y SI prefix/centi/.style={/pgfplots/axis base prefix={axis y base 2 prefix c}},%
145 /pgfplots/y SI prefix/deci/.style={/pgfplots/axis base prefix={axis y base 1 prefix d}},%
146 /pgfplots/y SI prefix/deca/.style={/pgfplots/axis base prefix={axis y base -1 prefix da}},%
147 /pgfplots/y SI prefix/hecto/.style={/pgfplots/axis base prefix={axis y base -2 prefix h}},%
148 /pgfplots/y SI prefix/kilo/.style={/pgfplots/axis base prefix={axis y base -3 prefix k}},%
149 /pgfplots/y SI prefix/mega/.style={/pgfplots/axis base prefix={axis y base -6 prefix M}},%
150 /pgfplots/y SI prefix/giga/.style={/pgfplots/axis base prefix={axis y base -9 prefix G}},%
151 /pgfplots/y SI prefix/tera/.style={/pgfplots/axis base prefix={axis y base -12 prefix T}},%
152 /pgfplots/y SI prefix/peta/.style={/pgfplots/axis base prefix={axis y base -15 prefix P}},%
153 /pgfplots/y SI prefix/exa/.style={/pgfplots/axis base prefix={axis y base -18 prefix E}},%
154 /pgfplots/y SI prefix/zetta/.style={/pgfplots/axis base prefix={axis y base -21 prefix Z}},%
155 /pgfplots/y SI prefix/yotta/.style={/pgfplots/axis base prefix={axis y base -24 prefix Y}},%
156 %
157 % z SI prefixes
158 %
159 /pgfplots/z SI prefix/.is choice,%
160 /pgfplots/z SI prefix/yocto/.style={/pgfplots/axis base prefix={axis z base 24 prefix y}},%
161 /pgfplots/z SI prefix/zepto/.style={/pgfplots/axis base prefix={axis z base 21 prefix z}},%
162 /pgfplots/z SI prefix/atto/.style={/pgfplots/axis base prefix={axis z base 18 prefix a}},%
163 /pgfplots/z SI prefix/femto/.style={/pgfplots/axis base prefix={axis z base 15 prefix f}},%
164 /pgfplots/z SI prefix/pico/.style={/pgfplots/axis base prefix={axis z base 12 prefix p}},%
165 /pgfplots/z SI prefix/nano/.style={/pgfplots/axis base prefix={axis z base 9 prefix n}},%
166 /pgfplots/z SI prefix/micro/.style={/pgfplots/axis base prefix={axis z base 6 prefix \mu}},%
167 /pgfplots/z SI prefix/milli/.style={/pgfplots/axis base prefix={axis z base 3 prefix m}},%
168 /pgfplots/z SI prefix/centi/.style={/pgfplots/axis base prefix={axis z base 2 prefix c}},%
169 /pgfplots/z SI prefix/deci/.style={/pgfplots/axis base prefix={axis z base 1 prefix d}},%
170 /pgfplots/z SI prefix/deca/.style={/pgfplots/axis base prefix={axis z base -1 prefix da}},%
171 /pgfplots/z SI prefix/hecto/.style={/pgfplots/axis base prefix={axis z base -2 prefix h}},%
172 /pgfplots/z SI prefix/kilo/.style={/pgfplots/axis base prefix={axis z base -3 prefix k}},%
173 /pgfplots/z SI prefix/mega/.style={/pgfplots/axis base prefix={axis z base -6 prefix M}},%
174 /pgfplots/z SI prefix/giga/.style={/pgfplots/axis base prefix={axis z base -9 prefix G}},%
175 /pgfplots/z SI prefix/tera/.style={/pgfplots/axis base prefix={axis z base -12 prefix T}},%
176 /pgfplots/z SI prefix/peta/.style={/pgfplots/axis base prefix={axis z base -15 prefix P}},%
177 /pgfplots/z SI prefix/exa/.style={/pgfplots/axis base prefix={axis z base -18 prefix E}},%
178 /pgfplots/z SI prefix/zetta/.style={/pgfplots/axis base prefix={axis z base -21 prefix Z}},%
179 /pgfplots/z SI prefix/yotta/.style={/pgfplots/axis base prefix={axis z base -24 prefix Y}}%
180 }
181 %
182 % The initial insert of labels. It utilizes the same method of implementation as
183 % the default label typesetting.
184 %
185 \def\pgfplots@label@units#1{%
186 \pgfkeysgetvalue{/pgfplots/#1label}{\pgfplots@loc@TMPa}%
187 \pgfplots@loc@TMPa\space\pgfplots@label@units@@{#1}%
188 }
189
190 %
191 % The main macro for typesetting the units. In steps:
192 %
193 % 1. Save the unit prefix, unit and the unit code in macros.
194 %
195 % 2. If the unit isn't present then don't utilize any of the things.
196 % FIXME, should this also happen if the prefix is given? Currently it is not!
197 %
198 % 3. Defines \pgfplots@loc@TMPa as the real typeset label.
199 % It inserts the <marking pre><unit prefix><unit><marking post>.
200 % It is encapsulated within a $$ notation. Currently this is enough.
201 % FIXME, maybe this should be changed to \ensuremath?
202 %
203 \def\pgfplots@label@units@@#1{%
204 \pgfkeysgetvalue{/pgfplots/#1\space unit prefix}{\pgfplots@loc@TMPb}%
205 \pgfkeysgetvalue{/pgfplots/#1\space unit}{\pgfplots@loc@TMPc}%
206 \ifx\pgfplots@loc@TMPc\pgfutil@empty%
207 \pgfutil@empty%
208 \else%
209 \ifx\pgfplots@loc@TMPb\pgfutil@empty%
210 $
211 \pgfplots@units@marking@pre
212 \pgfplots@invoke@pgfkeyscode{/pgfplots/unit code/.@cmd}{{}{\pgfkeysvalueof{/pgfplots/#1\space unit}}}
213 \pgfplots@units@marking@post
214 $%
215 \else%
216 $
217 \pgfplots@units@marking@pre
218 \pgfplots@invoke@pgfkeyscode{/pgfplots/unit code/.@cmd}{{\pgfkeysvalueof{/pgfplots/#1\space unit prefix}}{\pgfkeysvalueof{/pgfplots/#1\space unit}}}
219 \pgfplots@units@marking@post
220 $%
221 \fi%
222 \fi%
223 }
224 \endinput