Applied (mostly) as r3203 and r3201
[PyX/mjg.git] / manual / unit.rst
blob880ed805f8717e32d98abfe4ec8e97b8e0ac66fb
2 .. module:: unit
4 .. _module_unit:
6 ******************
7 Module :mod:`unit`
8 ******************
10 .. sectionauthor:: Jörg Lehmann <joergl@users.sourceforge.net>
15 With the ``unit`` module PyX makes available classes and functions for the
16 specification and manipulation of lengths. As usual, lengths consist of a number
17 together with a measurement unit, e.g., 1 cm, 50 points, 0.42 inch.  In
18 addition, lengths in PyX are composed of the five types "true", "user",
19 "visual", "width", and "TeX", e.g., 1 user cm, 50 true points, 0.42 visual + 0.2
20 width inch.  As their names indicate, they serve different purposes. True
21 lengths are not scalable and are mainly used for return values of PyX functions.
22 The other length types can be rescaled by the user and differ with respect to
23 the type of object they are applied to:
25 user length:
26    used for lengths of graphical objects like positions etc.
28 visual length:
29    used for sizes of visual elements, like arrows, graph symbols, axis ticks, etc.
31 width length:
32    used for line widths
34 TeX length:
35    used for all TeX and LaTeX output
37 When not specified otherwise, all types of lengths are interpreted in terms of a
38 default unit, which, by default, is 1 cm. You may change this default unit by
39 using the module level function
42 .. function:: set(uscale=None, vscale=None, wscale=None, xscale=None, defaultunit=None)
44    When *uscale*, *vscale*, *wscale*, or *xscale* is not `None`, the
45    corresponding scaling factor(s) is redefined to the given number. When
46    *defaultunit* is not `None`,  the default unit is set to the given
47    value, which has to be one of ``"cm"``, ``"mm"``, ``"inch"``, or ``"pt"``.
49 For instance, if you only want thicker lines for a publication version of your
50 figure, you can just rescale all width lengths using  ::
52    unit.set(wscale=2)
54 Or suppose, you are used to specify length in imperial units. In this,
55 admittedly rather unfortunate case, just use  ::
57    unit.set(defaultunit="inch")
59 at the beginning of your program.
62 Class :class:`length`
63 =====================
66 .. class:: length(f, type="u", unit=None)
68    The constructor of the :class:`length` class expects as its first argument a
69    number *f*, which represents the prefactor of the given length. By default this
70    length is interpreted as a user length (``type="u"``) in units of the current
71    default unit (see :func:`set` function of the :mod:`unit` module). Optionally, a
72    different *type* may be specified, namely ``"u"`` for user lengths, ``"v"`` for
73    visual lengths, ``"w"`` for width lengths, ``"x"`` for TeX length, and ``"t"``
74    for true lengths. Furthermore, a different unit may be specified using the
75    *unit* argument. Allowed values are ``"cm"``, ``"mm"``, ``"inch"``, and
76    ``"pt"``.
78 Instances of the :class:`length` class support addition and substraction either
79 by another :class:`length` or by a number which is then interpeted as being a
80 user length in  default units, multiplication by a number and division either by
81 another :class:`length` in which case a float is returned or by a number in
82 which case a :class:`length` instance is returned. When two lengths are
83 compared, they are first converted to meters (using the currently set scaling),
84 and then the resulting values are compared.
87 Predefined length instances
88 ===========================
90 A number of ``length`` instances are already predefined, which only differ in
91 there values for ``type`` and ``unit``. They are summarized in the following
92 table
94 +-----------------+--------+--------+
95 | name            | type   | unit   |
96 +=================+========+========+
97 | :const:`m`      | user   | m      |
98 +-----------------+--------+--------+
99 | :const:`cm`     | user   | cm     |
100 +-----------------+--------+--------+
101 | :const:`mm`     | user   | mm     |
102 +-----------------+--------+--------+
103 | :const:`inch`   | user   | inch   |
104 +-----------------+--------+--------+
105 | :const:`pt`     | user   | points |
106 +-----------------+--------+--------+
107 | :const:`t_m`    | true   | m      |
108 +-----------------+--------+--------+
109 | :const:`t_cm`   | true   | cm     |
110 +-----------------+--------+--------+
111 | :const:`t_mm`   | true   | mm     |
112 +-----------------+--------+--------+
113 | :const:`t_inch` | true   | inch   |
114 +-----------------+--------+--------+
115 | :const:`t_pt`   | true   | points |
116 +-----------------+--------+--------+
117 | :const:`u_m`    | user   | m      |
118 +-----------------+--------+--------+
119 | :const:`u_cm`   | user   | cm     |
120 +-----------------+--------+--------+
121 | :const:`u_mm`   | user   | mm     |
122 +-----------------+--------+--------+
123 | :const:`u_inch` | user   | inch   |
124 +-----------------+--------+--------+
125 | :const:`u_pt`   | user   | points |
126 +-----------------+--------+--------+
127 | :const:`v_m`    | visual | m      |
128 +-----------------+--------+--------+
129 | :const:`v_cm`   | visual | cm     |
130 +-----------------+--------+--------+
131 | :const:`v_mm`   | visual | mm     |
132 +-----------------+--------+--------+
133 | :const:`v_inch` | visual | inch   |
134 +-----------------+--------+--------+
135 | :const:`v_pt`   | visual | points |
136 +-----------------+--------+--------+
137 | :const:`w_m`    | width  | m      |
138 +-----------------+--------+--------+
139 | :const:`w_cm`   | width  | cm     |
140 +-----------------+--------+--------+
141 | :const:`w_mm`   | width  | mm     |
142 +-----------------+--------+--------+
143 | :const:`w_inch` | width  | inch   |
144 +-----------------+--------+--------+
145 | :const:`w_pt`   | width  | points |
146 +-----------------+--------+--------+
147 | :const:`x_m`    | TeX    | m      |
148 +-----------------+--------+--------+
149 | :const:`x_cm`   | TeX    | cm     |
150 +-----------------+--------+--------+
151 | :const:`x_mm`   | TeX    | mm     |
152 +-----------------+--------+--------+
153 | :const:`x_inch` | TeX    | inch   |
154 +-----------------+--------+--------+
155 | :const:`x_pt`   | TeX    | points |
156 +-----------------+--------+--------+
158 Thus, in order to specify, e.g., a length of 5 width points, just use
159 ``5*unit.w_pt``.
162 Conversion functions
163 ====================
165 If you want to know the value of a PyX length in certain units, you may use the
166 predefined conversion functions which are given in the following table
168 +---------------+--------------------------+
169 | function      | result                   |
170 +===============+==========================+
171 | ``tom(l)``    | ``l`` in units of m      |
172 +---------------+--------------------------+
173 | ``tocm(l)``   | ``l`` in units of cm     |
174 +---------------+--------------------------+
175 | ``tomm(l)``   | ``l`` in units of mm     |
176 +---------------+--------------------------+
177 | ``toinch(l)`` | ``l`` in units of inch   |
178 +---------------+--------------------------+
179 | ``topt(l)``   | ``l`` in units of points |
180 +---------------+--------------------------+
182 If ``l`` is not yet a ``length`` instance but a number, it first is interpreted
183 as a user length in the default units.