Manual for Package pgfplots
2D/3D Plots in LATeX, Version 1.18.1
http://sourceforge.net/projects/pgfplots
Related Libraries
5.14Units in Labels
by Nick Papior Andersen
-
\usepgfplotslibrary{units} % LaTeX and plain TeX ¶
-
\usepgfplotslibrary[units] % ConTeXt ¶
-
\usetikzlibrary{pgfplots.units} % LaTeX and plain TeX ¶
-
\usetikzlibrary[pgfplots.units] % ConTeXt ¶
A library which allows to use automatic typesetting of units in labels. The library utilizes different keys to typeset the final output in a consistent way. Calling one of the commands automatically sets the key ‘use units=true’ so one does not have to worry about this.
pgfplots has the capability of supporting units. This provides quick customization of the plot as well as the addition of units in labels.
Loading the library automatically enables the typesetting of units in labels. Currently it only supports predefined SI units but a per-user customization is also implemented such that it can be used in any way you like.
First the key which enables you to switch on/off the unit system.
-
/pgfplots/use units={boolean} (initially true) ¶
This key simply enables pgfplots to use what is described next. This key will be set to true if you load the library. You can use this to temporarily determine whether the unit library should be used in plots.
-
/pgfplots/x unit prefix={prefix} (initially empty) ¶
-
/pgfplots/y unit prefix={prefix} (initially empty) ¶
-
/pgfplots/z unit prefix={prefix} (initially empty) ¶
These keys set the prefix of the unit. If a value on the y axis is in kilo you would set the y unit prefix=k. Prefix will be typeset in front of the unit.
This command will not intervene with the basis of the axis system. That is a prefix as just mentioned will not divide every y axis number by 1000. In order to do this, see key axis SI prefix, see Section 5.14.1.
Notice that if the axis unit isn’t set the entire unit will not be typeset.
Remember that all typesetting of labels occur within math mode (i.e. within $ delimiters). Therefore one can use \frac and other mathematics commands.
Often one just has to utilize the above mentioned keys. It is the basis of the unit typesetting system provided by pgfplots.
Below is an example of what would be obtained according to the styles
Notice the second example. Only setting the prefix will not activate the unit typesetting. Therefore one should ensure to use the x unit key if the typesetting of the labels should be done.
For typesetting the units one can also change the appearance. For instance one might not like the square brackets which surround the unit. These can luckily be changed using the below keys.
-
/pgfplots/unit marking pre={pre} (initially \left[) ¶
-
/pgfplots/unit marking post={post} (initially \right]) ¶
-
/pgfplots/unit markings=parenthesis|square brackets|slash space (initially square brackets) ¶
These keys set the surroundings of the unit. The initial yields \(\left [\frac {1}{2}\right ]\) such that you can typeset fractions in units. Be aware that you can only obtain large fractions if you use \dfrac. These can easily be set using the option key unit markings where the options typesets as the following
Notice that all typesetting of units first inserts a space and then the unit marking pre code.
Of course you can just manually set each of them with the unit marking pre and unit marking post keys. Just remember that they are typeset within a $ delimiters.
One will typically typeset the unit with a specific font. To do so an option of changing the typesetting command is supplied.
-
/pgfplots/unit code/.code 2 args={...} ¶
This can be utilized to great extent. By default, units are typeset as \mathrm{unit prefixunit}. But if one for instance wishes to utilize the package siunitx, which has great capabilities in typesetting both units, numbers and angles, one can just set the key as
which would yield the unit as \si{unit prefixunit}.
The first argument is typeset as unit prefix and the second argument is unit.
The most important thing is that the command needs exactly two arguments. So if you would like a command that typesets the prefix in bold face and the unit in normal roman font you should call
5.14.1Preset SI prefixes¶
To support the SI system a number of preset keys are defined. This should yield a more intuitive way of supplying the prefix as well as add some more functionality. For instance it provides an easy scaling mechanism.
-
/pgfplots/x SI prefix=yocto|…|milli|centi|deci|deca|hecto|kilo|…|yotta (initially none) ¶
-
/pgfplots/y SI prefix=yocto|…|milli|centi|deci|deca|hecto|kilo|…|yotta (initially none) ¶
-
/pgfplots/z SI prefix=yocto|…|milli|centi|deci|deca|hecto|kilo|…|yotta (initially none) ¶
-
/pgfplots/change x base=true|false (initially false) ¶
-
/pgfplots/change y base=true|false (initially false) ¶
-
/pgfplots/change z base=true|false (initially false) ¶
These keys sets the prefix of the unit. The allowed prefixes are:
Prefix | Power |
yocto | -24 |
zepto | -21 |
atto | -18 |
femto | -15 |
pico | -12 |
nano | -9 |
micro | -6 |
milli | -3 |
centi | -2 |
deci | -1 |
Prefix | Power |
deca | 1 |
hecto | 2 |
kilo | 3 |
mega | 6 |
giga | 9 |
tera | 12 |
peta | 15 |
exa | 18 |
zetta | 21 |
yotta | 24 |
As well as resetting the base of the axis if the key change axis base=true. Just remember to set the change axis base before using the axis SI prefix key.
See the utilization as in the example below.
Notice that the x axis has changed base without displaying the \(\cdot 10^{3}\). This is done by using the key change x base. Even though you have used the key y SI prefix=milli the base isn’t changed on the y axis. Try adding change y base just after change x base and see the result!
The above keys are the easy implementation of the base change. Below is a further customization of the base change. It makes it easy to implement a prefix with a custom base change.
-
/pgfplots/axis base prefix=axis {axis} base {base} prefix {prefix} (initially empty) ¶
One can utilize this key to customize further of the base and setting the prefix.
The above two commands are thus equivalent. Remember that the base should operate in opposite of prefix!